Continuous Integration With Jenkins — PART 1
Introduction:
Jenkins is a popular open source tool to perform continuous integration and build automation. It allows us to execute a predefined list of steps, e.g. to compile Java source code, build a JAR and so on.
In this article, we will discuss how to enable Build stage in Jenkins using the classic Jenkins UI and Blue Ocean UI.
This series of articles includes:
Part 1: Setup a Jenkins Pipeline and Build the Spring Boot Source Code
Part 2: Enable SonarQube in the Pipeline for Code Review and Analysis.
Part 3: Wrap the application in a Docker image, build it in Jenkins, then push it to Docker Hub
Now, we will start with how to run Jenkins in our AWS EC2 machine as a docker container and build a Spring Boot Project.
Pre-requisites :
- AWS EC2 Machine with appropriate roles
- Docker and Docker-compose installed in EC2 Machine
- GitHub repository with Spring Boot Source Code
Jenkins as a Docker Container :
There are several Docker images of Jenkins available. Here, we use jenkinsci/blueocean to leverage the features of Blue Ocean in Jenkins Interface.
1. Jenkins Installation:
Here is the docker-compose.yaml file to install Jenkins in EC2 Machine
Step 1: Run the below command to start the Jenkins server.
docker-compose -f jenkins-dockercompose.yaml up
Once the Jenkins server is up, navigate the URL , http://{YourIP}:{YourPort} to access Jenkins and complete the initial setup.
Step 2: Get the initial Password using the below command
docker exec container_name cat /var/jenkins_home/secrets
The initial password is only for the initial setup. We need to create a new user for further Jenkins Configuration
2. Jenkins Configuration:
Now, we need to configure a few things before creating a pipeline.
Select “Manage Jenkins” and configure the highlighted elements
Step 1 : Manage Plugins
Install the required Plugins from the Available tab
- GitHub Plugin(if not installed already) as Jenkins needs to access GitHub to get the source code and click on “Install without Restart”
- Blue Ocean Plugin
Step 2 : Manage Credentials
- Select Add Credentials to add the GitHub credentials.
Make sure you choose the right kind for the credentials. For example : “Username with Password” in this case.
- Also, we need to create a “Personal Access Token” (PAT) with the required permissions and add that in Jenkins Credentials.
Please use the below link to know how to create Personal Access Token in GitHub.
- Add the generated PAT in Jenkins Credentials.
Step 3: Configure System
- Jenkins Location:
Add the URL where Jenkins is running and an email address.
In this case, the URL is the hostname of the EC2 Machine and the port where Jenkins is running
- GitHub Server
In the same page, configure the GitHub Server with Personal Access Token as credentials.
Step 4: Global Tool Configuration
The next step is to add the tool configurations that Jenkins needs to build the code.
In our case, we need Maven to build our Spring Boot Project.
We are all set with the basic configurations.
3. Pipeline using Jenkins Classic UI
We can tell Jenkins to build our code in two ways.
- With Jenkins UI
- Using a Declarative Script (Jenkinsfile)
Here, we choose the latter to communicate with Jenkins.
A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. It is written on a Domain-Specific Language based on Groovy
We have a Jenkinsfile, with single stage (Build) for now.
pipeline {
agent any
tools {
maven 'maven 3.6.3'
}
stages {
stage ("Build") {
steps {
sh "mvn -Dmaven.test.skip=true clean install"
echo 'Building..'
}
}
}
}
Step 1: Pipeline Creation
Let us create a pipeline in Jenkins and build our code using this Jenkinsfile.
Step 2 : Pipeline Configuration
Add the GitHub Repository with the appropriate credentials and branch name in which our code resides.
Step 3 : Add the Jenkinsfile
The name of the Jenkinsfile can be anything of our choice. Make sure you give the appropriate name and the Jenkinsfile is in the root directory of the GitHub repository where we have our code.
Step 4 : Select “Build Now” to start the build.
Our build is successful and we can see the logs on selecting the specific build step.
Note: Here, the build is triggered manually in Jenkins. For automatic triggers, we need to enable GitHub Webhooks.
As mentioned earlier, we have jenkinsci/blueocean image to leverage the features of Blue Ocean.
4. Pipeline using Blue Ocean UI:
Pre-requisites:
- Jenkins Classic UI (which we have done already)
- Spring Boot Source Code in Git (without Jenkinsfile)
Step 1: Open Blue Ocean from Jenkins Home
Step 2: Create a pipeline by selecting New Pipeline
Step 3: Choose the appropriate Source Code Repository
Since we use Git as our Source Code Repository here, we choose Git .
Step 4: Specify the Repository URL with corresponding GitHub Credentials and select Create Pipeline
Step 5: Create Jenkinsfile
Since we do not have a Jenkinsfile (Pipeline script) in our repository, Blue Ocean UI helps us to create one using the Pipeline Editor.
Step 5a: Name our Stage (Build) and add the required steps
Step 5b: Choose Shell Script and add the Maven command to build our code
Step 5c: We will add another step Print Message to print some string/message.
Step 5d: Add Tools section in the Pipeline script
Bring up the pipeline editor by pressing ctrl+s , add the tools section and Update
Note : Make sure the tool name is same as that we configured in Global Tool Configuration
Step 6: Save the Pipeline
Note: We have only one stage “Build” and two steps inside the stage
Step 7: Commit the Jenkinsfile to Master branch and select Save & run, which starts the pipeline execution automatically.
Once we save this, the created Jenkinsfile gets pushed to our repository and the Pipeline execution starts
And, that is a success.
Conclusion:
Our Spring Project is successfully built with Jenkins Classic and Blue Ocean UI. I hope this article is of great use to build code with Jenkins using declarative script. As we have .jar now, in the next article we will see how to analyze our code and do dome quality checks using SonarQube as our code review tool.