Skip to main content

Command Palette

Search for a command to run...

Building a Jenkins CICD Pipeline - Step-by-Step Guide

Updated
4 min read
Building a Jenkins CICD Pipeline - Step-by-Step Guide

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.

Updated for 2025: This guide has been revised to reflect current best practices, including Java 17/21 requirements, modern repository key management, and updated Slack integration methods.


Step 1: Installing Jenkins

Commands:

  1. Install Java (Java 17 or newer required; Java 21 recommended):

     sudo apt update
     sudo apt install fontconfig openjdk-21-jre -y
     java -version
    

    Note: Jenkins versions from mid-2024 onward require Java 17 or newer. Java 21 is recommended for optimal performance and future compatibility.

  2. Add Jenkins Repository (using modern keyring method):

     sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
     echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
    

    Note: The apt-key command has been deprecated for security reasons. This method uses isolated keyrings for safer repository key management.

  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. Ensure Git Plugin is Available:

    • The Git plugin is typically included in the "suggested plugins" bundle installed during initial setup. Check "Manage Jenkins" > "Plugins" > "Installed" to verify it's already installed.

    • If not present, navigate to "Manage Jenkins" > "Plugins" > "Available" and search for "Git Plugin," then 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 > Plugins", search for "Slack Notification," and install it.
  2. Configure Slack (using modern OAuth bot token):

    • Create a custom Slack app at https://api.slack.com/apps with required OAuth scopes (channels:read, chat:write, etc.).

    • Install the app to your workspace and copy the Bot User OAuth Token.

    • In Jenkins, go to "Manage Jenkins" > "Credentials" and add the token as a "Secret text" credential.

    • Navigate to "Manage Jenkins" > "System" > "Slack":

      • Select your credential from the dropdown
      • Enable "Custom slack app bot user"
      • Set a default channel (e.g., #builds)
      • Test the connection
    • Invite the bot to your desired Slack channel(s).

      Note: Legacy integration tokens are deprecated. Using a bot user OAuth token provides better security and supports modern Slack features.

  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!