How to Implement a CI/CD Pipeline with Jenkins

Jenkins automates the build, test, and deployment process. This tutorial walks you through setting up a basic CI/CD pipeline using a Jenkinsfile and a Multibranch Pipeline job.

Before starting, ensure you have Jenkins installed, a Git repository with your project code, and access to the Jenkins web UI. Configure Git credentials in Jenkins for private repos.

Article illustration

1. Create a Jenkinsfile

A Jenkinsfile is a text file that defines your pipeline using Declarative syntax. Create one in your repository root with the following structure:

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

Replace the echo commands with actual build commands (e.g., mvn clean compile for Java, or npm install for Node.js).

2. Set Up a Multibranch Pipeline

In Jenkins, create a new Multibranch Pipeline item. Point it to your Git repository, add credentials if needed, and set the branch discovery strategy (e.g., all branches). Jenkins will automatically find Jenkinsfiles in each branch and run the pipeline.

3. Enable Webhook Triggers

To trigger builds on every push, add a webhook in your Git provider. For GitHub, go to Settings > Webhooks and add the payload URL: http://YOUR_JENKINS_URL/multibranch-webhook-trigger/. Jenkins will then re-scan the repository on push events.

4. Enhance with Testing and Notifications

Make your pipeline robust: add unit tests (e.g., npm test), static code analysis (SonarQube), and deployment steps like Docker pushes or SSH transfers. Use post-build actions to send Slack or email notifications on failure.

Conclusion

You’ve built a working CI/CD pipeline with Jenkins. Start with the basics and layer on more stages as your project grows.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *