How do I configure SCM Polling in a Pipeline Template?

Issue

How do I configure SCM polling when creating a pipeline template?

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline
  • Templates

Resolution

Create a new Job Template.

Set Type to be “Groovy template for pipeline”

Create a General job definition template based on Template Groovy Transformer syntax that specifies the Polling Interval.

  1. The easiest way to achieve this, is to create a regular pipeline job, configuring the polling interval
  2. Copy the contents of element from the generated config.xml. This can be accessed from the browser by navigating to /config.xml
  3. Replace with any required templating - eg if the Polling Interval is to be configurable per instance
  4. Remove the element from the copied XML as this will be substituted in by the templating engine

Create a Flow Script

First ensure that the SCM step has polling enabled -
eg: git poll: true, url: 'https://github.com/harniman/hello-world-simple-mvn'

Example 1

Configure a 5 minute polling interval for all instances of a pipeline:

General job definition:

<flow-definition plugin="workflow-job@1.8">
  <actions/>
  <description>A test pipeline</description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <triggers>
    <hudson.triggers.SCMTrigger>
    <spec>5 * * * *</spec>
    <ignorePostCommitHooks>false</ignorePostCommitHooks>
    </hudson.triggers.SCMTrigger>
  </triggers>
</flow-definition>

Pipeline Script:

node('linux') {
  git poll: true, url: 'https://github.com/harniman/hello-world-simple-mvn'
  ensureMaven()
  sh "cd hello-world-simple-mvn; mvn package"
}

def ensureMaven() {
  env.PATH = "${tool 'Maven 3.x'}/bin:${env.PATH}"
}

Example 2

Allow per job instance configuration of the polling interval

Pipeline Attributes:

ID: interval

Type: Text-field

Help: Interval will be set as the required polling interval, i.e: "5 * * * *" in the job instance

General job definition:

<flow-definition plugin="workflow-job@1.8">
  <actions/>
  <description>A test pipeline</description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <triggers>
    <hudson.triggers.SCMTrigger>
    <spec>${interval}</spec>
    <ignorePostCommitHooks>false</ignorePostCommitHooks>
    </hudson.triggers.SCMTrigger>
  </triggers>
</flow-definition>

Pipeline Script:

node('linux') {
  git poll: true, url: 'https://github.com/harniman/hello-world-simple-mvn'
  ensureMaven()
  sh "cd hello-world-simple-mvn; mvn package"
}

def ensureMaven() {
  env.PATH = "${tool 'Maven 3.x'}/bin:${env.PATH}"
}
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.