Search code examples
jenkinsjenkins-job-dsl

Suppress automatic SCM triggering


We are running Jenkins with Configuration as Code.

With JobsDSL we are creating MultiBranch pipelines, which works fine. But I'm having trouble configuring " Suppress automatic SCM triggering" We want to use this, as we have some "nightly" pipelines that takes quite some time to run, so they are scheduled to run every night. Outside of office working hours. We don't want anything else than the cron schedule to start it.

On previous Jenkins servers, where we didn't run Configuration As Code, we had setup Suppress automatic SCM triggering in order to ensure that commits didn't trigger these jobs.

How do we set this up for Jenkins Configuration as Code? Is it something we should define in JobDSL? Is it on the groovy for the individual pipelines?


Solution

  • I found help here: https://stackoverflow.com/a/56291979/1955317

    And we ended up doing it like this:

    for (project in jobarray) {
      for (job in project.jobs) {
        multibranchPipelineJob(job.name) {
          displayName(job.name)    
          branchSources {
            branchSource {
            ...
              strategy {
                if(job.suppressSCMTrigger) {
                  defaultBranchPropertyStrategy {
                    props {
                      noTriggerBranchProperty() // Suppress SCM triggers, Documented here: https://stackoverflow.com/a/56291979/1955317
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    And we are then defining our jobs like this:

    jobarray = [  
      ['bitbucketProjectKey': 'SomeKey', 'bitbucketRepository': 'SomeRepo',
       'jobs':[
          ['name': 'Migration Tests ', 'filepath': 'build/Jenkins/migration-maestro.groovy', 'suppressSCMTrigger': true],
        ]
      ],
    ]
    

    The entire thing is handled by JobDSL where theis groovy script is part of it's input Hopefully this can help someone in the future :)