代码之家  ›  专栏  ›  技术社区  ›  Chris F

如何使后续的签出scm阶段在Jenkins管道中使用本地repo?

  •  0
  • Chris F  · 技术社区  · 6 年前

    我们使用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 repo/image。它的设置有一个“容器装入点”的概念,我假设它是在容器可以访问的主机上装入的。

    我在看 How to mount Jenkins workspace in docker container using Jenkins pipeline 查看如何将本地路径装载到我的docker上

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nick    6 年前

    您可以将签出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