Configure CI/CD on Linux Server for Reliable Deployments

CI/CD (Continuous Integration/Continuous Deployment) is a set of modern software development practices that allow you to automate the process of building, testing, and deploying code to production. By learning how to configure CI/CD, you can ensure that every code change is automatically tested, built, and deployed, improving the quality and speed of software delivery.

Configure CI/CD

Configuring CI/CD on a Linux server enables you to streamline your development workflow and deliver new features and fixes to users faster and more reliably.

In this guide, we will walk you through setting up a basic CI/CD pipeline using Jenkins, a popular open-source automation server.

Prerequisites

Before configuring CI/CD pipeline on your Linux server, ensure the following:

  • Linux Server: A Linux-based server (Ubuntu, CentOS, Debian, etc.) with root or sudo access.
  • Jenkins Installation: Jenkins will be used for automating the CI/CD pipeline.
  • Git Repository: You should have a project in a Git repository (e.g., GitHub, GitLab, Bitbucket).
  • Java: Jenkins requires Java to run, so Java needs to be installed on your server.

Once you have these prerequisites, you can begin setting up your CI/CD pipeline.

Configure CI/CD on Linux Server

To configure CI/CD on a Linux server, you need to set up tools that automate code integration, testing, and deployment. This streamlines development workflows, ensures faster delivery, and helps maintain high-quality software through continuous monitoring and automation.

Step 1: Install Jenkins

Jenkins will be the core tool in our CI/CD pipeline, automating the build, test, and deployment processes.

Install Java

Jenkins requires Java to run. You can install OpenJDK (Java Development Kit) on your Linux server.

  • On Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-11-jdk
  • On CentOS/RHEL:
sudo yum install java-11-openjdk-devel

Check the installed Java version:

java -version

Install Jenkins

To install Jenkins, first, add the official Jenkins repository and key.

  • On Ubuntu/Debian:

Add the Jenkins repository and GPG key:

wget -q -O - https://pkg.jenkins.io/ci.ubuntu.com/jenkins.io.key | sudo apt-key add -
echo "deb http://pkg.jenkins.io/debian/ stable main" | sudo tee -a /etc/apt/sources.list.d/jenkins.list

Install Jenkins:

sudo apt update sudo apt install jenkins
  • On CentOS/RHEL:

Add the Jenkins repository:

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo

Import the GPG key:

sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key

Install Jenkins:

sudo yum install jenkins

Start Jenkins

Once Jenkins is installed, start and enable Jenkins to start at boot.

  • On Ubuntu/Debian:
sudo systemctl start jenkins
sudo systemctl enable jenkins
  • On CentOS/RHEL:
sudo systemctl start jenkins
sudo systemctl enable jenkins

Open Jenkins in the Browser

By default, Jenkins runs on port 8080. You can access it by opening your browser and navigating to:

http://your-server-ip:8080

You’ll be asked to unlock Jenkins by providing a password. To get this password, run:

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

Copy the password and paste it into the web interface to complete the setup.

Step 2: Install Necessary Jenkins Plugins

Jenkins comes with a basic set of plugins, but to enable integration with Git, GitHub, and various deployment tools, you’ll need to install additional plugins.

Install Git Plugin:

The Git plugin allows Jenkins to connect to a Git repository.

  • Go to Manage Jenkins → Manage Plugins.
  • Select the Available tab, search for “Git plugin,” and install it.

Install Pipeline Plugin:

This plugin enables you to create CI/CD pipelines as code.

  • Go to Manage Jenkins → Manage Plugins.
  • Select the Available tab, search for “Pipeline,” and install it.

Install SSH and Deployment Plugins:

For deployment tasks, you can install plugins such as SSH Agent Plugin (for SSH key management) and Publish Over SSH (for deployment).

Step 3: Configure Git Repository Integration

Jenkins needs to access the Git repository to pull the code and trigger builds.

Add Your Git Repository:

  • Go to Manage Jenkins → Configure System.
  • Under Git, add the path to your Git executable.
  • Configure GitHub Integration (if you’re using GitHub) under GitHub Servers by adding your credentials (OAuth token, username/password).

Create a Jenkins Job:

  • Go to the Jenkins dashboard, click New Item, and create a Freestyle Project.
  • Under the Source Code Management section, select Git and provide the repository URL and credentials (if necessary).

Step 4: Create a Pipeline for CI/CD

Jenkins Pipelines allow you to define your CI/CD pipeline as code using a Jenkinsfile. This file contains instructions for building, testing, and deploying your code.

Create a Jenkinsfile in Your Project’s Root Directory

A simple example of Jenkinsfile for a Node.js project might look like this:

    pipeline {
        agent any
    
        stages {
            stage('Checkout') {
                steps {
                    git 'https://github.com/your-username/your-repo.git'
                }
            }
            stage('Build') {
                steps {
                    script {
                        sh 'npm install'
                    }
                }
            }
            stage('Test') {
                steps {
                    script {
                        sh 'npm test'
                    }
                }
            }
            stage('Deploy') {
                steps {
                    sshPublisher(
                        publishers: [
                            sshPublisherDesc(
                                configName: 'your-server',
                                transfers: [
                                    sshTransfer(
                                        sourceFiles: '**/*.tar.gz',
                                        remoteDirectory: '/path/to/remote/dir',
                                        removePrefix: 'dist',
                                        execCommand: 'tar -xzvf /path/to/remote/dir/*.tar.gz'
                                    )
                                ]
                            )
                        ]
                    )
                }
            }
        }
    }
    

    In this pipeline:

    • Checkout: Pulls the latest code from Git.
    • Build: Runs npm install to install dependencies.
    • Test: Runs npm test to execute unit tests.
    • Deploy: Uses SSH to deploy the build to a remote server.

    Create the Pipeline in Jenkins:

    • Go to New Item and select Pipeline.
    • Under the Pipeline section, set the Definition to “Pipeline script from SCM.”
    • Choose Git as the SCM, provide the repository URL, and point Jenkins to the Jenkinsfile in your repo.

      Step 5: Trigger Builds

      Jenkins can trigger builds automatically based on various events, such as code pushes or pull requests.

      Trigger Build on Code Push:

      • Under Build Triggers, enable GitHub hook trigger for GITScm polling (if using GitHub).
      • Alternatively, use Poll SCM to configure periodic polling for changes.

      Manual Trigger:

      You can also trigger builds manually by going to the Jenkins dashboard and clicking on Build Now.

      Step 6: Monitor and Maintain the Pipeline

      Jenkins provides a detailed view of each pipeline run, including logs, build results, and test outputs.

      View Build Logs: From the Jenkins job page, click on a build to view its logs and see where any errors occurred.

      Configure Notifications: Jenkins can send email notifications or integrate with services like Slack to notify the team about build statuses.

      Automate Deployments: Jenkins can be configured to automatically deploy the application to various environments (e.g., staging, production) once the build and tests pass.

      Conclusion

      In this guide, we have walked through how to configure CI/CD on a Linux server using Jenkins, from installation and integration with Git to creating a Jenkins pipeline for continuous integration and deployment. By automating your software build, test, and deployment processes, you improve the efficiency of your development workflow and ensure faster, more reliable software delivery.

      With Jenkins set up, you now have a powerful CI/CD pipeline running on your Linux server, capable of managing the complete lifecycle of your code from development to production. By continuously integrating and deploying changes, your software will be more stable and secure, allowing your team to focus on delivering new features instead of manually managing deployments.

      Leave A Comment