Use special agent for whole pipeline when a condition is met

北城余情 提交于 2021-01-29 07:30:21

问题


There is declarative pipeline. In the beginning of pipeline block the agent selection is made using agent directive. Label-based selection is being conducted. Agent selected this way is the standard/default agent.

How to set for whole pipeline a special agent when certain condition is met?

The plan is to do condition check based on pipeline's one parameter >> can that work?

What are the points the chosen approach needs to address?

Current solution blueprint:

  • Groovy code prior to pipeline block

  • Mentioned groovy code sets a variable based on value of pipeline's parameter how to access pipeline's parameter from within Groovy code located out of pipeline?

  • agent section uses variable set in Groovy code matching label special agent got attached to


回答1:


Both Jenkins.io and Cloudbees do not support dynamic agent selection with Declarative Pipeline syntax. Hence adding "when" expression within agent block will not work. However, the below approach can be tried 1. Create pipeline library - with a groovy file in vars folder. Keep all the stages inside this file and parameterize the "Agent" block 2. Jenkinsfile - embed the library inside Jenkinsfile and invoke the above groovy file using call(body) syntax. Pass agent deciding parameter from this Jenkinsfile.

For library syntax, please refer this url Shared Library syntax



来源:https://stackoverflow.com/questions/55225553/use-special-agent-for-whole-pipeline-when-a-condition-is-met

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