如何在jenkinsfile中定义jenkins构建触发器,以便在其他作业之后开始构建



我想在我的Jenkinsfile中定义一个构建触发器。我知道如何为BuildDiscarderProperty:

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

当另一个项目已经生成时,我如何设置启动作业的生成触发器。我在Java API文档中找不到合适的条目。

编辑:我的解决方案是使用以下代码:

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
  }
}

我只是找了同样的东西,发现这个Jenkinsfile在jenkins-infra/jenkins.io

简而言之:

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

这是一个例子:

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

post {}将在项目test1构建时执行,其中的代码

success {}只会在项目test1成功时执行。

build propagate: false, job: 'test2'将调用项目test2。

proprogate: false确保项目test1不等待项目test2的完成并简单地调用它

最新更新