【问题标题】:Conditional step in pipeline executed only based on output of a previous setp管道中的条件步骤仅基于前一个 setp 的输出执行
【发布时间】:2021-01-28 22:14:12
【问题描述】:

对不起,我不是 Jenkins 管道方面的专家,但也许有人可以指出正确的方向

我正在尝试做与这篇文章类似的事情,但我还没有弄清楚。

Conditional step in a pipeline

How to run a conditional step in Jenkins only when a previous step fails

https://www.jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/

所以我想要实现的是以下。

我有一个脚本可以获取一些文件并将它们存储在我的项目中 我想在第二阶段运行它,以便在获取的文件中有一些更改时创建一个 PR。这个想法是每天/每周运行这个管道

所以我正在尝试做这样的事情:

#!groovy

pipeline {
  stages {
    stage('Definitions updated') {
      steps {
        sh "./gradlew updateDefinitions"
        gitStatus = sh(returnStdout: true, script: 'git status').trim()
        ## how to expose gitStatus to the outside
      }
    }
    stage ('Create PR') {
      when {
        // Only say hello if a "status returned something"
        ## how to use the gitStatus to check against a certain output
        expression { SOMETHING == 'SOMETHING'' }
      }
      steps {
        sh "git add ."
        etc...
      }
    }
  }
}

有些我不确定如何将 sh 命令中的某些内容存储到我的环境变量中,以便稍后在下一步的条件下使用它。

我也不知道我是否正确理解这是否会并行运行,我希望所有阶段都是连续的,但我不能 100% 确定。

有没有什么例子可以让我想出类似于 out 的东西来将 sh 的输出存储到环境变量中?

感谢您的反馈

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    您可以在管道块之外声明 gitStatus,如下所示

    def gitStatus
    
    pipeline {
      stages {
        stage('Definitions updated') {
          steps {
            sh "./gradlew updateDefinitions"
            gitStatus = sh(returnStdout: true, script: 'git status').trim()
            ## how to expose gitStatus to the outside
          }
        }
        stage ('Create PR') {
          when {
            // Only say hello if a "status returned something"
            ## how to use the gitStatus to check against a certain output
            expression { gitStatus == 'SOMETHING'' }
          }
          steps {
            sh "git add ."
            etc...
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢,实际上我意识到我可以在我的环境块中定义值,当我后来尝试重新分配它时它给了我一个错误。但是关于在表达式中比较字符串的最基本问题让我发疯了“hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: gitStatus for class: WorkflowScript”
    • 它应该可以在没有在外部定义的情况下工作。由于没有def 关键字的变量定义被视为全局变量。在声明式管道中,它可能必须放在脚本块中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2023-04-08
    相关资源
    最近更新 更多