詹金斯在声明式管道上的投入

时间:2020-04-23 05:29:41

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

请问这是否可行,是否可以获取用户输入并根据用户选择的内容运行脚本?逻辑应该是

如果用户选择继续,然后运行脚本(在我的情况下为提升或蓝色/绿色部署) 如果用户选择中止,然后不要终止詹金斯工作,请运行脚本(在我的情况下为回滚)

这是我正在使用的脚本,但是我不知道在验证阶段之后如何申请

  pipeline {
  agent any

  stages {
    stage('Deploy') {
      steps {
        sh """#!/bin/bash +x
          echo "performing sts assume role"
          //SCRIPT-HERE//        
          echo "performing ansible deploy"
          //SCRIPT-HERE//  
        """
      }
    }
    stage('validate') {
        steps {

            timeout(30) {
                script {
                    input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
                }
            }
        }
    }
  }

}

我看到的另一个问题是,尽管此管道脚本正在运行,但是在jenkins作业控制台输出上无法单击Proceed / Abort,这是一个错误吗?我指的是下面显示的图片

enter image description here

我能够添加nandilov的建议,但是似乎逻辑仍然不适用,请就此处遗漏的内容提出建议

pipeline {
  agent any

  stages {
    stage('Deploy') {
      steps {
        sh """#!/bin/bash +x
          echo "performing sts assume role"

          echo "performing ansible deploy"

        """
      }
    }
    stage('validate') {
        steps {
          script {
            env.flagError = "false"
              try {
              input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')

              }catch(e){
                println "input aborted or timeout expired, will try to rollback."
                env.flagError = "true"        
              }
          }
        }
    }

    stage("If user selects Proceed"){
        when{
            expression { env.inputValue == "value1" }
        }
        steps{
            sh """#!/bin/bash +x
              echo "User selected proceed"
            """
        }
    }

    stage("rollback if flag error true"){
        when{
            expression { env.inputValue == "value2" }
        }
        steps{
            sh """#!/bin/bash +x
              echo "User selected Abort"
            """
        }
    }

  }

}

从管道视图来看,在选择“中止”或“继续”时,它从未触发最后两个阶段之一

enter image description here

这些是选择中止或继续时的日志

中止

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
[Pipeline] echo
input aborted or timeout expired, will try to rollback.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

继续

Started by user lagot
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-job-lagot
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
performing sts assume role
performing ansible deploy
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (validate)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided
Proceed or Abort
Approved by lagot
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (If user selects Proceed)
Stage "If user selects Proceed" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (rollback if flag error true)
Stage "rollback if flag error true" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

2 个答案:

答案 0 :(得分:2)

如果您想中止该作业并执行某些操作,则可以尝试/抓住它:

try{
    input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')
}catch(e){
    println "input aborted or timeout expired, will try to rollback."
    // execute rollback
}

您也可以在其他阶段执行此操作:

pipeline {
  agent any

  stages {
    stage('Deploy') {
      steps {
        sh """#!/bin/bash +x
          echo "performing sts assume role"

          echo "performing ansible deploy"

        """
      }
    }
    stage('validate') {
        steps {
          script {
            env.flagError = "false"
              try {
              input(message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok: 'Proceed')

              }catch(e){
                println "input aborted or timeout expired, will try to rollback."
                env.flagError = "true"        
              }
          }
        }
    }

    stage("If user selects Proceed"){
        when{
            expression { env.flagError == "false" }
        }
        steps{
            sh """#!/bin/bash +x
              echo "User selected proceed"
            """
        }
    }

    stage("rollback if flag error true"){
        when{
            expression { env.flagError == "true" }
        }
        steps{
            sh """#!/bin/bash +x
              echo "User selected Abort"
            """
        }
    }

  }

答案 1 :(得分:1)

尝试此操作,您需要单击“继续”,然后在“部署”和“回滚”之间进行选择。结果将存储在环境变量中,您可以在后续阶段将其用作条件

    stage('validate') {
        steps {
            timeout(30) {
                script {
                    CHOICES = ["deploy", "rollback"];    
                        env.yourChoice = input  message: 'Please validate, this job will automatically ABORTED after 30 minutes even if no user input provided', ok : 'Proceed',id :'choice_id',
                                        parameters: [choice(choices: CHOICES, description: 'Do you want to deploy or to rollback?', name: 'CHOICE'),
                                            string(defaultValue: 'rollback', description: '', name: 'rollback value')]
                        } 

                }
            }
        }
    }
    stage('Deploy') {
        when {
            expression { env.yourChoice == 'deploy' }
        }
        steps {
            ...
        }
    }
    stage('Rollback') {
        when {
            expression { env.yourChoice == 'rollback' }
        }
        steps {
            ...
        }
    }