Using a Multibranch Jenkins Pipeline with Docker
Jenkins is an open source automation server. It has a general-purpose pipeline that may be used to run a build script integrating various phases of code integration and build. A Jenkins pipeline can be used for continuous integration of Docker image code.
A Docker image build could be based on a single GitHub branch. Typically, code for a Docker image is developed by multiple team members making use of multiple GitHub branches, with each branch being the code for a single Docker image tag. However, having to create and run a Jenkins pipeline for each GitHub branch can be cumbersome.
Luckily, Jenkins provides a multibranch pipeline to integrate multiple GitHub branches simultaneously. Each GitHub branch that includes a Jenkinsfile starts a separate Jenkins pipeline, even through a user-initiated single multibranch pipeline. With a single build of the Jenkins multibranch pipeline, multiple GitHub branches get built at once.
Creating Multiple Branches on a GitHub Repository
Using a Node server GitHub repository for the Docker image code, create multiple branches from the master branch.
Click on the master branch and create a new branch, 1.0.
Similarly create branches 1.1, 1.2, and 2.0. Including the master branch, this creates a total of four branches.
The Docker image tag is different in each branch. In the master branch, the tag is “Latest.” In branch 1.0, the tag is 1.0, and so on. The code files are the same in each branch. For example, the 2.0 branch has the same files as the master and the other branches, which are Dockerfile, server.js, and Jenkinsfile. The content of the files could be different, as these are different builds.
Creating and Running a Jenkins Pipeline
Next, create a multibranch Jenkins project with branch sources as Git.
Create a new Jenkins pipeline. Then, click on the pipeline, and five subpipelines should be displayed, one for each GitHub branch.
Build the Jenkins project. The Jenkins project will be run after the configured schedule is due, such as every hour. Starting the build on the master branch starts the build on all branches, as shown below. All the branch pipelines in the Jenkins multibranch project should run to completion before the master branch is completed because the branches are within the master branch.
The master branch should also run to completion. The Docker image tags get uploaded to Docker Hub, one for each branch pipeline that is run.
By using this Jenkins multibranch pipeline method, you don’t have to create and run a separate Jenkins pipeline for each GitHub branch you have. Being able to integrate multiple GitHub branches simultaneously saves you time and keeps everything more organized.