是否可以在Jenkinsfile中定义非阻塞输入步骤?

时间:2018-07-18 12:59:41

标签: jenkins groovy

我为Git分支设置了Jenkins管道,最后一个可选步骤是部署到阶段:

stage('Stage') {
    if (gitBranch != "master") {
        timeout(time: 1, unit: 'DAYS') {
            input message: "Do you want to deploy ${shortCommit} from branch ${gitBranch} to STAGE?"
        }
    }
    node {
        stage('Deploy Stage') {
            echo("Deploying to STAGE ${gitCommit}")
            sh "NODE_ENV=stage yarn lerna-run --since ${sinceSha} deploy"
        }
    }
}

问题是部署到阶段的分支是可选的,但是Jenkins直到完成才将成功代码返回给Github。

是否有任何语法将其标记为可选?

1 个答案:

答案 0 :(得分:-1)

您可以将timeout步骤与input步骤结合起来,就像我们拥有here一样:

/**
 * Generates a pipeline {@code input} step that times out after a specified amount of time.
 *
 * The options for the timeout are supplied via {@code timeoutOptions}.
 * The options for the input dialog are supplied via {@code inputOptions}.
 *
 * The returned Map contains the following keys:
 *
 * - proceed: true, if the Proceed button was clicked, false if aborted manually aborted or timed out
 * - reason: 'user', if user hit Proceed or Abort; 'timeout' if input dialog timed out
 * - submitter: name of the user that submitted or canceled the dialog
 * - additional keys for every parameter submitted via {@code inputOptions.parameters}
 *
 * @param args Map containing inputOptions and timoutOptions, both passed to respective script
 * @return Map containing above specified keys response/reason/submitter and those for parameters
 */
Map inputWithTimeout(Map args) {
    def returnData = [:]

    // see https://go.cloudbees.com/docs/support-kb-articles/CloudBees-Jenkins-Enterprise/Pipeline---How-to-add-an-input-step,-with-timeout,-that-continues-if-timeout-is-reached,-using-a-default-value.html
    try {
        timeout(args.timeoutOptions) {
            def inputOptions = args.inputOptions
            inputOptions.submitterParameter = "submitter"

            // as we ask for the submitter, we get a Map back instead of a string
            // besides the parameter supplied using args.inputOptions, this will include "submitter"
            def responseValues = input inputOptions
            echo "Response values: ${responseValues}"

            // BlueOcean currently drops the submitterParameter
            // https://issues.jenkins-ci.org/browse/JENKINS-41421
            if (responseValues instanceof String) {
                echo "Response is a String. BlueOcean? Mimicking the correct behavior."
                String choiceValue = responseValues
                String choiceKey = args.inputOptions.parameters.first().getName()
                responseValues = [(choiceKey): choiceValue, submitter: null]
            }
            echo "Submitted by ${responseValues.submitter}"

            returnData = [proceed: true, reason: 'user'] + responseValues
        }
    } catch (FlowInterruptedException err) { // error means we reached timeout
        // err.getCauses() returns [org.jenkinsci.plugins.workflow.support.input.Rejection]
        Rejection rejection = err.getCauses().first()

        if ('SYSTEM' == rejection.getUser().toString()) { // user == SYSTEM means timeout.
            returnData = [proceed: false, reason: 'timeout']
        } else { // explicitly aborted
            echo rejection.getShortDescription()
            returnData = [proceed: false, reason: 'user', submitter: rejection.getUser().toString()]
        }
    } catch (err) {
        // try to figure out, what's wrong when we manually abort the pipeline
        returnData = [proceed: false, reason: err.getMessage()]
    }

    returnData
}

除了您的要求外,还会返回提交对话框的人。