Jenkins Pipelines: Re-use workspace when loading an external Jenkins pipeline script

左心房为你撑大大i 提交于 2019-12-06 03:57:09

A kind of workaround is described here.

  1. You have to use {...}() braces at the end of the caller script
  2. You have to rewrite the called script to return a closure (a lambda)
    {-> /* former code */ }

This way, you don't "give away" the control of program flow to the executed script. Instead, you use its returned closure and "call it yourself". This prevents Jenkins from creating further workspaces.

Sadly, I have no idea, if this solution would allow multiple nodes to be declared in the caller script and/or in the called script.

I have incorporated these changes into your example code.
Look for lines marked with "<--- CHANGE".

Inlined pipeline script

node('master') {
  def latestBuildableRevision = readFile '/my/LATEST-BUILDABLE-REVISION.txt'

  checkout poll:false,
   scm:[$class:'GitSCM', branches:[[name:latestBuildableRevision]],
   doGenerateSubmoduleConfigurations:false,
   extensions:[[$class: 'CleanBeforeCheckout']], submoduleCfg:[],
    userRemoteConfigs:[[credentialsId:'...', url:'...']]]

  load 'further-script-logic.jenkins'
}()   // <--- CHANGE 1

File: further-script-logic.jenkins

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