Search code examples
jenkinsgroovyjenkins-pipeline

How to share a workspace between 2 Jenkins pipelines?


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.


Solution

  • 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:

    • create bare git repo on the agent machine - this will be your local intermediate repo
    • clone original repo into intermediate repo
    • modify job2 to:
      • update intermediate repo so that it is up-to-date with original - this is fast
      • clone from intermediate repo - this is fast too because you are on the same machine

    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: concept of intermediate repository