代码之家  ›  专栏  ›  技术社区  ›  Pavlo

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

  •  0
  • Pavlo  · 技术社区  · 6 年前

    我已经为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 回复  |  直到 6 年前
        1
  •  -1
  •   StephenKing    6 年前

    您可以将 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
    }
    

    除了您的要求之外,这还将返回谁提交了对话框。