Hi,
Given the following Jenkinsfile I would expect two different workspaces to be printed but that is not the case. Am I doing something wrong?
def getWorkspace() {
path = "${WORKSPACE}"
sleep 1
return path
}
def build_steps = [:]
build_steps['first'] = {
node {
println getWorkspace()
}
}
build_steps['second'] = {
node {
println getWorkspace()
}
}
stage ('test') {
parallel build_steps
}
Console:
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] parallel
[Pipeline] [first] { (Branch: first)
[Pipeline] [second] { (Branch: second)
[Pipeline] [first] node
[first] Running on agent1 in /home/jenkins/workspace/test
[Pipeline] [second] node
[Pipeline] [first] {
[second] Running on agent1 in /home/jenkins/workspace/test@2
[Pipeline] [second] {
[Pipeline] [first] sleep
[first] Sleeping for 1 sec
[Pipeline] [second] sleep
[second] Sleeping for 1 sec
[Pipeline] [first] echo
[first] /home/jenkins/workspace/test@2
[Pipeline] [first] }
[Pipeline] [first] // node
[Pipeline] [first] }
[Pipeline] [second] echo
[second] /home/jenkins/workspace/test@2
[Pipeline] [second] }
[Pipeline] [second] // node
[Pipeline] [second] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Best regards
Rasmus
Ok, I know what I did wrong now.
This:
path = "${WORKSPACE}"
Should be changed to:
def path = "${WORKSPACE}"
without def, path will be a variable with script-scope making getWorkspace() return the workspace of the last caller.
Making path a local variable fixes the problem.
Best regards
Rasmus