How to enable SCM polling with the Jenkins Pipeline plugin

if you wanted to use the multi-branch pipeline plugin to create jobs automatically using a Jenkinsfile there is not a way I know of to have the "Poll SCM" option enabled in the job

Nor is any needed. Multibranch projects have a configurable polling interval for the branch indexing as a whole, which also serves as a per-branch build trigger, and will also receive webhooks automatically.


In order to get my Bitbucket to connect to the web hook, I had to add the following to my declarative pipeline:

pipeline {
    stages {
        stage('Initialize') {
            steps {
                //enable remote triggers
                script {
                    properties([pipelineTriggers([pollSCM('')])])
                }
                //define scm connection for polling
                git branch: BRANCH_NAME, credentialsId: 'my-credentials', url: 'ssh://[email protected]/stash/my-project.git'
            }
        }
    }
}

This allows to rebuild a branch, without scanning the entire multibranchiverse. This is especially valuable, when using Bitbucket Project /Github Team-multibranch projects. There a scan can take a few minutes, once you have a few repos/branches.

By directly being able to hook into the branch, you can get a build result much faster, and without any side effects.

NB: In a declarative pipeline the properties call has to be wrapped by a script-block.


I am thinking about the same problem.

If you are using a online Git service like Github or Bitbucket, I think you could use their Webhooks features to solve it. I have not been able to test the solution yet, but it should work.

In your Multibranch Pipeline configuration, enable the Trigger builds remotely option.

Then you need to enable your Github/Bitbucket Webhook on your repository, using the path (as described in the Jenkins configuration descrition): JENKINS_URL/job/test/build?token=TOKEN_NAME


To answer the question how to enable SCM polling, you need to do the following.

Using the Pipeline Syntax generator and "properties: Set job properties" you can generate the following which will enable SCM polling.

properties([pipelineTriggers([pollSCM('H * * * *')])])

However as Jesse Glick points out for Multibranch pipelines you don't need to enable SCM polling.