Chris F
Chris F

Reputation: 16812

How to make subsequent checkout scm stages use local repo in a Jenkins pipeline?

We use Jenkins ECS plugin to spawn Docker containers for "each" job we build. So our pipelines look like

node ('linux') {
  stage('comp-0') {
    checkout scm
  }
  parallel(
    "comp-1": {
      node('linux') {
        checkout scm
      ...
      }
    }
    "comp-2": {
      node('linux') {
        checkout scm
      ...
      }
    }
  )
}

The above pipeline will spawn 3 containers, one for each node('linux') call.

We set up a 'linux' node in our Jenkins configuration page to tell Jenkins the Docker repo/image we want to spawn. Its setup has a notion of 'Container mount points' which I assume are mounts on the host that the container can access.

So in above pipeline, I want the "first" checkout scm to clone the our repo onto a host path mounted by our containers, say /tmp/git. I then want the succeeding 'checkout scm' lines to clone the repo in my host's /tmp/git path.

I'm looking at How to mount Jenkins workspace in docker container using Jenkins pipeline to see how to mount a local path onto my docker

Is this possible?

Upvotes: 0

Views: 2413

Answers (1)

Nick
Nick

Reputation: 2514

You can stash the code from your checkout scm step and then unstash it in subsequent steps. Here's an example from the Jenkins pipeline documentation.

// First we'll generate a text file in a subdirectory on one node and stash it.
stage "first step on first node"

// Run on a node with the "first-node" label.
node('first-node') {
    // Make the output directory.
    sh "mkdir -p output"

    // Write a text file there.
    writeFile file: "output/somefile", text: "Hey look, some text."

    // Stash that directory and file.
    // Note that the includes could be "output/", "output/*" as below, or even
    // "output/**/*" - it all works out basically the same.
    stash name: "first-stash", includes: "output/*"
}

// Next, we'll make a new directory on a second node, and unstash the original
// into that new directory, rather than into the root of the build.
stage "second step on second node"

// Run on a node with the "second-node" label.
node('second-node') {
    // Run the unstash from within that directory!
    dir("first-stash") {
        unstash "first-stash"
    }

    // Look, no output directory under the root!
    // pwd() outputs the current directory Pipeline is running in.
    sh "ls -la ${pwd()}"

    // And look, output directory is there under first-stash!
    sh "ls -la ${pwd()}/first-stash"
}

Jenkins Documentation on Stash/Unstash

Upvotes: 2

Related Questions