Jenkins 声明式管道:在不阻塞执行器的情况下输入条件步骤

Jenkins declarative pipeline: input with conditional steps without blocking the executor

我正在尝试让以下功能在 Jenkins 的声明式管道语法中发挥作用:

这是我最终得到的结果:

pipeline {
  agent none
  stages {
    stage('1. Compile') {
      agent any
      steps {
        echo 'compile'
      }
    }
    stage('2. Build & push Docker image') {
      agent any
      when {
        branch 'master'
      }
      steps {
        echo "build & push docker image"
      }
    }
    stage('3. Deploy to stage') {
      when {
        branch 'master'
      }
      input {
        message "Deploy to stage?"
        ok "Deploy"
      }
      agent any
      steps {
        echo 'Deploy to stage'
      }
    }
  }
}

问题是第 2 阶段需要第 1 阶段的输出,但在 运行 时不可用。如果我用全局 agent any 替换各种 agent 指令,则输出可用,但执行程序在第 3 阶段被阻止等待用户输入。如果我尝试将 1 和 2 组合成一个单阶段,然后我失去了仅在 master.

上有条件地 运行 某些步骤的能力

有什么方法可以实现我正在寻找的所有行为吗?

您需要在第一步结束时使用 stash 命令,然后在需要文件时使用 unstash 命令

我认为这些可以在 snippet generator

根据 documentation

Saves a set of files for use later in the same build, generally on another node/workspace. Stashed files are not otherwise available and are generally discarded at the end of the build. Note that the stash and unstash steps are designed for use with small files. For large data transfers, use the External Workspace Manager plugin, or use an external repository manager such as Nexus or Artifactory