Jenkins' stash not working in step run with Dockerfile agent

1.2k views Asked by At

I have been struggling with getting the Jenkins' declared pipeline to stash the results of steps that run under a Dockerfile agent. After a variety of Dockerfile / stash configurations, it continues to fail. Hopefully, someone can identify my error.

Below is the stripped-down version of the Jenkins file, which fails in the same way as the original.

Jenkinsfile:

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"
        }
      }
    }
  }
}

And the dockerfile it references based on the dotnet/sdk:5.0-alpine base image.

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

Output snippet from a run, with the failure message at the bottom 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

I know it is something simple about using a Dockerfile agent, but I cannot figure out what it is, so any help would be appreciated.

Thank you in advance.

1

There are 1 answers

0
Scott S On

I changed the output from the root directory to a subdirectory of root in the Docker container, making it so the output is written to /out/hi.txt. Next, I added a volume mount to the Dockerfile args parameter, args '--user root -v /tmp:/out'. Finally, I modified the stash command to load the file from the /tmp directory, which is shared with the /out directory inside the container.

Once these changes were made, the stash command could find the file in the /tmp directory and save it off for later steps.

...
      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: "**"
          }
        }
      }      
    }
...