一次失败,如何用不同的参数值再次触发同一作业?

时间:2019-03-06 06:32:11

标签: jenkins jenkins-pipeline jenkins-plugins

当失败时,我需要使用不同的参数值再次触发相同的作业。但是如果由于失败而触发运行,我不想触发相同的触发。我该如何处理?

2 个答案:

答案 0 :(得分:1)

你可以做

try {
    build job: 'PayloadJob', parameters: [
              //some param
          ]
} catch (Exception e) {
    build job: 'PayloadJob', parameters: [
              //other param
          ]
}

如果您需要触发自身,只需设置一些额外的参数来确定您是在失败后开始的。

答案 1 :(得分:-1)

此示例显示如何使用指定是否重新构建的参数来触发同一作业
,或者如果由于某种原因构建失败,则触发该作业。

pipeline{
    agent any
    stages{
        stage('do something'){
            steps{
                script{
                    if(rebuild == "no"){
                        // this is to demonstrate how it's work.
                        currentBuild.result = 'FAILURE'
                    }else{
                        println "success"
                    }
                }
            }
        }
    }
    post {
        failure {
            // To prevent endless loop in case the nested build fails,
            // trigger nested job only if it's not a rebuild . 
            if(rebuild == "no"){
                println "On failure"
                build job:"TestStep" ,
                     propagate: true, //Set current job to failure if nested job fail. 
                     parameters:[string(name:'rebuild ',value:"yes")]
            }
        }
    }
}