Docker – Continuous Integration
In this post, we will see how to make Continuous Integration using Jenkins. In the previous post, we have seen how to install docker and the basic terminology and how to create an application using docker. With the help of Continuous Integration we can “Build”, “Test”, and “Package” our application.
For this, we need to make a Continuous Integration Pipeline. You can practice the below implementation with any preferred language of your application. For this post, we will be making a continuous integration pipeline for a java project. We have used the following project.
Continuous Integration Pipeline:
Setup of Jenkins:
Follow the below steps to setup Jenkins:
Step 1: Initial Images using the below command:
Step 2: Use the below command to pull the Jenkins Image:
docker pull jenkins/jenkins
Step 3: Use the below command to run the Jenkins image in a container made at port 1024 in localhost (make sure that the container is running in docker)
docker container run -it -u root -p 1024:8080 jenkins/jenkins:latest /bin/bash
Here the container is running at port 1024 in our local system and 8080 is running in the container.
Step 4: Use the below command to update the container image:
Step 5: Now use the below command to install the net-tools:
apt install net-tools
Step 6: As it is a Java-based project installing “Maven” inside the container. (If it was a Nodejs based project we have to install node, git)
apt-get install maven
Step 7: Install zip to get the bundled file during the package phase. Below is the command for the same
apt-get install zip
Step 8: Now run the Jenkins container at port 1024 using the below command:
Creating Custom user-id Details:
Follow the below steps to create custom users:
- Copy the password given in the terminal and enter it into the Jenkins running at localhost:1024
- Install all the required plugins for the project
- After that configure the global security so that you can add custom users to Jenkins.
- Jenkins is not ready for the first build job.
Install the recommended plugins:
Creating first user-id (this step can be avoided and the user can continue with the admin login done before):
Instance configuration shows the port where it’s working on the browser:
Jenkins Setup Completed:
- Click on start using Jenkins
Step 8: Now go to the Manage Jenkins section and click on “Configure Global Security” so as to enable other users to sign up. So, now we can add new users to this Jenkins.
Step 9: Installing the required plugins required for the java project under the “Manage plugin section” that is “Pipeline Maven”, “AdoptopenJDK” and “Maven Integration”
Now, whenever there is a change/editing in the source code there will always be a new version of the commit. For each commit we need to trigger a build and build will trigger test and test will trigger package. So, we need to create a pipeline between Build-Test-Package
First making independent files for build, test, and package then we will integrate all three together thus creating a pipeline.
Make first build job by clicking on the “New Item” option in the sidebar and giving the name of the item and selecting “Maven Project” as a Java project.
Under source code management enter the git repository link with “.git” as an extension as shown below:
Goals and options(build): clean install
After building we get the output in the console
Same steps to be followed as done in build phase only difference is that under goals and option we write the command “test”
After configuration builds the test phase.
Same steps to be followed as done in build phase only difference is that under goals and option we write the command “package” and
Under post-build actions choose: “File to archive”—>**/target/*.jar (write this where the bundled file will be stored in this location)
After configuration build the package phase.
Under post-build action choose “Files to archive” then write the location where the package file will be stored
Integration of “Build-Test-Package”:
For now, all these 3 files are independent we need to integrate them.
1. Integrating “build” file to “test” file
Save it after making the above changes.
2. Integrating “test” file to “package” file
Save it after making the above changes.
After integrating whenever there is a change/edit in the project it will trigger the build phase. The build phase will trigger the test phase and the test phase will trigger the package phase.
Hence Continuous Integration Pipeline is made.