Jenkins Continuous Integration Server
Install Jenkins
-
Create a new folder named
jenkins-lab
in CLASSWORK/w08 and copy thisdocker-compose.yml
:version: "3.7" services: jenkins: image: jenkins/jenkins:lts privileged: true # gives container capabilities of its host machine user: root ports: - "8083:8080" - "50003:50000" # less important: port that allows inbound agents to connect to Jenkins container_name: jenkins-lab volumes: - ~/jenkins_data:/var/jenkins_home # persist Jenkins configuration files to `~/jenkins_data` - /var/run/docker.sock:/var/run/docker.sock
As you see I call the container
jenkins-lab
. I will use that name in all further code snippets but feel free to choose whichever name you want. -
Run Jenkins via
docker-compose up -d
(If you want you can look into the container via
docker exec -it jenkins-lab bash
) - Open
http://localhost:8083
in your browser (you might have to wait a bit until docker is done) You’ll notice that I am using port8083
instead of the default8080
. The reason, other than demonstrating that you can use other ports, is that port8080
is used by some frameworks like Spring Boot.
If you run docker ps
, you should see your docker container running
-
Go to Docker Desktop to inspect the logs of the running Jenkins container and find the line “Please use the following password to proceed to installation” and copy the password.
(Alternatively you can run
docker exec jenkins-lab cat /var/jenkins_home/secrets/initialAdminPassword
to get the password) -
Set up Jenkins:
- Choose “Install suggested plugins”
- Create an admin user
- Confirm the Jenkins URL (
http://localhost:8083
) and hit “Save and Finish”
Some images of the Jenkins setup process:
Wait until the installation is complete and then you can proceed in creating your first admin user.
After creating the admin user, setup the Instance configuration. Since you are only using Jenkins locally, leave the URL to your localhost URL. Click on Save and Finish to start using Jenkins.
Blue Ocean Plugin
Install blueocean plugin to beautify Jenkins
- In home of Jenkins click on Manage Jenkins
- Click on Manage Plugins
- In the Plugin Manager in the Available tab, search
blue ocean
. - Tick the box next to the plugin that’s simply called ‘Blue Ocean’ and at the bottom click “Install without restart”
- When done, click on “Go back to the top page”
Slack Plugin
Let Jenkins post to Slack!
-
Slack: Install Jenkins plugin
You have to be a Slack admin in order to install the Jenkins addon. Do so and copy the credential.
- Jenkins: Install the Slack Notification plugin in a similar manner as the blueocean plugin.
- After it’s installed, click on Manage Jenkins again in the left navigation, and then go to Configure System. Find the Slack section and add the following values:
- Workspace:
name of your workspace
(usually all lowercase) - Credential: Click on Add > Jenkins, then add a secret text credential using the value from step 2 (e.g.
abcdefg12345678
) as the value (pick any ID & Description) - Channel:
jenkins-updates
- DON’T TICK “Custom slack app bot user”!
- Test the connection (if successful a message should appear in the
jenkins-updates
Slack channel) and press Save when you’re done.
Useful: You can click on the question mark icons next to them for more information.
Create a Pipeline
A pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.
A continuous delivery (CD) pipeline is an automated expression of your process for getting software from version control to your users and customers. Every change to your software (committed in source control) goes through a complex process on its way to being released. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a build) through multiple stages of testing and deployment.
Your Task:
- Go back to the Jenkins home page (http://localhost:8083). Scroll down to the end and click
Create new jobs
underWelcome to Jenkins!
. - Specify a name for your pipeline (e.g.,
hello-world
). - Scroll down and click
Pipeline
, then clickOK
at the end of the page. - Scroll down to the
Pipeline
section. You can also click on thePipeline
tab. - As
Pipeline script
pick the “GitHub plus Maven** template and save. - Run (aka “Build now”) your first pipeline!
- This build will probably fail. —> To fix it go to Manage Jenkins > Global Tool Configuration and under Maven installations add Maven with Name
M3
which installs version3.8.2
automatically from Apache. - Hit
Save
to apply the new Maven installation - Go back to the pipeline and hit “Build now” again to re-run it.
- Now re-run it in Blue Ocean: Go back to the Jenkins homepage, open the Blue Ocean interface and Click Run at the top left. Then, quickly click the OPEN link which appears briefly at the lower-right to see the pipeline run details.
Jenkins + Slack
Create a Jenkins freestyle project
- Make sure your backend project is in an independent git repository, i.e. not inside the
HOMEWORK
repo (which itself is a git repo already) - In “Manage Jenkins” > “Configure System” set your GA GitHub enterprise username and email in the git section (not essential).
- Create a new Jenkins “Freestyle project”
- Under “Source Code Management” add your GA GitHub enterprise credentials (use your email address NOT your GitHub username) and the url of the repository you want to use.
- Adjust the branch to build, probably
main
- As Build Triggers tick
Poll SCM
and set the schedule to poll every 2 min. Use the?
help from Jenkins. - As a build step add “execute shell” and in there add the line
echo "Hi there"
and another line where you add an environment variable, e.g.GIT_COMMIT
e.g. asecho "Name of commit: ${GIT_COMMIT}
.
Sending Slack notifications
- As Post build action choose “Slack Notification” and tick all boxes.
- Hit Save and run the job manually once! Do you see a Slack message?
- Now go to your repository and create a file README.md in the root folder.
- Write something into the file and commit. —> Do you see more Slack messages coming in after
-
Go to Configure (inside your job) to make changes to your project:
- Play around with the “advanced” Slack settings.
- Add some more lines of
echo
messages to your shell script.
Jenkins build automation with a JAVA SpringBoot Application
What?
We now build a pipeline which will …
- listen to changes in a GitHub Enterprise repository in 1min intervals
- Run integration tests.
- Send out regular Slack messages
Setup
- Make sure your backend (i.e. your Spring Boot App) is in a single git repository - ideally as a folder inside your
development
directory. - Add Java 17 JVM in Jenkins config (we’ll do this together).
- Go to
Manage Jenkins
>Global Tool Configuration
>JDK
- Click on
Add JDK
- As Name enter
jdk-17.0.1
- Click on
Delete Installer
for the section that automatically appears - Click
Add Installer
then chooseExtract *.zip/*.tar.gz
- As label choose
master
- Use Download URL paste in
https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
- As subdirectory choose
jdk-17.0.1
- Click on
Save
- We change the database for our Springboot integration tests
Go to file application-it.properties
and replace the contents with the following
# URL for my H2 Database
spring.datasource.url=jdbc:h2:mem:stationtestdb
# Database driver - we're using H2
spring.datasource.driverClassName=org.h2.Driver
# Dialect of SQL
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
This will change the database for tests from Postgres to h2.
- Push changes to your backend repository.
Your task in Jenkins
- Create a “New Item” > “Pipeline”
- Tick
Poll SCM
and have it poll events every minute. -
Use the following as the pipeline script as starting point:
Be sure to replace the repository
credentialsId
andurl
with your own.pipeline { agent { label "master" } tools { jdk "jdk-17.0.1" } stages { stage('Pre') { steps { slackSend channel: "#jenkins-updates", message: "Job ${env.JOB_NAME} started (Build number: ${env.BUILD_NUMBER})" } } stage('Test') { steps { git branch: 'main', credentialsId: 'abcdefg-abcd-42ab-8b46-e46abc1234c1', url: 'https://git.my-company.ly/my-organisation/my-repository' sh 'chmod +x mvnw' echo '--------- TESTING domain 1 ---------' sh './mvnw test -Dtest="Domain1IT"' echo '--------- TESTING domain 2 ---------' sh './mvnw test -Dtest="Domain2IT"' } post { // If Maven was able to run the tests, even if some of the test // failed, record the test results. success { junit '**/target/surefire-reports/TEST-*.xml' } } } } }
- Click on
Pipeline Syntax
underneath the pipeline script code block and a new window will open - In
Sample Step
choosegit:Git
- Paste in the repository URL from step 3, e.g.
https://git.my-company.ly/my-organisation/my-repository
- Click on the dropdown menu in
Credentials
that says- none -
and choose your credentials (you should have generated these earlier) - Click on
Generate Pipeline Script
- Change the line
git branch: 'main', credentialsId: 'abcdefg-abcd-42ab-8b46-e46abc1234c1', url: 'https://git.my-company.ly/my-organisation/my-repository'
with what you create via the pipeline snippet generator. - Change the following tests
Domain1IT
andDomain2IT
to reference your tests instead. - Test whether the job automatically starts by commiting to the main/master branch of your backend project.
Improving the pipeline
- Send out three more Slack message commands at suitable places into the pipeline script.
- Improve the Slack messages! Add some more environment variables (e.g.
${env.GIT_BRANCH}
). Go here to find all available environment variables: http://localhost:8083/env-vars.html/ - Add some more
echo
commands to help you find things faster in the Logs.
General
Jenkins Projects are saved in ~/.jenkins/workspace/
on your Continuous Integration server.
Tips
-
Start Shell scripts with the
-l
tag, i.e.#!/bin/bash -l
e.g. here it’s only executing a script which is in version control.
#!/bin/bash -l ./internals/scripts/build-cms.sh
Discuss on Twitter ● Improve this article: Edit on GitHub