Scalable Jenkins on Kubernetes Cluster and Pipeline for Automated CI/CD (Jenkins—Github Integration)
This article demonstrates how to set up a scalable Jenkins on Kubernetes and how to integrate Jenkins into GitHub so developers can review pull requests.
Join the DZone community and get the full member experience.Join For Free
Setting up scalable Jenkins on Kubernetes can be a daunting task. This article demonstrates not only how to set up scalable Jenkins on Kubernetes but also how to integrate Jenkins into Github so that whenever any Pull Request is requested on a branch, the build is automatically triggered and results are made available to the branch admin to review before merging the Pull Request.
Let us first get the scalable Jenkins up before setting up a pipeline for automated CI/CD.
Before we go through the steps, here is the brief on the Master and Slave node docker images.
Jenkins Master Node
Jenkins master uses the image
This image has many plugins required for smooth CD on Kubernetes. To know the image content, Dockerfile (./master/Dockerfile) is kept here. The following plugins are made available as part of this image to save time:
Jenkins Slave Node
Jenkins slave uses the image
This image is based on openjdk11 containing Maven, Docker runtime so that the Springboot project can be checked out, and is Maven built including Docker image building & deployment.
Steps—Scalable Jenkins on Kubernetes Cluster
Below is the step-by-step guide to making it happen.
Launch Jenkins Master
Run the below Kubernetes configurations:
Secure the Master
- Once the Jenkins master server is deployed, it will take few minutes to get the UI up and running. To access the UI, run the following command:
kubectl port-forward deploy/jenkins 8080:8080
- Launch the URL http://localhost:8080 on the browser.
- Go to http://localhost:8080/configureSecurity/ and secure the server by enabling security as shown in the below picture.
- Once you save it, you will get an option to set a username and password.
- After login, come back to this URL again and select tickmark to Agent—Controller Security. This option gets visible after setting up a username and password.
Configure the Kubernetes Plugin as Shown in the Snapshots Below
Now go to the URL: http://localhost:8080/configureClouds and key in the configuration as shown in the below snapshots.
Set Up Global Credentials
To test the docker commands especially login, first set up the global credentials as shown below.
Create Build Job
Create a docker-test job as a pipeline, click OK, and insert the below text in the pipeline block:
Test the Pipeline
Run the docker-test job and you'll see that a pod will be launched by Jenkins master to run the docker-test build job; the pod will be terminated immediately once the build completes.
Once the build job completes, the build pod is terminated.
Steps—Jenkins Pipeline for Automated CI/CD
To illustrate automated CI/CD, I have set up an example GitHub project Weather Service. I have also created a branch named Jenkins and whenever any contributions are made to this branch via pull request, the branch/project admin can review the pull request, view the build, and test the status before merging into the Jenkins branch
On Jenkins, set up a build job named weather-service and type pipeline, click OK, and then scroll down to insert the content of this pipeline file into the pipeline section.
Also, install the below plugin.
- GitHub pull request builder
Now generate an access token using https://github.com/settings/tokens/new (select repo:status)
Create a webhook using your Github repo settings
- Event trigger—Select individual events (Pull requests, Issue comments) so that whenever any pull request is created, Github can notify your Jenkins URL
Create Jenkins Credentials (type: Secret text) using the above access token
Configure the GitHub Server here http://localhost:8080/configure.
Scroll down and configure the GitHub Pull Request Builder and choose the right credentials.
Create a new job that says weather-service, select pipeline, and click OK. Select the GitHub Pull Request Builder; see the below snapshot.
Click on advanced and insert the whitelist target branch that says Jenkins. You should also select Build every pull request automatically without asking to ensure that every pull request is good enough in terms of build and test quality before merging.
Test the Setup
Now, you can raise a pull request on the selected branch (Jenkins in my case); notice that the build gets triggered automatically and its results will be shown on the Pull request page so that the branch admin can review the outcome and decide on the merge.
To show the build status on your main Github page, install the plugin
embeddable-build-status and go to your build job. Click on this plugin link to generate a URL that will look similar to the one below:
Kubernetes Cluster Info
Below is the info on the tool used to set up the Kubernetes cluster and its current version.
- Kubernetes Server 1.19.7
- Cluster Management Tool — kops
- Cloud Provider AWS
This article demonstrated how to set up a Jenkins Pipeline to achieve automated CI/CD while including Github and Jenkins integration. The moment Git Pull request is raised on a particular branch, the build is triggered and results are made available to the branch admin to review before merging the pull request.
Published at DZone with permission of Rajesh Gheware. See the original article here.
Opinions expressed by DZone contributors are their own.