Automating Application Deployment with Jenkins: A Guide to Streamline Software Delivery
Introduction :
Jenkins is an open-source repository of rich integrations and plugins that are well-documented and extensible, based on an extended community of developers who have developed hundreds of plugins to accomplish almost any task.
Jenkins runs on the server and requires constant maintenance and updates. The availability of Jenkins as a cross-platform tool for Windows, Linux, and various operating systems makes it stand out among other DevOps tools. Moreover, it can easily be configured using CLI or GUI.
A short note about CI/CD Jenkins:
While implementing CI/CD with Jenkins, developers and the DevOps team don’t need to worry about the additional procurement costs involved in setting up code pipelines, as Jenkins is free and open-source. This simply means that they no longer have to get spending approval from management.
Another remarkable feature that highlights Jenkins’ value is the extended variety of Jenkins plugins available in Jenkins. With this diverse range, users of different cloud providers can feasibly utilize CI processes via Jenkins in significantly lesser time.
Jenkins, with its long history of CI/CD practices, was introduced in the year 2011. Its availability on the open-source platform provides it an edge over other tools used for the same purpose.
Configuration :
Installation
To configure automated CI/CD with Jenkins, the first and most obvious step is to install Jenkins. To use the Ubuntu repository, execute the command in the terminal:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
After that, add the Jenkins repository by executing this command:
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ >
/etc/apt/sources.list.d/jenkins.list'
Make sure to update the packages using the sudo command as shown here:
sudo apt-get update
Finally, install Jenkins:
sudo apt-get install jenkins
Make sure to install Java Runtime Environment (JRE) explicitly after installing Jenkins. However, if you get a “package error”, you must check for the Java version by executing the command given below:
java -version.
If the package does not exist, then you will have to install it. For this, you will first search for the available packages:
sudo apt search openjdk
Next, pick the available version and install it.
sudo apt install openjdk-11-jdk
After the successful installation, check the Java version.
java -version
You will get a result like this:
The second step is to run Jenkins. However, you can also run Jenkins on several platforms including Docker, and Kubernetes.
Starting the Server
After the successful installation of Jenkins, you will start it on localhost. For this step, you should start Jenkins first using the systemctl command on the command line.
sudo systemctl start jenkins
sudo systemctl enable jenkins
To check the status of the Jenkins service, use the status command as shown below:
sudo systemctl status jenkins
The output will be rendered as “Active” if everything has been configured successfully.
Post-Installation Setup
When you configure Jenkins for the first time, it is crucial to unlock it using an automatic password. For this setup, type http://localhost:8080 in the browser, and wait for the Getting Started page to appear. The automatically-generated password is stored in /var/jenkins_home/secrets/initialAdminPassword file. The file can be also accessed via the command-line interface using the cat command with the filename.
Copy/paste the password in the Administrator Password field and click Continue. The dialogue box will close and that’s it! You have successfully set up the user. After that, you will get the Customize Jenkins page. From this page, you can install any number of plugins you require. You can install suggested plugins as they are selected by default and you can also choose any additional plugins. Make sure to install the Git Plugin.
Configure GitHub
Now that you have successfully configured the user, it is time to configure the GitHub repository using Webhooks. Additionally, you can also perform this step after adding the Jenkins project.
Head over to the existing GitHub repository, and click the Settings tab.
After that, click on the Webhooks option from the navigation panel on the left side of the screen.
Now, click on the Add webhook button on the right side of the screen.
You will see a new form appear in the webhooks section. In the Payload URL field, type in the Jenkins endpoint. In our case, it is localhost: 8080. Since the localhost is behind a Firewall, make sure to make it visible to your public GitHub repository by using a webhook proxy service. In our case, we have used SocketXP. If you have a specific URL, make sure that it is publicly available. At the end of the URL, type /github-webhook/.
Choose application/json as Content-Type from the drop-down menu, select Just the push event, and click the Add webhook button.
For now, this repository will only call Jenkins on the push event. For other events, you will have to select the Let me select individual events option instead of Just the push event.
That’s all! You are done with the configurations on the GitHub side.
Jenkins Project and Adding GitHub
After successfully logging in, the Jenkins dashboard will appear on the screen. Here, all the CI/CD pipelines and their summary are visible to a user. On the Jenkins dashboard, click on “New Item”. After that, enter the name of your project, choose “Freestyle Project” from the list, and hit the enter key.
The next step is to configure the project on the Jenkins dashboard. In the General tab, choose your GitHub Project, and enter the GitHub Repository URL. Now, head over to the Source Code Management tab to add the credentials.
For this step, press the Add button and type your Username and Password. Press Add again to close the dialogue box. Be sure to select the main branch in the Branch Specifier.
Now, head over to the Build Triggers section to set the triggers for Jenkins. The purpose of triggers is to necessarily indicate to Jenkins when to build the project. Select the Poll SCM section, which queues VCS on the predefined schedule.
Build Jenkins Project
Now go back to the repository that you configured, make a sample file, or edit any existing file and commit the changes.
pipeline {
agent {
docker {
image 'trainwithshubham/django-todo'
args '-p 3000:3000'
}
}
stages {
stage('Build') {
steps {
sh 'docker-compose -f /path/to/docker-compose.yml up'
}
}
}
stage('Test') {
steps {
sh './jenkins/scripts/test.sh'
}
}
}
After that, head over to Jenkins and click on the “Build Now” button from the navigation bar.
The build number will display a green tick if the build is successful, and the app will be published on the localhost:8080. You can also use the output console to debug the application.
As you can see, Jenkins pulled the changes from the GitHub repository and reflected them in its console.
Finally, you have successfully integrated Jenkins with the GitHub repository. With each push, commit, or update, GitHub will trigger the Jenkins job, which in turn will execute the necessary steps to deploy the changes.
Plugins:
Jenkins can be extended via additional Plugins with more functionality. You can configure your Plugins via the Manage Jenkins Manager Plugins link.
To install Plugins in Jenkins select the Manage Jenkins Manager Plugins link and search for the Plugin you want to install. Select it from the list and select to install it and restart Jenkins.
The following table is a summary of commonly used Plugins.
Table 1. Jenkins Plugins | ||
Plug-in name | Description | URL |
---|---|---|
Git Plugin | This Plugin allows to use Git as a build SCM. | |
Xvnc Plugin | This Plugin allows projects to run xvnc during a build. This allows for example to run tests which require a display to run on a virtual display. To use this Plugin you need to connect once to your vncserver on the command line to provide a password. Use for example the following commands. | |
Gradle Plugin | This Plugin allows to run Gradle builds, e.g., as required for Android, via Jenkins. | |
Maven Plugin | This Plugin allows to run Maven builds. | |
GitHub Plugin | This Plugin integrates Jenkins with Github projects. | |
Publish Over SSH Plugin | This Plugin allows to publish build artifacts via ssh | |
Workspace Cleanup Plugin | This Plugin allows to delete the workspace before the build or when a build is finished and artifacts are saved. | |
Github Pull Request Builder | This Plugin allows to build GitHub Pull Requests |
6.2. Restart your Jenkins
After installing Plugins you will need to restart Jenkins. You can do so by adding restart
as URL parameter,
or with the following command:
service jenkins restart
Conclusion:
With the automation capabilities of Jenkins and the power of Docker containers, deploying and managing applications becomes more efficient and reliable. In Task-01, we learned how to integrate a Docker container into a Jenkins freestyle project, enabling automated builds and deployments. In Task-02, we extended the automation by using Jenkins to orchestrate multiple containers defined in a Docker Compose file. By following these steps, you can harness the full potential of containerization and automation to streamline your software development and deployment processes.
To connect with me - https://www.linkedin.com/in/subhodey/