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

我只是一个虾纸丫 提交于 2019-12-22 09:59:50

问题


I have the following use case:

  1. Checkout/pull a certain Git revision, using written pipeline script
    (I need this because I retrieve the revision dynamically)

  2. From that revision, load a Jenkins-pipeline-file, located among the previously checked out files

  3. This file would rely on files from the same checked out revision
    (thus, from the same workspace)

Problem: The loaded Jenkins-pipeline-file gets executed in a new workspace. But it is empty. I need that file to get executed in the same old workspace.

I thought, perhaps it's because of surrounding node, because the node keyword creates workspaces, as mentioned in the docs. But when I tried to load it outside the node, Jenkins disallowed this because of "leaving the sandbox".

Note: the jenkins-pipeline-file is found and really gets executed. The problem is during the execution.


Please look at the example code:

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'
}

File: further-script-logic.jenkins

node('master') {
   // make use of certain files
   // assumption: pwd() is the *same* workspace which were checked-out before
   // problem: it's not, it's a new empty workspace
}

回答1:


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') {
    // .....
  }
}


来源:https://stackoverflow.com/questions/37209440/jenkins-pipelines-re-use-workspace-when-loading-an-external-jenkins-pipeline-sc

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