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

Declarative jenkins pipeline do not abort on input

一个简单的问题,我在 jenkins 的声明性管道中得到了输入。当我在提示中单击中止时,我不希望它将构建标记为中止。为了防止堆栈已经有了答案,我正在声明性管道中寻找解决方案,而不是转义到脚本。


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

嗯,你可以的

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

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

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


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