如何使后续的检出scm阶段在Jenkins管道中使用本地回购?

时间:2018-08-10 17:51:19

标签: docker jenkins jenkins-pipeline

我们使用Jenkins ECS插件为我们构建的“每个”作业生成Docker容器。所以我们的管道看起来像

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

上面的管道将产生3个容器,每个节点('linux')调用一个容器。

我们在Jenkins配置页面上设置了一个'linux'节点,以告知Jenkins我们要生成的Docker存储库/映像。它的设置具有“容器安装点”的概念,我认为是容器可以访问的主机上的安装。

因此,在上述管道中,我希望“第一个”签出scm将我们的存储库克隆到由我们的容器安装的主机路径上,例如/ tmp / git。然后,我希望后续的“ checkout scm”行将克隆的存储库复制到主机的/ tmp / git路径中。

我正在查看How to mount Jenkins workspace in docker container using Jenkins pipeline,以了解如何将本地路径安装到docker上

这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以在scm结帐步骤中存储代码,然后在后续步骤中将其取消存储。这是Jenkins管道文档中的示例。

// 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