How to define jenkins build trigger in jenkinsfile to start build after other job

user2131878 picture user2131878 · Oct 9, 2016 · Viewed 8k times · Source

I would like to define a build trigger in my Jenkinsfile. I know how to do it for the BuildDiscarderProperty:

properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '50', artifactNumToKeepStr: '20']]])

How can I set the Build Trigger that starts the job, when another project has been built. I cannot find a suitable entry in the Java API docs.

Edit: My solution is to use the following code:

stage('Build Agent'){
  if (env.BRANCH_NAME == 'develop') {
    try {
        // try to start subsequent job, but don't wait for it to finish
        build job: '../Agent/develop', wait: false
    } catch(Exception ex) {
        echo "An error occurred while building the agent."
    }
  }
  if (env.BRANCH_NAME == 'master') {
    // start subsequent job and wait for it to finish
    build '../Agent/master', wait: true
  }
}

Answer

Niklaus Bucher picture Niklaus Bucher · Jan 23, 2017

I just looked for the same thing and found this Jenkinsfilein jenkins-infra/jenkins.io

In short:

properties([
    pipelineTriggers([cron('H/30 * * * *')])
])