声明性詹金斯管道不会在输入时中止

时间:2019-05-29 21:27:06

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-declarative-pipeline

一个简单的问题,我在詹金斯的声明式管道中得到了输入。当我在提示上单击中止时,我不希望它将构建标记为已中止。为了防止堆栈已有答案,我正在声明式管道中寻找解决方案,而不必逃避编写脚本的工作。


 options {
    timeout(time: 1, unit: 'HOURS')
 }

 steps {
   input 'Deploy to UAT?'
   deploy()
 }

 post {
   aborted {
     script {
       //Throws exception(not allowed to use rawBuild)
       currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
       //Do not change status because it can only be worse
       currentBuild.result = 'SUCCESS'
       //Do not change status because it can only be worse
       currentBuild.currentResult = 'SUCCESS'
     }
   }
 }

2 个答案:

答案 0 :(得分:1)

我认为不可以使用简单的输入字段来中止管道,因为这样做的目的是。

您可以做的是在输入中使用复选框,例如


def deployToUat
steps {
    script {
        deployToUat = input(
                id: 'Proceed', message: 'Deploy to UAT?', parameters: [
                [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Proceed with deployment?']
        ])
    }
}

stage('UAT deployment') {
    when {
        expression { deployToUat == true }
    }
    steps {
        deploy()
    }
}

答案 1 :(得分:0)

好吧,你可以

script {
    try {
        input 'Deploy to UAT?'
    } catch(err) {
       currentBuild.result = 'SUCCESS'
       return
    }
}

我想以上是since the result can only worsen唯一正确的方法。

public void setResult(@Nonnull Result r) {
    if (state != State.BUILDING) {
        throw new IllegalStateException("cannot change build result while in " + state);
    }

    // result can only get worse
    if (result==null || r.isWorseThan(result)) {
        result = r;
        LOGGER.log(FINE, this + " in " + getRootDir() + ": result is set to " + r, LOGGER.isLoggable(Level.FINER) ? new Exception() : null);
    }
}