jenkinsjenkins-pipelinemultibranch-pipelinenightly-build

Run nightly jobs on multibranch pipeline with declarative Jenkinsfile without deprecated feature 'Suppress automatic SCM triggering'


Jenkins ver. 2.150.3

I have a multi-branch pipeline set up. I am using a declarative Jenkinsfile. I have a set of jobs which take a long time to run. I want these to run over night for any branches which have changes.

In the past, one could use the 'Suppress automatic SCM triggering' option along with a cron trigger to achieve the nightly build for branches with changes. (See Run nightly jobs on multibranch pipeline with declarative Jenkinsfile

I no longer have access to the 'Suppress automatic SCM triggering' option.

Image of missing 'Suppress automatic SCM triggering' option

The following trigger will run even if there are no changes to the code in the branch.

triggers {
    cron('H 0 * * * *')
}

The following code runs if there are changes in the branch. However, the Jenkins multibranch project seems to trigger from the push rather than the pollSCM. This doesn't seem to achieve my desired outcome of running once nightly per branch if there are changes.

triggers {
    pollSCM('H 0 * * * *')
}

How do I configure Jenkins to achieve the nightly jobs per branch only if changes exist in that branch?


Solution

  • Adding answer from comment to here.

    You can achieve this by using the following script:

    triggers {
      pollSCM ignorePostCommitHooks: true, scmpoll_spec: 'H H * * *'
    }
    

    With directive generator (available at <yourJenkinsUrl>/directive-generator/ you can generate scripts available in your instance + see some documentation, f.e.:

    To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.