Edited 1 week ago by ExtremeHow Editorial Team
AutomationJenkinsCI/CDDevOpsConfigurationInstallationPipelinesServer SetupJob ManagementScripting
This content is available in 7 different language
Jenkins is a widely used open-source automation server that helps in building, deploying, and automating software processes. When it comes to continuous integration and delivery (CI/CD), Jenkins is one of the most popular tools. One of the major advantages is that Jenkins can be integrated with many testing and deployment technologies. This document provides a comprehensive guide to installing and configuring Jenkins on a Linux server. We will cover everything from prerequisites, to installation, to basic security configuration.
Before we get into the installation process, it is essential to ensure that certain pre-requisites are met:
To check if Java is installed and to verify its version, run the following command:
java -version
If Java is not installed, you can install it using the following command depending on your Linux distribution:
sudo apt update sudo apt install openjdk-11-jdk
sudo yum install java-11-openjdk
Once Java is installed, confirm the installation by running the following:
java -version
Once Java is up and running, the next step is to install Jenkins. Jenkins provides a repository for all Linux distributions, making the installation process easy.
You need to add the Jenkins Debian repository key to the system.
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
Now that the Jenkins repository has been added, you can install Jenkins using a package management tool like apt
or yum
.
sudo apt install jenkins
sudo yum install jenkins
Once Jenkins is installed, you can start it and enable it to start automatically when you boot.
sudo systemctl start jenkins sudo systemctl enable jenkins
To make sure Jenkins is running, check its status:
sudo systemctl status jenkins
After running Jenkins, the next step is to configure it.
Jenkins can be accessed via a web browser. By default, it runs on port 8080. Open the browser and go to:
http://localhost:8080
If you're working remotely, replace localhost
with your server's IP address.
When run for the first time, Jenkins displays a page that prompts you to unlock Jenkins using the initial password.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste it into the "Administrator Password" field, then click Continue.
You can install suggested plugins for Jenkins or choose specific plugins. Initially, it is recommended to choose the "Install suggested plugins" option to add commonly used plugins, but you can customize it later.
Setting up the first admin user is very important for security. Fill in the required fields and save your credentials.
After the initial setup, go to Manage Jenkins > Configure System. Here, you can adjust settings such as executors, nodes, and system messages.
Securing Jenkins is a crucial step, as it is a critical point in your CI/CD pipeline. Below are some essential security steps that must be implemented:
Cross-Site Request Forgery (CSRF) token protection is important. Make sure "Prevent cross-site request forgery exploits" is checked under Manage Jenkins > Configure Global Security.
An effective way to manage user permissions is through role-based access control. You can achieve this by installing the Role Strategy plugin.
To secure communications between the Jenkins server and its users, configure Jenkins to use HTTPS. You will need a valid SSL certificate, which can be obtained for free with Let's Encrypt or purchased from a certificate authority (CA).
keytool -import -alias jenkins -file your_ssl_certificate.crt -keystore /path/to/your/certificate/store
Jenkins allows you to monitor user activities through security logs. This can help troubleshoot security issues or observe suspicious activities.
Once your Jenkins server is set up and secured, the next logical step is to configure it to build and deploy your projects. Jenkins offers seamless integration with version control systems like GitHub, Git, SVN, and others.
Go to the Jenkins dashboard and click on New Item. Choose a Freestyle project for a wide range of configuration options.
Set up the version control system under Source Code Management. For Git, specify the repository URL and any required credentials.
Jenkins allows you to automate builds using triggers. These can be scheduled or set to run when a change is detected in your version control system.
You can specify the build environment, such as scripts that need to be run before or after the build process. This can include setting paths, cleaning up directories, or other preliminary steps.
You can add multiple build steps depending on your project's needs:
Configure the actions to be performed by Jenkins after the build, such as sending notifications, deploying packages, or archiving artifacts.
Jenkins has a huge range of plugins that extend its capabilities. Here are some common types of integrations:
These plugins support interaction with various version control systems such as Git, Subversion, Mercurial, etc. Install them under Manage Jenkins > Manage Plugins.
Integrate with Apache Maven, Gradle, and Ant using build tool-specific plugins. These streamline your build lifecycle.
Configure Jenkins to distribute build status to external systems such as Slack, HipChat, or email.
Implement monitoring using Nagios, DataDog, or NewRelic plugins for advanced system diagnostics and alerts.
With JUnit, NUnit, TestNG, and other plugins, Jenkins can run and report tests as part of the build process.
Administering Jenkins instances involves ensuring that processes are optimized and efficient, and that system reliability is always a priority.
Pipelines written in Groovy provide more flexibility and control over the build. They support complex job structures, parameterization, and more detailed job configuration.
Be sure to regularly back up the /var/lib/jenkins
directory to preserve system configuration and historical job data.
Check for updates to Jenkins and its plugins frequently. Regular updates ensure you have the latest security patches and features.
Monitor CPU, memory, and I/O to avoid interruptions that can slow down or corrupt the build.
Docker complements Jenkins by providing a containerized build environment. Its isolation ensures that builds are predictable and consistent.
The larger your Jenkins infrastructure, the more likely you are to encounter problems. Here are ways to solve common Jenkins problems:
Logs are your first line of information when troubleshooting. Check /var/log/jenkins/jenkins.log
file for any error messages or warnings.
If the build hangs or is extremely slow, ensure adequate CPU, RAM, and disk space allocation.
Make sure the repository URL, branch name, and access credentials are correct.
Verify that the network settings allow Jenkins to access the required services, and validate any HTTP proxy settings.
This guide has provided a comprehensive look at how to install and configure Jenkins for optimal use on Linux systems. From installation and security to best practices, this tutorial covered several important aspects to harness the full potential of Jenkins. With these essentials, your software development and deployment initiatives can become more efficient and flexible. Remember, the power of Jenkins increases significantly when it is tailored to your specific project needs through its extensive plugin system and customizable configuration.
If you find anything wrong with the article content, you can