Using Jenkins for CI/CD on an Ubuntu / Debian Linux Server

Jul 17, 2024

What is Jenkins?

Jenkins is an open-source automation server that helps automate parts of the software development process, including building, testing, and deploying applications. Originally developed as the Hudson project, Jenkins has grown into a robust platform with a large community and a wide array of plugins. Some of its key features include:

  • Easy installation and configuration
  • Extensibility through plugins
  • Support for various version control systems
  • Integration with a wide range of build and deployment tools

Setting Up Jenkins on a Linux Server

Before we can start using Jenkins, we need to set it up on a Linux server. Here are the steps to get Jenkins up and running:

Prerequisites:

  • A Linux server (Ubuntu is used in this example)
  • Java Development Kit (JDK) installed
  • Root or sudo access to the server

Installation Steps:

Update the system:

sudo apt update
sudo apt upgrade

Install Java:

sudo apt install openjdk-11-jdk

Add Jenkins repository and key:

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

Install Jenkins:

sudo apt update
sudo apt install jenkins

Start Jenkins service:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Initial Configuration:

Access Jenkins: Open your browser and go to http://your_server_ip:8080. Unlock Jenkins: Retrieve the initial admin password using:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Install suggested plugins: Follow the on-screen instructions to install the recommended plugins.

Create the first admin user: Complete the setup by creating an admin user.

Creating a Simple CI/CD Pipeline

A CI/CD pipeline automates the process of integrating code changes and deploying applications. Here’s how to create a basic pipeline in Jenkins: Create a new pipeline job: Define the pipeline:

pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}

Save and run the pipeline:

Running and Monitoring the Pipeline

Once your pipeline is set up, Jenkins provides tools to monitor and manage your builds:

  • Build History: View the history of builds in the job’s page.
  • Console Output: Access logs for each build to see detailed output and errors.
  • Build Status: Use the built-in indicators to quickly assess the status of recent builds.

Common Issues and Troubleshooting

Setting up Jenkins can sometimes come with challenges. Here are some common issues and their solutions:

  • Jenkins service not starting: Check the status with sudo systemctl status jenkins and look for error messages in the logs (/var/log/jenkins/jenkins.log).

  • Port conflicts: Ensure no other services are using port 8080 or configure Jenkins to use a different port.

  • Insufficient permissions: Ensure Jenkins has the necessary permissions to access the required directories and files.

    Jenkins is a powerful tool for implementing CI/CD, and setting it up on a Linux server is a straightforward process. By following the steps outlined in this article, you can create and manage a simple CI/CD pipeline, helping to streamline your development workflow. As you become more familiar with Jenkins, you can explore its many plugins and advanced features to further enhance your CI/CD processes. Happy automating!

Back to Top

Follow us on Twitter, Facebook or LinkedIn to receive updates regarding network issues, discounts and more.
2024 © JustAHost. All rights reserved.