Building a Jenkins CI/CD Pipeline: Step-by-Step Guide

Suyash BhawsarSuyash Bhawsar
3 min read

Continuous Integration/Continuous Deployment (CI/CD) is essential for modern software development. Jenkins, the popular open-source automation server, simplifies building, testing, and deploying applications. In this guide, you'll learn to set up a basic Jenkins pipeline with practical examples and clear instructions.


Step 1: Installing Jenkins

Commands:

  1. Install Java:

     sudo apt update
     sudo apt install openjdk-11-jdk -y
     java -version
    
  2. Add Jenkins Repository:

     wget -q -O - <https://pkg.jenkins.io/debian-stable/jenkins.io.key> | sudo apt-key add -
     sudo sh -c 'echo deb <https://pkg.jenkins.io/debian-stable> binary/ > /etc/apt/sources.list.d/jenkins.list'
    
  3. Install Jenkins:

     sudo apt update
     sudo apt install jenkins -y
     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

Step 2: Setting Up Jenkins

  1. Unlock Jenkins: Locate the initial admin password:

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

    Enter this password on the Jenkins setup page.

  2. Install Suggested Plugins: On the "Customize Jenkins" page, choose "Install suggested plugins."

  3. Create Admin User: Fill out the admin username, password, and email fields.


Step 3: Creating Your First Jenkins Pipeline

  1. Go to the Jenkins dashboard.

  2. Click "New Item" and select "Pipeline".

  3. Enter a name for your pipeline, e.g., MyFirstPipeline.

  4. In the pipeline configuration, select "Pipeline script" and paste the following code:

     pipeline {
         agent any
         stages {
             stage('Build') {
                 steps {
                     echo 'Building the application...'
                     // Simulate build step
                     sh 'echo "Build Step Simulated"'
                 }
             }
             stage('Test') {
                 steps {
                     echo 'Testing the application...'
                     // Simulate test step
                     sh 'echo "Test Step Simulated"'
                 }
             }
             stage('Deploy') {
                 steps {
                     echo 'Deploying the application...'
                     // Simulate deployment step
                     sh 'echo "Deploy Step Simulated"'
                 }
             }
         }
     }
    
  5. Save the pipeline configuration.

  6. On the pipeline page, click "Build Now" to run the pipeline.


Step 4: Adding Source Control Integration

  1. Install the Git Plugin:

    • Navigate to "Manage Jenkins" > "Manage Plugins" > "Available" and search for "Git Plugin."

    • Install and restart Jenkins.

  2. Update the pipeline script to pull code from a GitHub repository:

     pipeline {
         agent any
         stages {
             stage('Checkout') {
                 steps {
                     git url: '<https://github.com/your-repo/sample.git>', branch: 'main'
                 }
             }
             stage('Build') {
                 steps {
                     echo 'Building the application...'
                     sh 'make build'
                 }
             }
             stage('Test') {
                 steps {
                     echo 'Testing the application...'
                     sh 'make test'
                 }
             }
         }
     }
    

Step 5: Adding Notifications

  1. Install the Slack Notification Plugin:

    • Go to "Manage Jenkins > Manage Plugins", search for "Slack Notification," and install it.
  2. Configure Slack:

    • In Jenkins, go to "Manage Jenkins > Configure System > Slack".

    • Enter your Slack team subdomain and integration token.

  3. Update the pipeline script to send notifications:

     pipeline {
         agent any
         stages {
             stage('Build') {
                 steps {
                     echo 'Building the application...'
                 }
             }
         }
         post {
             success {
                 slackSend(channel: '#builds', message: 'Build succeeded!')
             }
             failure {
                 slackSend(channel: '#builds', message: 'Build failed.')
             }
         }
     }
    

Step 6: Monitoring and Troubleshooting

  1. Monitor Builds:

    • Check the pipeline dashboard for build status.
  2. View Logs:

    • Click a specific build and select "Console Output" for detailed logs.

Wrapping Up

Congratulations! You've set up a Jenkins pipeline that integrates with GitHub, runs build and test stages, and sends notifications. With these foundations, you can expand your pipeline to deploy applications to production environments or integrate advanced tools like Docker, Kubernetes, or SonarQube.

Encourage readers to experiment with Jenkins plugins and refine their pipeline scripts for real-world projects. Happy automating! ๐Ÿš€

10
Subscribe to my newsletter

Read articles from Suyash Bhawsar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suyash Bhawsar
Suyash Bhawsar

Tech enthusiast, DevOps learner. Arch Linux w/ KDE. Rust learner. Harmonium player. Sudoku solver. Passionate about music and technology.