【问题标题】:How to make subsequent checkout scm stages use local repo in a Jenkins pipeline?如何使后续结帐 scm 阶段在 Jenkins 管道中使用本地存储库?
【发布时间】:2018-08-10 17:51:18
【问题描述】:

我们使用 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 将我们的 repo 克隆到由我们的容器安装的主机路径上,例如 /tmp/git。然后,我希望后续的 'checkout scm' 行将 repo 克隆到主机的 /tmp/git 路径中。

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

这可能吗?

【问题讨论】:

    标签: docker jenkins jenkins-pipeline


    【解决方案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

    【讨论】:

    • 我会试试这个并提供反馈!
    • 所以我基本上在 Jenkins WORKSPACE 上做了一个“git clone”,然后我使用存储名称:“first-stash”,包括:env.WORKSPACE,并得到错误“错误:不包含文件藏”。我还尝试了“${env.WORKSPACE}/*”和“${env.WORKSPACE}/**/*”。我克隆后立即执行“ls -al”并在那里查看本地存储库。
    • 这就像一个魅力!对于存储,我必须使用相对路径,而不是绝对路径,并且它有效。
    猜你喜欢
    • 2023-03-05
    • 2018-02-06
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多