How to Create a Jenkins CI CD Pipeline

Published:27 December 2021 - 5 min. read

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Did you know that Jenkins has built-in support for Continuous Integration/Continuous Delivery CI/CD? Implementing a Jenkins CI CD pipeline with your project can significantly improve your software development process.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

Jenkins is one of the most popular DevOps automation tools to date. And if you’re looking to get started with the Jenkins CI CD pipeline, you’ve come to the right place.

Read on, and in this tutorial, you’re going to learn how to set up a Jenkins CI CD pipeline with GitHub.

Prerequisites

This tutorial assumes you have the following requirements to follow along with the examples.

You must already have a Jenkins installation. The examples in this tutorial will be using a Jenkins v2.319.1 on a Windows 10 20H2 computer.

  • Your Jenkins web console must be accessible over the internet.
  • A Github account to which you’ll connect with the Jenkins CI CD pipeline.
  • Git SCM. This tutorial uses the latest (2.34.1) 64-bit version of Git for Windows.

Preparing the GitHub Repository

This tutorial involves integrating a Jenkins CI CD pipeline with a GitHub repository. Instead of creating a blank project, forking an existing repository is quicker to get started.

1. First, open a browser and log in to your GitHub account.

2. Next, navigate to the example repository URL (MyShuttle2 application from Microsoft Connect 2016). Once you’re on the repo page, click the Fork button near the top-right section of the page.

Forking a repo
Forking a repo

Wait for the forking process to finish. By the end, you will have the repository named “username/MyShuttle2” on your GitHub account to which you have full access.

3. Now, add a new file to the repository named Jenkinsfile. A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. This file should be inside the source code repository.

On your repository page, click Add file —> Create new file.

Adding a new file
Adding a new file

4. Type in Jenkinsfile as the name of the new file. Next, copy the code below and paste it into your Jenkinsfile.

  • The term pipeline is a set of instructions written in code for continuous delivery.
  • A stage block contains a series of steps in a pipeline, visualizing the Jenkins pipeline process.
  • A step is a single task that executes a specific process at a defined time. A pipeline contains a series of steps.

This Jenkinsfile is only a basic example of a pipeline script. In production, pipeline scripts will be more complex and include multiple actions on one or all stages.

pipeline {
    agent any

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

After writing the code, your file will look like the screenshot below.

Jenkinsfile pipeline script
Jenkinsfile pipeline script

5. Scroll to the bottom of the page and click Commit new file.

Committing the new file
Committing the new file

You now have a Jenkinsfile on your repository.

New Jenkinsfile on the GitHub repo
New Jenkinsfile on the GitHub repo

6. Finally, copy the repository URL. To do so, click Code and click the Copy button. You will need the resulting URL during the Jenkins CI CD pipeline creation.

Copying the repository URL
Copying the repository URL

Creating a Jenkins CI CD Pipeline

Developing a feature involves many code changes. Between every code change, there are a set of tasks to perform. These tasks may include committing changes, compiling, testing and packaging.

Automation services such as Jenkins help make these steps fast and error-free.

1. Open a browser, navigate to your Jenkins URL, and log in.

2. On the Jenkins Dashboard, click the New Item option on the left-hand side menu.

Creating a new item in Jenkins
Creating a new item in Jenkins

3. Enter the name of the new pipeline. For this tutorial, you will name it demoPipeline and select the Pipeline template. Click OK to finish.

Creating a Pipeline project
Creating a Pipeline project

4. Check the GitHub project box under the General tab on the configuration page. Next, paste the repository URL you copied earlier into the Project URL box.

Specifying the GitHub repository URL
Specifying the GitHub repository URL

5. Scroll down to the Build Triggers section and check the GitHub hook trigger for GITScm polling box, like the screenshot below.

Specifying the build trigger
Specifying the build trigger

6. Next, scroll further down to the Pipeline section and select/specify the following values:

  • Definition: Pipeline script from SCM
  • SCM: Git
  • Repository URL: Your repository URL
Specifying the pipeline values
Specifying the pipeline values

7. Scroll to the bottom of the page and confirm that the Script Path value is Jenkinsfile. This value references the Jenkinsfile you created earlier while preparing your Github repo. Finally, click Save.

Saving the pipeline configuration
Saving the pipeline configuration

Configuring a Webhook in GitHub

For Jenkins to run a new job, you must first create a webhook in the GitHub repository. This webhook will notify Jenkins as soon as a new push occurs in the repository.

Follow the steps below to create a Webhook.

1. Navigate to the Settings page and click the Webhooks tab on your GitHub repository. On the Webhooks page, click Add webhook.

Adding a webhook
Adding a webhook

2. Specify your Jenkins URL and append /github-webhook/in the Payload URL field. For example, HTTP://jenkinsURLhere/github-webhook/.

Also, change the Content type value to application/json.

Setting the webhook URL and content type
Setting the webhook URL and content type

3. On Which events would you like to trigger this webhook selection, click the Let me select individual events option.

Selecting individual events
Selecting individual events

4. Scroll down and check the Pushes and Pull requests checkboxes. These options will ensure that GitHub will send a payload to Jenkins during these specific events.

Enabling Pushes and Pull requests events
Enabling Pushes and Pull requests events

5. Validate the webhook by clicking on the Add webhook button at the bottom. If the webhook validation went well, you’d see a notification on the top of the page similar to the screenshot below.

Webhook creation notification
Webhook creation notification

Executing and Triggering the Jenkins CI CD Pipeline Job

Now that you’ve created and configured your pipeline, how do you know that the pipeline works? Jenkins provides a Pipeline Stage View plugin to help you view the pipeline status.

1. To open the pipeline status, open the pipeline in Jenkins and click on Status. As you can see below, the Stage View status shows No data available because there hasn’t been an event that would trigger the pipeline.

Jenkins pipeline stage view
Jenkins pipeline stage view

2. To generate initial build data, click Build Now and wait for the build process to complete. As you can see below, the Stage View now has one entry, saying that there are no changes in this build.

To generate initial build data, click Build Now
To generate initial build data, click Build Now

3. Now, test the pipeline by committing a new dummy file to the repository. To do so, go back to your GitHub repository and click click Add File —> Create new file.

Enter dummy as the new file’s name and make sure to add content.

Creating a new file
Creating a new file

Finally, click Commit new file at the bottom of the page.

4. Go back to your Jenkins pipeline status page, and you should see a new build entry with one commit, as shown below.

Jenkins pipeline build status
Jenkins pipeline build status

Conclusion

Throughout this tutorial, you have learned how to set up a Jenkins CI CD pipeline to automate the software development lifecycle. You have also learned how to use Jenkins to maintain a chain of actions for CI in a software project.

Do you think the Jenkins CI CD pipeline is a strategy you would apply to your projects? Or is there another one you already have in place that you believe is a better CI/CD solution?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!