How to use post steps with Jenkins pipeline on multiple agents?

依然范特西╮ 提交于 2019-12-03 18:48:25

问题


When using the Jenkins pipeline where each stage runs on a different agent, it is good practice to use agent none at the beginning:

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])
    }
  }
}

But doing this leads to Required context class hudson.FilePath is missing error message when the email should go out:

[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] }

When I change from agent none to agent any, it works fine.

How can I get the post step to work without using agent any?


回答1:


wrap the step that does the mailing in a node step:

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



回答2:


I know this is old but I stumbled on this looking for something related. If you want to run the post step on any node, you can use

    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 Says that the label may be left blank. Many times in a declarative pipeline if something is left blank this results in an error. To work around this, setting it to null will often work.



来源:https://stackoverflow.com/questions/44531003/how-to-use-post-steps-with-jenkins-pipeline-on-multiple-agents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!