I have 2 pipeline jobs where job1 triggers job2. I want job2 to run in the same build node and workspace as job1. I've done the following
job1:
node("linux-ubuntu") {
echo "Node: $env.NODE_NAME"
ws {
"$env.WORKSPACE/test"
}
build job: "Utilities/Playground/pass-node-job2",
parameters: [
string(name: "BUILD_NODE", value: env.NODE_NAME),
string(name: "WSPACE", value: "$env.WORKSPACE/test")
],
wait: true
}
job2:
properties([
parameters([
string(name: 'BUILD_NODE', defaultValue: "linux-ubuntu", description: 'If this job is triggered by another job, it will passed in its build node.'),
string(name: "WSPACE", defaultValue: env.WORKSPACE, description: "Workspace")
])
])
node(env.BUILD_NODE) {
sh "echo node: $env.BUILD_NODE"
sh "echo wspace: $env.WSPACE"
dir(env.WSPACE) {
sh "pwd && ls"
}
}
Is there a better way to do this? I'm aware of the shared workspace plugin, but I don't think it supports pipelines.
Edit I know this is a simplified use case. To provide a context, I want to be able to still run job2 separately.
For my case, I use a Git submodule. I have several job2's
dedicated to build each child module, and have job1
to clone the Git submodule repo. So when job1
clones the repo, it'll do it on a certain build node and workspace inside that build node. Now when it triggers job2
to build the child module, I need job2
to run in the same build node and workspace where job1
cloned the Git submodule.
But I don't need to run job1
every time, and still want to be able to run job2
any time.
It seems like you try to avoid cloning the repo multiple times. If it takes too much time you could make the trick and then just clone the repo in all your job2 instances instead of tampering with workspace:
I am not sure about your motivation to do the task specifically by using shared workspace, so I may be wrong with the above suggestion. This is however an example of action in case of concrete problem.
For sure you should understand what is your desired outcome of the task. Workspace as the name says is meant to be tightly coupled with its job and passing it around doesn't seem to be great idea - just use wait:false
and everything will fall apart when job1 restarts before job2 ends.
Edit1:
I am adding concept diagram here for the situation git submodules exist in git repo 1: