Introduction
Jenkins is a powerful open-source automation server designed to automate repetitive technical tasks within the continuous integration and delivery of software. With a vast array of plugins and extensive support, Jenkins is capable of managing a wide range of workloads to build, test, and deploy applications.
Previously, we set up Jenkins on an Ubuntu 20.04 server and configured it with SSL using an Nginx reverse proxy. This guide demonstrates how to configure Jenkins to automatically test an application whenever changes are pushed to a repository.
Prerequisites
To follow this guide, you must have an Ubuntu 20.04 server equipped with at least 1G of RAM and a secure Jenkins installation. Assigning a domain name to the Jenkins server is necessary to secure the web interface properly. You can follow these guides to set up Jenkins as recommended:
- Install Jenkins on Ubuntu 20.04
- Install Nginx on Ubuntu 20.04
- Secure Nginx with Let’s Encrypt on Ubuntu 20.04
- Configure Jenkins with SSL using an Nginx Reverse Proxy
To effectively manage our testing environment, we will conduct our application’s tests within Docker containers. Once Jenkins is operational, install Docker on the server by following steps one and two of the guide on installing and using Docker on Ubuntu 20.04.
Add the Jenkins User to the Docker Group
Once prerequisites are complete, both Jenkins and Docker should be installed on your server. However, the Linux user running the Jenkins process is unable to access Docker by default.
To rectify this, add the jenkins user to the docker group using the usermod command:
sudo usermod -aG docker jenkins
Verify that the jenkins user has been successfully added by listing the members of the docker group:
grep docker /etc/group
Output: docker:x:999:sammy,jenkins
Restart the Jenkins process to apply the new group membership:
sudo systemctl restart jenkins
Create a Personal Access Token in GitHub
To enable Jenkins to monitor your GitHub projects, create a Personal Access Token in your GitHub account.
Visit GitHub and log into your account. Click on your user icon in the upper-right corner and select Settings from the dropdown menu. In the Developer settings section, click Personal access tokens, then Generate new token. Define the token's scope, ensuring to check repo:status, repo:public_repo, and admin:org_hook. After generating the token, copy it for later use.
Add the GitHub Personal Access Token to Jenkins
Log into your Jenkins web interface using the administrative account. Click your username in the top-right corner, select Credentials, and add new credentials by selecting Secret text under the Kind dropdown. Paste your GitHub personal access token into the Secret field, describe it, and save.
Set Up Jenkins Access to GitHub
In Jenkins, go to Manage Jenkins and select Configure System. Locate the GitHub section, click Add GitHub Server, and input your GitHub personal access token in the Credentials dropdown. Test the connection and save your changes.
Set Up the Demonstration Application in your GitHub Account
Use a "hello world" program created with Hapi.js for demonstration. Fork the project repository on GitHub. The repository contains a package.json file, defining runtime and development dependencies. You can run tests using npm test.
Create a New Pipeline in Jenkins
In Jenkins, create a new item and select Pipeline as the item type. Enter your project's GitHub repository URL, select the GitHub hook trigger for GITScm polling in the Build Triggers section, and configure the pipeline to run the Jenkinsfile from your repository.
Performing an Initial Build and Configuring the Webhooks
Manually initiate a build in your pipeline's main page by clicking Build Now. Once Jenkins builds the project, it will register a webhook with your GitHub project. Verify the webhook in your GitHub repository's settings.
Conclusion
In this guide, Jenkins was configured to watch a GitHub project and automatically test new changes. Jenkins pulls code from the repository and runs build and testing procedures within Docker containers. Further instructions can be added to the Jenkinsfile for deployment or storage of the code.





















