问题
I have the following use case:
Checkout/pull a certain Git revision, using written pipeline script
(I need this because I retrieve the revision dynamically)From that revision, load a Jenkins-pipeline-file, located among the previously checked out files
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.
- You have to use
{...}()
braces at the end of the caller script - 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