Project - CICD Using Jenkins and Docker
Project - CICD Using Jenkins and Docker
Project - CICD Using Jenkins and Docker
This Project will help you to setup a CI/CD pipeline using Jenkins and Docker. It
includes automation using Jenkins Pipeline/Groovy scripting language, it uses sonar for
code quality and artifactory for artifactory management.
Tools:
Jenkins- CI/CD
Docker — Container
Ansible/Github/Docker/Cucumber/
Welcome to simplilearn .
In this project we will explore one of the most popular CI/CD tool Jenkins. We will try to
cover each aspect of Jenkins with a demo.
What is Jenkins?
Components of Jenkins:
• Master Node: The system where you install and run Jenkins.
• Managed Node: Target systems managed by Jenkins.
• Repository: Version controlled system where you keep your code.
• UI: User interface to manage and configure Jenkins.
• Pipeline: Means of continuous deployment of your code to target systems from the
version control system.
• Plugins: Means of enhancing the functionality of a Jenkins.
Next, we are going to learn how to install Jenkins.
Jenkins Installation:
Prerequisite:
1 #####################################
2 ## Jenkins installation on AWS EC2 ##
3 #####################################
4 ## Create an EC2 instance with Amazon Linux 2 AMI
5 ## Amazon Linux 2 AMI (HVM), SSD Volume Type
6 ## Connect to your EC2 instance
7
8 ## Update all packages
9 sudo yum update -y
10
11 ## Install Java
12 sudo yum install java-1.8.0-openjdk-devel
There are several ways you can install Jenkins in your master node (EC2 instance in
our case).
• Using Docker
• Using Yum
• Using a War file
I would prefer the 1st method, using docker which comes with blue ocean plugin
preinstalled and this will be used in rest of the Jenkins blog series.
Using docker:
1 #############################################################
2 ## Jenkins installation on AWS EC2 using docker blue ocean ##
3 #############################################################
4
5 ## Install Docker.
6 sudo yum install docker
7
8 ## Add the ec2-user to the docker group so you can execute Docker commands without using sudo.
9 ## Exit the terminal and re-login to make the change effective
10 sudo usermod -a -G docker ec2-user
11 exit
12
13 ## Enable docker service
14 sudo systemctl enable docker
15
16 ## Start docker service
17 sudo systemctl start docker
18
19 ## Check the Docker service.
20 sudo systemctl status docker
21
22 ## Run docker jenkins blueocean container
23 docker run \
24 -u root \
25 --rm \
26 -d \
27 -p 8080:8080 \
28 -p 50000:50000 \
29 --name myjenkin \
30 -v jenkins-data:/var/jenkins_home \
31 -v /var/run/docker.sock:/var/run/docker.sock \
32 jenkinsci/blueocean
33
34 ## get the administrator password
35 docker exec -it myjenkin bash
36 cat /var/jenkins_home/secrets/initialAdminPassword
37 exit
38
39 #or from docker logs
40
41 docker logs myjenkin
Using Yum:
1 ###############################################
2 ## Jenkins installation on AWS EC2 using YUM ##
3 ###############################################
4
5 ## Add Jenkins repo to your yum repository
6 sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
7
8 ## Import a key file from Jenkins-CI to enable installation from the package
9 sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
10
11 ## Install Jenkins
12 sudo yum install jenkins -y
13
14 ## Start and enable Jenkins service
15 sudo systemctl start jenkins
16 sudo systemctl enable jenkins
17 sudo systemctl status jenkins
18
19 ## Get the initial administrative password
20 sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Using a WAR file:
1 ###################################################
2 ## Jenkins installation on AWS EC2 using war file##
3 ###################################################
4
5 ## Get the latest jenkins war file
6 wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
7
8 ## Install jenkins using war file
9 java -jar jenkins.war
10
11 ## Get the initial administartive password from the output of the above command
12 # Jenkins initial setup is required. An admin user has been created and a password generated.
13 # Please use the following password to proceed to installation:
14 # 2871112a4e704e3bbc9c2e0b2963c8c0
15 # This may also be found at: /home/ec2-user/.jenkins/secrets/initialAdminPassword
Jenkins Configuration:
Step 1: Open your EC2 instance public DNS or public IP
(http://<PUBLIC_DNS/PUBLIC_IP>:8080/) along with port 8080 in your
favorite browser. And provide the administrative password obtained during
the installation.
Install JAVA 8
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export PATH
sudo alternatives --config java
Install Jenkins
sudo yum update
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import http://pkg.jenkins-ci.org/redhat-stable/jenkins-ci.org.key
sudo yum install jenkins
sudo service jenkins start
http://localhost:8080/
It will ask for the initial password, please run the below command
cat /var/lib/jenkins/secrets/initialAdminPassword
Add JDK -> JAVA_HOME -> put the java_home path of the machine
In Build Section,select maven version and goal which you want to execute as shown
below. This is very important, you have to select invoke Artifactory maven 3 only not the
invoke maven top level targets.
Install JAVA 8
Click on New:
Provide user details and Save:
Edit the permissions and assign the user: If user is already admin
then it has all permissions
Click on Save & Finish:
Check the Permissions Management section in Artifactory for
recent changes:
4. Click on Refresh Repositories and select the repository in the release and snapshot
field from the lists:
1. Save and click on Build now and verify logs in the Console Output. Jar files are
resolved from the local repository or Artifactory:
2. Once the package is created, it is stored in Artifactory too:
Sonarqube Setup
1. Click on Login and give the default username and password as admin and
default to log in as an administrator.
2. Click on Login:
1. As of now, there is no project available in the SonarQube dashboard.
2. Click on the Administration tab and in the Security menu click on Users:
1. Now we have all the required parameters to integrate Jenkins and SonarQube:
2. Go to the Jenkins dashboard and click on Manage Jenkins.
3. Click on Configure system and find the SonarQube section.
4. Click on Add SonarQube.
5. Provide the Name, Server URL, and Server version.
6. Paste the token value in Jenkins and save it:
1. Go to Global Tool Configuration and configure Add SonarQube Scanner:
1. Now, you are ready for the static code analysis of the project.
2. Go to the Build section and select Execute SonarQube Scanner:
1. You can provide the location of sonar-project.properties or provide details directly
for static code analysis.
# Required metadata
sonar.projectKey=SonarHTMLCSSJS
sonar.projectName=Simple HTML CSS JS project analyzed with the SonarQube
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.sources=.
# Encoding of the source files
sonar.sourceEncoding=UTF-8
sonar.java.binaries=.
1. sonar.sources is the main property for static code analysis. With this property,
you inform SonarQube which directory needs to be analyzed:
1. Click on Save.
2. Go to Jenkins Project and click on Build now.
3. Go to Console output to check the logs.
For Gradle, any .sh file should have the below permission else while executing
permission denied error comes
Create a Pipeline project in Jenkins and put the below code in the pipeline script and
trigger the build
pipeline {
agent any
stages {
stage(“Checkout”) {
steps {
git url: ‘https://github.com/dhutifile/calculator.git’
}
}
stage(“Compile”) {
steps {
sh “./gradlew compileJava”
}
}
stage(“Unit test”) {
steps {
sh “./gradlew test”
}
}
}
}
After build is successful, you could see the below output view
./gradlew bootRun
Now we will see how to create the Jenkinsfile and commit it with the source code into
the git repository.
Jenkinsfile
Let’s create a file called Jenkinsfile in the root directory of our project
pipeline {
agent any
stages {
stage(“Compile”) {
steps {
sh “./gradlew compileJava”
}
}
stage(“Unit test”) {
steps {
sh “./gradlew test”
}
}
}
}
$ git add .
$ git commit -m "Add sum Jenkinsfile"
$ git push
Trigger Build.
Code Coverage
Code coverage is a tool that runs all tests and verifies which parts of the code have
been executed. Then, it creates a report showing not-tested sections. Moreover, we can
make the build fail when there is too much untested code.
JACOCO
Adding a Dockerfile and commit in Git and and add docker build/push to the jenkins
pipeline(Jenkinsfile)
In the root directory of the project, let’s create the acceptance_test.sh file:
#!/bin/bash
test $(curl localhost:8765/sum?a=1\&b=2) -eq 3
Jenkinsfile
pipeline {
agent any
stages {
stage("Compile") {
steps {
sh "./gradlew compileJava"
}
}
stage("Unit test") {
steps {
sh "./gradlew test"
}
}
stage("Package") {
steps {
sh "./gradlew build"
}
}
stage("Docker build") {
steps {
Let’s start with an example and imagine that our calculator project uses the Redis
server for caching. In this case, we need an environment with two containers, calculator
and redis. let’s create the docker-compose.yml file at the same location.
version: "3"
services:
calculator:
image: calculator:latest
ports:
- 8080
redis:
image: redis:latest
References