Learn about Jenkins and CI / CD

Tram Ho

1. Introduction

Recently, I have just received a task to learn and integrate CI / CD for the project I am working on. If you have ever learned about CI / CD, you will definitely see the need to integrate it into the software development process. In other words, it will take care of automatically build, run tests and deploy code when there is a change. After a week of fidgeting, learning online Jenkins has a tool that can assist in creating jobs running CI and CD.

CI / CD – Continuous Intergration / Continuous Delivery

First, let’s talk about CI / CD again. CI or Continuous Integration, is a software development method that requires team members to integrate their work regularly, at least once a day. Each integration is built automatically (including tests) to detect errors as quickly as possible. And of course makes software integration and development faster and more efficient.

Next, the full CD is Continuous Delivery, which is the process of deploying all code changes to the test or staging environment. Continuous Delivery allows automation of testing in addition to using unit tests, testing software through multiple measures before deploying to customers.

What is Jenkins?

Is an open source Web application written in Java, acting as the build & test server of the continuous integration system. Jenkins can be integrated with almost all other tools of continuous integration with various platforms. In addition, the Jenkins community is large, so it is easy to use and support when in need.

2. Use Jenkins to configure CI / CD

Returning to the initial problem, the required details are as follows: When a new PR is created, Jenkins will capture events and triggers to run the CI job, the CI job will run Unit Test, checkstyle code, and report the result. When the PR is merged, the job CD will run new deploy code on the server test and just wait until the notification of successful deployment on Chatwork is done: v .

2.1. Install Jenkins

Jenkins is written in Java, so before you install it you need to install Java on your computer. Let’s get started!

To access Jenkins, go to the browser and enter http: // localhost: 8080 . Next is the installation step for the first time you start Jenkins. In general there is nothing special, just install the plugin step, you should click on “Install suggested plugins” only.

Here we also need to use Github webhook to trigger events like creating PR or merge PR, which webhook needs to have a public domain to operate. You can use tools to public port 8080 on local to public domain. For example, ngrok.com and follow the instructions or run the command ssh -R 80:localhost:8080 ssh.localhost.run .

2.2. CI configuration

First of all we need to know when on the repo a new pull request is created to run CI. This requires a Jenkins plugin called GitHub Pull Request Builder . We will go to plugin management: Manage Jenkins> Manage Plugins> Available tab , search and click “Install without restart”. Step 1 : Create a webhook on the repo: Go to webhook settings, select Add webhook. At the url payload, enter the following format: <PUBLIC_DOMAIN>/ghprbhook/ example: http:/my-domain.com/ghprbhook/ . Content type select application / json and add Secret if needed. Finally in the action section select Issue comments and Pull requests.

Step 2 : Install the GitHub Pull Request Builder plugin. Go to settings: Manage Jenkins> Configure System , Add the credentials (username / password) of the github account for the plugin.

Step 3 : Create a new job. In the Github project section enter the repo link into it. Next, select GitHub Pull Request Builder, tick “Use github hooks for build triggering”. In the Advance Setting … section, enter whitelist branch when a new pull request is created. And if you want to change the display name on the pull request when the job runs, you can set it in the Trigger setup section. Add the “Update commit status during build” action and enter the job’s display name in the “Commit Status Context” box. Step 4 : Write a script pipeline for the job.

The installation is done, now try to make a pull request on the repo to the master branch and experience.

2.3. CD configuration

Unfortunately, the webhook github doesn’t support us when there is a merge pull request event. Therefore, we need to use the Generic Webhook Trigger Plugin and analyze the Github webhook payload when there is a merge event. Here, we need to check the $ .action = “closed” and $ .pull_request.merged = true fields.

Step 1 : Create another webhook with the url payload as follows: <PUBLIC_DOMAIN>/generic-webhook-trigger/invoke?token=my-token , in the action section select Pull requests.

Step 2 : Create a new job. In the Build Trigger section, select Generic Webhook Trigger and set as in the image below. Step 3 : Write a script for the job.

3. Conclusion

Above is a tutorial on configuring and integrating CI / CD using Jenkins into the project. Look forward to the comments and share the comments of more people. Thank all!

Share the news now

Source : Viblo