声明式 Jenkins 管道中的检查点

Checkpoint in Declarative Jenkins Pipeline

我正在查看 Cloudbees documentation 上面写着:

The correct approach is to always keep the checkpoint step outside of any node block, not associated with either an agent or a workspace

给出的示例是针对脚本管道的。我试图在声明式管道中实现它,但不断出错。我能让它工作的唯一方法是:

stage ('Promotion Checkpoint') {
    steps {
        checkpoint 'Ready for Manual intervention'
        timeout(time: 60, unit: 'SECONDS') {
            input message: 'Do you want to proceed?'
        }
    }
}

我的理解是声明式管道中的一个阶段类似于脚本式管道中的节点。我不能在阶段或步骤之外进行检查点工作,这似乎是我对 Cloudbees 的建议的解释。有人可以帮助在检查点之外正确使用吗?

您正面临声明性管道的问题,这使得应该 运行 在代理和工作区之外的事情有点混乱。

"normal" 声明式管道在顶部定义了一个代理

pipeline {
  agent any
  stages {
    stage("Build") {
      echo 'Build' 
    }
  }
}

但现在所有阶段标签都将使用相同的代理和工作区。这使得编写 "standard" 管道更容易,但不可能有不使用任何代理的命令。因此,使用 checkpoint 或编写在使用 input 时不阻塞执行程序的管道变得不可能。

因此,对于声明性管道,建议的正确方法是在顶级管道中使用 agent none,并在每个 stage 中指定 agent anyagent none

CloudBees documentation中有两个例子。您还可以在 Jenkins 文档中找到 input 的此类解决方法。

因此,使用代理开关的一种解决方案如下所示:

pipeline {
  agent none
  stages {
    stage("Build") {
      agent any
      steps {
        echo "Building"
      }
    }
    stage("Checkpoint") {
      agent none //running outside of any node or workspace
      steps {
        checkpoint 'Completed Build'
      }
    }
    stage("Deploy") {
      agent any
      steps {
        sh 'Deploying'
      }
    }
  }
} 

还有一个在声明性管道中使用 node 块。

pipeline {
  agent none
  stages{
    stage("Build"){
      // no agent defined will be solved with node
      steps{
        node('') { // this is equivalent to 'agent any'
          echo "Building"
        }
        checkpoint "Build Done"
      }
    }
    stage("Deploy") {
      agent any
      steps {
        echo 'Deploy'
      }
    }
  }
}