An end-to-end tutorial to Continuous Integration and Continuous Delivery by Dockerize Jenkins Pipeline

Hüseyin Akdoğan
Nov 26, 2017 · 6 min read

Continuous Integration and Continuous Delivery highly fundamental topics in the software industry especially with the cloud and container technologies. Container technologies such as Docker, Kubernetes, OpenShift and automation servers like Jenkins make easier to manage our projects workflow.

The repository I have created, answers the question of how to automatically manage the process of building, testing with the highest coverage, and deploying phases.

Our goal is to ensure our pipeline works well after each code being pushed. The processes we want to auto-manage:

  • Code checkout
  • Run tests
  • Compile the code
  • Run Sonarqube analysis on the code
  • Create Docker image
  • Push the image to Docker Hub
  • Pull and run the image

First step, running up the services

Since one of the goals is to obtain the sonarqube report of our project, we should be able to access sonarqube from the jenkins service. Docker compose is a best choice to run services working together. We configure our application services in a yaml file as below.

Paths of docker files of the containers are specified at context attribute in the docker-compose file. Content of these files as follows.

FROM sonarqube:6.7-alpine #sonarqube/Dockerfile

If we run the following command in the same directory as the docker-compose.yml file, the Sonarqube and Jenkins containers will up and run.

GitHub configuration

We’ll define a service on Github to call the Jenkins Github webhook because we want to trigger the pipeline. To do this go to Settings -> Integrations & services. The Jenkins Github plugin should be shown on the list of available services as below.

After this, we should add a new service by typing the URL of the dockerized Jenkins container along with the /github-webhook/ path.

The next step is that create an SSH key for a Jenkins user and define it as Deploy keys on our GitHub repository.

If everything goes well, the following connection request should return with a success.

Jenkins configuration

We have configured Jenkins in the docker compose file to run on port 8080 therefore if we visit http://localhost:8080 we will be greeted with a screen like this.

We need the admin password to proceed to installation. It’s stored in the /var/jenkins_home/secrets/initialAdminPassword directory and also It’s written as output on the console when Jenkins starts.

To access the password from the container.

After entering the password, we will download recommended plugins and define anadmin user.

After clicking Save and Finish and Start using Jenkins buttons, we should be seeing the Jenkins homepage. One of the seven goals listed above is that we must have the ability to build an image in the Jenkins being dockerized. Take a look at the volume definitions of the Jenkins service in the compose file.

The purpose is to communicate between the Docker Daemon and the Docker Client(we will install it on Jenkins) over the socket. Like the docker client, we also need Maven to compile the application. For the installation of these tools, we need to perform the Maven and Docker Client configurations under Manage Jenkins -> Global Tool Configuration menu.

We have added the Maven and Docker installers and have checked theInstall automatically checkbox. These tools are installed by Jenkins when our script(Jenkins file) first runs. We give myMaven and myDocker names to the tools. We will access these tools with this names in the script file.

Since we will perform some operations such as checkout codebase and pushing an image to Docker Hub, we need to define the Docker Hub Credentials. Keep in mind that if we are using a private repo, we must define Github credentials. These definitions are performed under Jenkins Home Page -> Credentials -> Global credentials (unrestricted) -> Add Credentials menu.

We use the value we entered in the ID field to Docker Login in the script file. Now, we define pipeline under Jenkins Home Page -> New Item menu.

In this step, we select GitHub hook trigger for GITScm pooling options for automatic run of the pipeline by Github hookcall.

Also in the Pipeline section, we select the Pipeline script from SCM as Definition, define the GitHub repository and the branch name, and specify the script location (Jenkins file).

After that, when a push is done to the remote repository or when you manually trigger the pipeline by Build Now option, the steps described in Jenkins file will be executed.

Review important points of the Jenkins file

The Maven and Docker client tools we have defined in Jenkins under Global Tool Configuration menu are added to the PATH environment variable for using these tools with sh command.

withCredentials provided by Jenkins Credentials Binding Plugin and bind credentials to variables. We passed dockerHubAccount value with credentialsId parameter. Remember that, dockerHubAccount value is Docker Hub credentials ID we have defined it under Jenkins Home Page -> Credentials -> Global credentials (unrestricted) -> Add Credentials menu. In this way, we access to the username and password information of the account for login.

Sonarqube configuration

For Sonarqube we have made the following definitions in the pom.xml file of the project.

In the docker compose file, we gave the name of the Sonarqube service which is sonarqube, this is why in the pom.xml file, the sonar URL was defined as http://sonarqube:9000.

In this article, I have tried to share an end-to-end tutorial, I hope this could help your needs.

Hüseyin Akdoğan

Written by

Expert Software Consultant at Kodcu.Com