6

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
  }
}
user2131878
  • 161
  • 1
  • 7

2 Answers2

6

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

In short:

properties([
    pipelineTriggers([cron('H/30 * * * *')])
])
Niklaus Bucher
  • 134
  • 1
  • 6
0

This is an example:

#Project test1
pipeline {
  agent {
    any
  }
    stages {
      stage('hello') {
        steps {
            container('dind') {
              sh """
                  echo "Hello world!"
              """
          }
        }
      }
    }
    post {
      success{
        build propagate: false, job: 'test2'
      }
    }
  }

post {} will be execute when project test1 is built and the code inside

success {} will only be executed when project test1 is successful.

build propagate: false, job: 'test2' will call project test2.

propogate: false ensures that project test1 does not wait for project test2's completion and simply invokes it.