Jenkins

Using Jenkins Triggers

A Jenkins build trigger refers to a means by which a build is automatically started based on specific events. Jenkins offers a wide range of build triggers allowing you to choose the most efficient and suitable for your needs.

An example of Jenkins triggers include:

  1. Periodic build – Periodic builds are a build scheduled at a specific interval. For example, a build can run every hour, minute, day, etc.
  2. SCM polling –SCM polling is another type of build trigger in Jenkins. In this trigger type, Jenkins will continuously query the SCM manager for new commits; if a new commit is found, Jenkins will trigger a new build; otherwise, no action is taken.
  3. Upstream build completion – This trigger type is used to start a build automatically after completing another build in the same or different Jenkins instances.
  4. SCM Push Notification – In this type of trigger, the SCM is responsible for notifying Jenkins when a new commit is added to the repository. This prevents Jenkins from polling the SCM for any changes and waits for a notification from the SCM itself.

Using the Jenkins plugin API, Jenkins also allows you to define your trigger type. But, again, you can check the documentation to learn more.

This tutorial will cover the basics of working with Jenkins build triggers by discussing how to integrate the SCM Polling trigger type.

Jenkins Setup SCM Polling Trigger

The first step is to create a pipeline. For this tutorial, we will be using a simple Git Repository with nothing but a readme file. We will then create a simple Jenkinsfile with nothing but a hello world string, as shown:

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Once the Jenkinsfile is added to the repository, we can set up a Jenkins job.

On the Jenkins Dashboard, select “New Item” to add a new Jenkins Job.

Provide your job’s name and choose the Job type as “Multibranch Pipeline.”

Add the branch sources such as GitHub, self-hosted Git Server, Git lab, etc. You can check our tutorial on how to configure this to learn more.

To set up the SCM Polling trigger on the Jenkins pipeline, scroll down and locate the “Scan Multibranch Pipeline Triggers.”

Choose the interval at which Jenkins will poll the SCM server for any changes to the repository.

Once configured, click on Save. You can then run the build for the first time.

To test the trigger functionality, create a commit on the target repository. Once Jenkins scans the storage and finds a new commit, it will trigger a new build.

Conclusion

In this article, you discovered how to create an SCM Polling Build Trigger in Jenkins to build a repository after a new commit automatically.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list