微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jenkins 的存储无法与 Dockerfile 代理同步运行

如何解决Jenkins 的存储无法与 Dockerfile 代理同步运行

我一直在努力让 Jenkins 声明的管道存储在 Dockerfile 代理下运行的步骤的结果。各种Dockerfile/stash配置后,继续失败。希望有人能指出我的错误

以下是 Jenkins 文件的精简版,失败的方式与原始文件相同。

詹金斯文件

pipeline {
  agent {
    label 'Docker-enabled'
  }
  stages {
    stage('Build') {
      agent {
        dockerfile {
            filename 'cicd/docker/light.Dockerfile'
            label 'Docker-enabled'
            args '--user root'
        }
      }
      steps {
        script { 
          sh """
            echo "hello" > /hi.txt
            chmod 666 /hi.txt
            chown jenkins:jenkins /hi.txt
          """
          dir("/") {
             stash name: "TARGET",includes: "hi.txt"
          }
        }
      }      
    }
    stage('Test'){
      steps {
        script {
          echo "test"
        }
      }
    }
  }
}

它引用的 dockerfile 基于 dotnet/sdk:5.0-alpine 基础镜像。

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine
RUN adduser -D -g GECOS -u 1341 jenkins jenkins

运行的输出片段,底部显示失败消息错误:存储“目标”中不包含任何文件

 > /usr/local/bin/git rev-parse --resolve-git-dir /app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test/.git # timeout=10
 > /usr/local/bin/git config remote.origin.url https://github.com/CenturyLink/CDMMS-dotnet-core # timeout=10
Fetching upstream changes from https://github.com/CenturyLink/CDMMS-dotnet-core
 > /usr/local/bin/git --version # timeout=10
 > git --version # 'git version 2.9.5'
using GIT_ASKPASS to set credentials GitHub Creds superseding SCMAUTO
 > /usr/local/bin/git fetch --tags --progress -- https://github.com/CenturyLink/CDMMS-dotnet-core +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/local/bin/git rev-parse refs/remotes/origin/jenkins-integration3^{commit} # timeout=10
 > /usr/local/bin/git config core.sparsecheckout # timeout=10
 > /usr/local/bin/git checkout -f 0a76f8dae13c65b68110443a94491f51c57998ae # timeout=10
+ docker build -t 5eed65047d1b89e50cde2aa993e9999fedc2f078 -f cicd/docker/light.Dockerfile .
Sending build context to Docker daemon  22.94MB

Step 1/2 : FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine
 ---> ea61adf98d30
Step 2/2 : RUN adduser -D -g GECOS -u 1341 jenkins jenkins
 ---> Using cache
 ---> ba336af25d41
Successfully built ba336af25d41
Successfully tagged 5eed65047d1b89e50cde2aa993e9999fedc2f078:latest
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . 5eed65047d1b89e50cde2aa993e9999fedc2f078
.
[Pipeline] withDockerContainer
jenkinsndodc14-prod does not seem to be running inside a container
$ docker run -t -d -u 1341:1341 --user root -w /app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test -v /app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test:/app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test:rw,z -v /app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test@tmp:/app/jenkins/workspace/DevOps-Pipeline-Demos/CDMMS-test@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** 5eed65047d1b89e50cde2aa993e9999fedc2f078 cat
$ docker top 56a906f34d15b532bba7682788eda7778936f344d5a4f355f1641bf5dc160ef1 -eo pid,comm
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo hello
+ chmod 666 /hi.txt
+ chown jenkins:jenkins /hi.txt
+ ls -lsa /hi.txt
     4 -rw-rw-rw-    1 jenkins  jenkins          6 Apr 21 16:26 /hi.txt
+ cat /hi.txt
hello
[Pipeline] dir
Running in /
[Pipeline] {
[Pipeline] stash
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // script
[Pipeline] }
$ docker stop --time=1 56a906f34d15b532bba7682788eda7778936f344d5a4f355f1641bf5dc160ef1
$ docker rm -f 56a906f34d15b532bba7682788eda7778936f344d5a4f355f1641bf5dc160ef1
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
Stage "Test" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: No files included in stash ‘TARGET’
Finished: FAILURE
REST API
Jenkins 2.263.2

我知道使用 Dockerfile 代理很简单,但我无法弄清楚它是什么,因此我们将不胜感激。

提前致谢。

解决方法

我将输出从根目录更改为 Docker 容器中 root 的子目录,以便将输出写入 /out/hi.txt。接下来,我将卷挂载添加到 Dockerfile args 参数 args '--user root -v /tmp:/out'。最后,我修改了 stash 命令以从 /tmp 目录加载文件,该目录与容器内的 /out 目录共享。

完成这些更改后,stash 命令可以在 /tmp 目录中找到该文件并将其保存以供后续步骤使用。

...
      agent {
        dockerfile {
            filename 'cicd/docker/light.Dockerfile'
            label 'Docker-enabled'
            args '--user root -v /tmp:/out'
        }
      }
      steps {
        script { 
          sh """
            mkdir /out
            echo "hello" > /out/hi.txt
            chmod 666 /out/hi.txt
            chown jenkins:jenkins /out/hi.txt
          """
          dir("/tmp") {
             stash name: "TARGET",includes: "**"
          }
        }
      }      
    }
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。