Jankins with Vagrant - lago-morph/chiller GitHub Wiki
Manual install using Vagrant and fresh Ubuntu 22.04 image
Want to learn a bit more about both Jenkins and Vagrant. So I'll spin up a Vagrant box and do the experimentation there.
Start with fresh Vagrantfile: vagrant init ubuntu/jammy64
, vagrant up
, vagrant ssh
. Note, I have not found a Canonical or Hashicorp noble box, so I'll stick with 22.04 (jammy) for now.
vagrant up
froze at something along the lines of the ssh key method. It turns out it just took a long time to boot. Brought up the virtual box GUI, and could watch the boot process.
Pasting into a Powershell terminal doesn't work the same way as with PuTTY. When I paste a multi-line thing, the shell only executes the first one, and throws away the rest. I don't know if I'm doing something wrong or it is a setting. Annoying.
Do what is needed to get Jenkins running. Using this web page as reference.
Installing openjdk-21-jre takes a LONG time. Should build a box with Packer to make this fast.
sudo apt install fontconfig openjdk-21-jre -y
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
Manually added a port forward using the virtual box GUI from 8080 on the guest to 8080 on the host.
Grabbed the admin password from this file on the guest: /var/lib/jenkins/secrets/initialAdminPassword
On the page that comes up, installed suggested plugins.
Created admin user admin/admin.
Accepted default value for Jenkins URL (127.0.0.1:8080)
Now shift over to guided tour tutorial.
- Installed Docker Pipeline plugin
- Restarted Jenkins with
sudo systemctl restart jenkins
(which takes a long time for some reason) - Created the lago-morph/jenkins-test repository as a public repo
- And this is where the whole packer/vagrant thing breaks down a bit. I don't have my private keys installed on this throwaway environment. What I probably need to do is keep them in my user directory on Windows somewhere, and when I bring up a machine with Vagrant it just copies them over, and does the git config as well. Since I don't want to mess with this right now, I will bring up my normal VMWare Linux box and do the git stuff from there.
- Add the suggested python version of a Jenkinsfile to the repo and push it back up to github
- Create a multi-branch pipeline called jenkins-test, and provide the information for the above git repository.
- It ran the build queue job quickly, and is sitting at the build executor status build and just sort of waiting there...
- Docker not installed on my vagrant machine. Get that installed, since it is trying to use Docker to "run" the build pipeline
- you would think it would provide an error, rather than just spinning on the pipeline...
sudo apt install docker.io -y
- was getting permission errors, had to
sudo usermod -aG docker vagrant
thennewgrp docker
and it fixed it for the vagrant user. Do the same thing, I may have to restart Jenkins, we will see. Yes, have to restart Jenkins, as the running process is not associated with the group. - Then the pipeline run was successful (downloaded python docker image, ran python --version)
- In Manage Jenkins/System I set the GitHub rate limit to be only when at or near limit. I don't need that for testing.
Finished up the tutorial
Get git running on Windows
Took a side trip (all of this is on Windows)
- Set up git for Windows
- Download binary from git-scm.com
git config --global user.name "Jonathan Manton"
git config --global user.email "[email protected]"
- Install winget-cli (part of the AppInstaller package on the Microsoft Store)
- Install gh-cli with winget
winget install --id GitHub.cli
- Set up git authorization
gh auth login
(and this time I could do it through web browser, which was easier)
Build custom Jenkins box for Vagrant
Using Packer, was able to create a custom Vagrant box that has all the steps in the manual install except for mapping the port and adding the vagrant user to the docker group (it does add the jenkins user to this group). This is all in the jenkins-test public repository in the packer directory.
It takes just under 20 minutes to complete the Packer build on my laptop. From the packer directory, have to add the box to Vagrant, but can't do it in the output directory for some reason (!?)
mkdir vagrant
cd .\vagrant\
cp ..\output-jenkins-jammy64\package.box .
vagrant box add jenkins-jammy64 .\package.box
vagrant init jenkins-jammy64
Add the following to the configuration part of the Vagrantfile generated by the vagrant init
command above:
config.vm.network :forwarded_port, guest: 8080, host: 8080
I do the port mapping in the Vagrantfile, because that is sort of a user preference thing, rather than something I want to bake into the box. User may want the host port to be different, for instance.
Then
vagrant up
vagrant ssh
Web page is now available at 127.0.0.1:8080.
Using the web interface, logged in and created an admin user (admin/admin) and installed the default plugins.
Test vagrant box using Jenkins CLI
Set up environment variables and download the Jenkins CLI from the Jenkins server process.
export JENKINS_URL=http://127.0.0.1:8080
export JENKINS_USER_ID=admin
export JENKINS_API_TOKEN=admin
wget $JENKINS_URL/jnlpJars/jenkins-cli.jar
Do a simple CLI command to make sure it works (this just lists the commands available)
java -jar jenkins-cli.jar help
Install docker-workflow plugin and restart
java -jar jenkins-cli.jar install-plugin docker-workflow -restart
Do the manual stuff from the tutorial, then grab the XML configs so I can do it from the command line.
Had to disable Git connection throttling unless near limit. Could not figure out how to do that anywhere but the web interface. Looked through /usr/lib/jenkins
directory, and diffed config.xml
before and after, and it wasn't obvious (unless the thing I turned off was the hudson.util.DoubleLaunchChecker
which might have been it). Not going to spend any more time on this.
EDIT - found a couple of ways to do this:
- Groovy init scripts (page linked below says this is advanced and error prone)
- The Jenkins Configuration as Code (JCasC) plugin which uses a yaml file that can be changed on the filesystem and Jenkins can then be told to apply that configuration.
Related to configuration, it seems if you define Jenkinsfiles in a set of GitHub repositories, it can scan them and create jobs for you.
My idea of creating things manually, then dumping the XML ran into the fact that it inserts tons of default stuff when created, and the XML file is big enough that you can't easily comprehend it.
Got the XML with
java -jar jenkins-cli.jar list-jobs
java -jar jenkins-cli.jar get-job my-jenkins-test-item
I followed the tutorial, and the box is running Docker-based pipeline jobs (docker-workflow
plugin) with no problems.