【问题标题】:Jenkins' stash not working in step run with Dockerfile agentJenkins 的存储无法与 Dockerfile 代理一起运行
【发布时间】:2021-04-22 05:03:17
【问题描述】:

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

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

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

从运行中输出 sn-p,底部显示失败消息ERROR: No files included in stash ‘TARGET’

 > /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 代理很简单,但我不知道它是什么,因此我们将不胜感激。

提前谢谢你。

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-docker


    【解决方案1】:

    我将输出从根目录更改为 Docker 容器中根目录的子目录,使输出写入/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: "**"
              }
            }
          }      
        }
    ...
    
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2019-11-17
      • 2020-04-28
      • 2022-07-04
      • 2022-08-15
      • 1970-01-01
      相关资源
      最近更新 更多