如何在多个代理上使用Jenkins管道的后续步骤?

时间:2017-06-13 20:32:09

标签: jenkins jenkins-pipeline

当使用Jenkins管道时,每个阶段在不同的代理上运行,good practice在开头使用agent none

pipeline {
  agent none
  stages {
    stage('Checkout') {
      agent { label 'master' }
      steps { script { currentBuild.result = 'SUCCESS' } }
    }
    stage('Build') {
      agent { label 'someagent' }
      steps { bat "exit 1" }
    }
  }
  post {
    always {
      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "test@test.com", sendToIndividuals: true])
    }
  }
}

但是,当电子邮件发出时,这样做会导致Required context class hudson.FilePath is missing错误消息:

[Pipeline] { (Declarative: Post Actions)
[Pipeline] step
Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
[Pipeline] error
[Pipeline] }

当我从agent none更改为agent any时,它可以正常工作。

如何在不使用post的情况下让agent any步骤正常工作?

2 个答案:

答案 0 :(得分:21)

step步骤包裹发送邮件的node

post {
  always {
    node('awesome_node_label') {
      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "test@test.com", sendToIndividuals: true])
    }
  }
}

答案 1 :(得分:1)

我知道这很老了,但是我偶然发现了与此相关的东西。如果要在任何节点上运行发布步骤,可以使用

    post {
      always {
        node(null) {
          step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "test@test.com", sendToIndividuals: true])
        }
      }
    }

https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node说标签可能留为空白。在声明性管道中,如果很多东西留空,则会导致错误。要解决此问题,通常将其设置为null。