管道中的条件步骤仅根据先前 setp 的输出执行

Conditional step in pipeline executed only based on output of a previous setp

抱歉,我不是 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。这个想法是 运行 这个管道 daily/weekly

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

#!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% 确定。

有没有我可以想出类似于将 sh 的输出存储到环境变量中的示例?

感谢您的任何反馈

你可以在管道块外声明 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...
      }
    }
  }
}