Jenkins
Jenkins Setup Guide and General Information
Updated: 03 September 2023
The Guide for getting started can be found here
Setup
Prerequisites
- Docker
- Java 8
- Download Jenkins here
Place your jenkins.war
file in the project root directory
Running
Run jenkins with the following command from the project root
Navigate to http://localhost:8080
in your browser and complete the setup
You may need to configure the proxy, do this using the version without the
http://
part - for some reason it doesn’t work otherwise. The Jenkins install will be atC:\Users\USERNAME\.jenkins
You can find the Admin Password at C:\Users\USERNAME\.jenkins\secrets\initialAdminPassword
Adding a Local Repo
We can add a local repository by simply linking to it with file://C:/Users/USERNAME/source/repos/jenkins-getting-started
Setting Up a Build
I’m using the BlueOcean Plugin to build the pipellines as the visual editor is easier, the documentation on using that can be found here
When configuring the pipeline on BlueOcean, you may run into an error when running a bash script on a Windows host, use Powershell Instead
Docker Registry
Creating a local docker registry to store applications can be done by simply running the registry container, and then adding stage after the build to push the content to that registry and then run that as opposed to the “local” version of the container
Start a local registry with
We will push the content to the Docker Registry using the following command
If having issues with the above use
127.0.0.1:5000
instead oflocalhost:5000
The image can be removed from the build server with
Stop current running application and clean environment on target machine
Note that with the above you need to be sure to check if there is a container with that name currently running or else it will throw an error. This is handled using the following script in the pipeline:
The final Docker image will be run on the client with
Build Kickoff Automation
Ensure your git proxy is set up correctly with:
You need to make use of a post-commit
hook in your .git/hooks/post-commit
file with the following content, note that you need to make sure to bypass the proxy
Running a Build Slave
Ther are multiple methods for configuring a Jenkins Build Slave, usually using JNLP or SSH
You can run a slave instance for JNLP with docker using:
Or an SSH Slave with:
You will need to get your SSH key from the slave though, you can do this with:
DIY Docker SSH Build Slave
This is probably not a good idea but it works fine for testing your connections
First create the sshUbuntu.Dockerfile
so we have something to run
And then build and run that image with:
You can then log into this instance using SSH to test the connection (this is built into bash on Windows)
Then copy the entire key from the previous script and move to your Jenkins Setup
Jenkins Slave Config
Credentials
First go to your Jenkins Instance and Navigate to Jenkins > Credentials > System > Global
or the following route in your browser /credentials/store/system/domain/_/
And click Add Credential
, select the type as SSH Username with private key
and set the username as root
(or your actual user if you’re doing this for real) and Select Private Key: Enter Directly
and paste in the Key you copied from your slave container instance, if you created a Passphase for the key (the Docker one above does not) you will need to enter that in as well
Node
Then navigate to Jenkins > Manage > Manage Nodes
or /computer/
in your browser and click New Node
. Give this a name, e.g. ubuntudockerssh
, use the Permanent Agent
option. Thereafter set the Launch Method to be Launch Agents via SSH
, the host should be localhost
or whatever your actual host is, and be sure to use the Credentials we just created. Lastly the Host Key Verification Strategy should be Manually Trusted key verification strategy
and click save
Test Build
A simple Pipeline can be built using the following. Note that the agent.label
value is the name of the Node set up in the previous step
Checkout
To checkout code from a GitHub repo using the GitHub plugin, the following should work under most circumstances:
However for more complex repos which result in timeouts or other issues, it may be a better choice to use the GitSCM
method instead, something more like this:
Powershell Plugin Issues
The Powershell plugin often does not exit correctly when using it inside of a Jenkinsfile, a step may have a function like so:
Which would throw an error. Jenkins sometimes ignores errors in Powershell scripts, to exit with this correctly, the most reliable way I’ve found is to check if there a non-null $error
in the powershell script, and exit with 1
if there is: