WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install and Configure Jenkins on Linux

Edited 1 week ago by ExtremeHow Editorial Team

AutomationJenkinsCI/CDDevOpsConfigurationInstallationPipelinesServer SetupJob ManagementScripting

How to Install and Configure Jenkins on Linux

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.

Prerequisites

Before we get into the installation process, it is essential to ensure that certain pre-requisites are met:

Install Java

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:

On Debian/Ubuntu

sudo apt update sudo apt install openjdk-11-jdk

On CentOS/RHEL

sudo yum install java-11-openjdk

Once Java is installed, confirm the installation by running the following:

java -version

Installing Jenkins

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.

Step 1: Add Jenkins key and repository

You need to add the Jenkins Debian repository key to the system.

On Debian/Ubuntu

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

On CentOS/RHEL

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

Step 2: Install Jenkins

Now that the Jenkins repository has been added, you can install Jenkins using a package management tool like apt or yum.

On Debian/Ubuntu

sudo apt install jenkins

On CentOS/RHEL

sudo yum install jenkins

Step 3: Start and enable Jenkins service

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.

Configuring Jenkins

Step 1: Access Jenkins via the web interface

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.

Step 2: Unlocking Jenkins

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.

Step 3: Customize Jenkins

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.

Step 4: Create an admin user

Setting up the first admin user is very important for security. Fill in the required fields and save your credentials.

Step 5: Configure system settings

After the initial setup, go to Manage Jenkins > Configure System. Here, you can adjust settings such as executors, nodes, and system messages.

Security configuration

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:

Enable CSRF protection

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.

Set up role-based access control

An effective way to manage user permissions is through role-based access control. You can achieve this by installing the Role Strategy plugin.

Use HTTPS

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

Enable security logging

Jenkins allows you to monitor user activities through security logs. This can help troubleshoot security issues or observe suspicious activities.

Configuring Jenkins for projects

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.

Step 1: Create a new job

Go to the Jenkins dashboard and click on New Item. Choose a Freestyle project for a wide range of configuration options.

Step 2: Configure source code management

Set up the version control system under Source Code Management. For Git, specify the repository URL and any required credentials.

Step 3: Set up build triggers

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.

Step 4: Configure the build environment

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.

Step 5: Add the build step

You can add multiple build steps depending on your project's needs:

Step 6: Post Build Actions

Configure the actions to be performed by Jenkins after the build, such as sending notifications, deploying packages, or archiving artifacts.

Integrations and plugins

Jenkins has a huge range of plugins that extend its capabilities. Here are some common types of integrations:

SCM plugins

These plugins support interaction with various version control systems such as Git, Subversion, Mercurial, etc. Install them under Manage Jenkins > Manage Plugins.

Construction equipment

Integrate with Apache Maven, Gradle, and Ant using build tool-specific plugins. These streamline your build lifecycle.

Notification plugins

Configure Jenkins to distribute build status to external systems such as Slack, HipChat, or email.

Monitoring tools

Implement monitoring using Nagios, DataDog, or NewRelic plugins for advanced system diagnostics and alerts.

Testing framework

With JUnit, NUnit, TestNG, and other plugins, Jenkins can run and report tests as part of the build process.

Best practices for Jenkins administration

Administering Jenkins instances involves ensuring that processes are optimized and efficient, and that system reliability is always a priority.

Use pipelines instead of freestyle projects

Pipelines written in Groovy provide more flexibility and control over the build. They support complex job structures, parameterization, and more detailed job configuration.

Implement a backup strategy

Be sure to regularly back up the /var/lib/jenkins directory to preserve system configuration and historical job data.

Update Jenkins and plugins regularly

Check for updates to Jenkins and its plugins frequently. Regular updates ensure you have the latest security patches and features.

Resource monitor

Monitor CPU, memory, and I/O to avoid interruptions that can slow down or corrupt the build.

Use Docker for isolation

Docker complements Jenkins by providing a containerized build environment. Its isolation ensures that builds are predictable and consistent.

Jenkins troubleshooting

The larger your Jenkins infrastructure, the more likely you are to encounter problems. Here are ways to solve common Jenkins problems:

Check the logs

Logs are your first line of information when troubleshooting. Check /var/log/jenkins/jenkins.log file for any error messages or warnings.

Insufficient resources

If the build hangs or is extremely slow, ensure adequate CPU, RAM, and disk space allocation.

Error in Git/SCM configuration

Make sure the repository URL, branch name, and access credentials are correct.

Networking problems

Verify that the network settings allow Jenkins to access the required services, and validate any HTTP proxy settings.

Conclusion

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


Comments