I hope you have enjoyed following along with my tutorials on Jenkins and the CI/CD pipeline so far. In this article you will learn how to implement the "CI" in CI/CD.
Prep Work
If you haven't already, you'll want to follow my installation and setup guides for Jenkins. You'll definitely want to knock those two tutorials out first so I don't lose you.
We are going to create a job in Jenkins that will checkout the project, execute the test cases, then build and test the project. First we need to do some setup.
Git
SSH into your Jenkins install (if you aren't already). If you don't remember how, it's outlined in this step. We will be bouncing back and forth between our terminal and the Jenkins Dashboard a lot for this section.
We need to verify that git is installed on your server. You can do this by running git --version
. If it's not installed already (It'll say 'git command not found'), then you will need to install it using sudo apt-get update && sudo apt-get install git
.
Installing Maven
To run this project, we need to ensure Maven is installed. Back in your terminal, run mvn --version
. You will likely see "Command 'mvn' not found".
If maven isn't installed you'll need to run sudo apt install maven
then y
and ⏎
. To be sure it installed run mvn --version
again.
Head back to your Jenkins Dashboard and navigate back to the Manage Jenkins page. From there select "Global Tool Configuration". In the JDK section we will want to turn name it "LocalJDK" and make sure to select "Install automatically" (it's selected by default). Check the "I agree to the Java SE Development Kit License Agreement"
Now we have to do the same thing for Maven. On the Jenkins Dashboard, select "Add Maven". Provide it a name: LocalMaven
. Unselect "Install automatically". In the MAVEN_HOME
field provide it: /usr/share/maven
.
At the bottom of the Jenkins Dashboard, select "Save".
Jenkins Plugins
We will need a Jenkins plugin. Log into your Jenkins Dashboard and select "Manage Jenkins".
There you will see a tab called “Installed” - verify Github plugin is installed. It should already be.
Manual Github & Git Integration
Connecting Github to your Jenkins Install
Head over to Github and fork this Maven Sample project: https://github.com/drewlearns/simple-java-maven-app.
Clone that forked project to your local computer. If you aren't familiar with git, navigate to your development directory and just run git clone <INSERT_URL_OF_GITREPO>
.
We need to create a job that will checkout the Github Repository. Copy the Github URL for the forked repository. Be sure to paste the https version instead of the ssh version of the GitHub repo.
On the Jenkins Dashboard, create a "New Item" - if you aren't sure how to do this, check out this section of my last article for a refresher. Give it a name "Example Maven Project" and select the "Freestyle project" option. Then click "OK".
In the "Source Code Management" section of your New Item, paste the GitHub URL for the forked project. In the “Branches to build” field type "*/master
".
In the Build Environment section of the new Item, select Delete workspace before build starts
.
Configuring the Maven "stuff"
Now we need to create a command to build the project. Scroll down to the Build section and click "Add Build Step" drop down. Select "Invoke top-level Maven Targets".
Select the Maven Version we named "LocalMaven" In the setup section above.
We will provide two goals: test install
. If you'd like to dive into what these mean, head over to https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html for a detailed explanation on it.
Select the "Advanced" option. We need to define the path of the pom.xml
file. We can look at the repo image above for more details on that.
Jenkins_Maven_Sample/maven-samples/single-module/pom.xml
is what it should be. Now add that to your build information.
Now click "Save" at the bottom of the page.
Build
On the Jenkin's Dashboard menu for the Maven Project, click "Build now". You should see it pop up in the build history. Click on the build link and then open the "Console Output". If you scroll allllllllllll the way down, the final line reads "Finished: SUCCESS
."
If you read that console log, you'll see that it checks out the project from Github, executes the build and creates a .jar file on your server.
Thats neat and all but what if I push a new change to the repository? I want it to build automatically. No worries, we are about to learn about that!
Automating builds with Git Source Code Polling
To understand the next section, we have to understand Cron jobs
Here is where we drive home the Continuous Integration part of the CI/CD. We will be automating builds when we commit code to run tests.
Head back to the Jenkins Dashboard and Select the "Maven Project" and then select "Configure" from the Menu. Scroll down to "Build Triggers".
Select "Poll SCM".
Here we will define a schedule to poll the repository. We will use Cron.
Cron is a time-based job scheduler in Unix-like computer operating systems. Each line has 5 fields separated by a Tab or space.
Here is an in-depth article from linux if you are really interested to dig deeper: https://www.linux.com/news/beginners-guide-cron-jobs-0/
Schedule
In the text field of Schedule section called "Poll SCM", write the following: * * * * *
.
This will poll the GitHub repo every minute.
Open up the repository on your local machine in your favorite editor. Navigate to Jenkins_Upgradev3/maven-samples/single-module/src/test/java/com/example/TestGreeter.java
file. Copy lines 20-25. and paste them just below 25.
Pro tip, on Mac with Vs Code you can highlight the lines and press ⌥
+shift
and press ⬇️
and it will copy the lines.
In the second test, change the String text to something interesting.
Save all files.
When you are done saving run git add .
and then git commit -m "Changed the 2nd test"
then git push origin master
to push to the GitHub repo. It will take up to 60 seconds for it to poll the repo and start running.
We know this one was automatically triggered as well by looking at the details of the job. The Job was "Started by an SCM change" instead of your Jenkins User.
Next...
In my next article about CI/CD, we will take a deep dive into the Continuous delivery side of Jenkins.
If you found this article helpful, give me a shout on twitter – I’d love to hear from you. @codingwithdrewk or connect with me on linkedin. As always, if you found any errors, just highlight it and mash that “R” button on the right side of the screen and I’ll get those fixed right up!
Featured image credit: Created by Marco Davalos
Platform Engineering Support Team
SVG format
Drew is a seasoned DevOps Engineer with a rich background that spans multiple industries and technologies. With foundational training as a Nuclear Engineer in the US Navy, Drew brings a meticulous approach to operational efficiency and reliability. His expertise lies in cloud migration strategies, CI/CD automation, and Kubernetes orchestration. Known for a keen focus on facts and correctness, Drew is proficient in a range of programming languages including Bash and JavaScript. His diverse experiences, from serving in the military to working in the corporate world, have equipped him with a comprehensive worldview and a knack for creative problem-solving. Drew advocates for streamlined, fact-based approaches in both code and business, making him a reliable authority in the tech industry.