CI with Jenkins and GitHub in K8s
In the last article, we teach you how to set up Jenkins in a Kubernetes cluster. Now that you got your Jenkins up and running, it’s time to use Jenkins as a CI tool to run your project tests based on GitHub pull requests.
In order to do that, you need to do mainly 4 steps:
- Providing Jenkins a worker in your K8s cluster;
- Connecting Jenkins with GitHub;
- Adding a Jenkinsfile to your project;
- Creating the pipeline.
1. Providing a Jenkins a worker in your K8s cluster
Configuring Jenkins
The first you should do is install the Kubernetes plugin.
Once it is installed you must go to Configure Clouds page (Manage Jenkins > Manage System > Cloud) and add Kubernetes as your cloud. After that, you will be able to see a form in which you can input Jenkins’s namespace and the service in the cluster.
Creating a docker image for Jenkins workers
You must create a docker image with your project dependencies in order for Jenkins’s workers to be able to run your project.
We took as an example an application that needs Node and Yarn and we are installing these dependencies to the image.
2. Connecting Jenkins with GitHub
In order to trigger Jenkins in GitHub Pull Requests, we need to install a plugin. We are going to install the GitHub Pull Request Builder Plugin.
Once you have installed, if you go to the Configure System page (Manage Jenkins > Manage System), you will find a form.
First, we need to fulfill the first field with the GitHub URL https://api.github.com.
Then we need to add your SSH credentials to connect. That credential should be registered on Jenkins.
Now it’s time to test your credentials. Click the `Connect to API` button to test your connection to the GitHub API:
You can also see the permissions of your credentials
In addition to that, you can configure custom messages for each build status and a whole of other things. But all this is optional, so I won’t go in more detail now.
Configuring GitHub
Now it’s time to configure your GitHub account to notify Jenkins when something happens. For that, we will configure GitHub WebHooks.
GitHub webhooks in Jenkins are used to trigger the build whenever a developer commits something to the master branch. For more details on that check out this article.
Let’s see how to add build a webhook in GitHub and then add this webhook in Jenkins.
- Go to your project repository;
- Go to “settings” in the right corner;
- Click on “webhooks”;
- Click “Add webhooks”;
- Write the Payload URL.
3. Adding a Jenkinsfile to your project
To keep track of your pipeline changes we advise you to keep the Jenkins Configuration in your project repository. To do that we add a Jenkinsfile.
In the Jenkinsfile you should declare the pod template of the Jenkins workers that will run your project and describe the pipeline stages of the CI process.
This file defines everything that will happen during your CI. So let’s explain everything in detail.
The podTemplate attribute (line 4 in the file above) is related to the how much of your cluster resources the Jenkins worker pod will use it. If you were running web-servers or websites, the configuration above is more than enough.
Between the curly braces, the first command is:
checkout scm
scm is a special variable that instructs the checkout step to clone the specific revision which has triggered this Pipeline run.
After that, we are setting the git configurations:
sh(‘git config user.name <YOUR_USERNAME>)
sh(‘git config user.email <YOUR_EMAIL>)
Then, we have the stages checkout with the master and the stages itself. In summary, checkout with the master, installing dependencies, build, and testing.
4. Creating the pipeline
When the GitHub sends the WebHook event, Jenkins will receive and do its work. In other words, it will execute a defined pipeline for that repository.
So let’s create a pipeline. In the Jenkins home click on “New Item” (first item):
After that select pipeline and click on ok button:
Now you shall see a bunch of configurations. We will go through to the required ones to make your pipeline be triggered by the GitHub WebHook.
You should enter your GitHub repository name in the Project url fielURLhen in the Build Trigger section you should check the following option:
And select your own credentials in the GitHub API credentials. In the field GitHub API Credentials.
Finally in the Pipeline section in the Definition select the option “Pipeline script from SCM”.
Enter your repository information in the Repositories section. Add the path to the JenkinsFile defined bellow in the Script Path section:
Try it out
Now that everything is done, let’s walk through a working example.
Go to your local repository folder and create a new branch:
git checkout -b test-branch
And push this branch to the remote repository:
git push test-branch
Commit something, and create a pull-request to the branch wanted:
Once you create this pull-request it will start a sequence of events:
1- GitHub Webhook will notify Jenkins.
If you go to the repository webhook configuration, you will see that a request was sent to Jenkins with all the information on the PR.
2- The pipeline will take the JenkinsFile and will start in a Pod with the defined Docker image.
If you go to Jenkins you will see that a Job was triggered.
You can also check in your K8s namespace that a Pod with the Jenkins Worker is running.
Good, we are all set, Jenkins will run all the steps you defined in your JenkinsFile.
If everything goes well your pipeline will succeed and you be able to see in the Jenkins UI.
Conclusion
Now we have your CI process running, we hope that this can make your development faster, easier, and less error-prone. Since all your process is now automated.
In the next article, we intend to teach you how to publish project images to the AWS Elastic Container Service using Jenkins.
Written together with Júlia Arruda.