14

According to Jenkins Pipeline Docs, I should be able to use pipeline steps while working with Docker. However, "archiveArtifacts" does not appear to work:

    def container = docker.image("some_image")
    container.inside {
        sh 'date > /tmp/test.txt'
        sh 'cat /tmp/test.txt' //works, shows file

        def fileContents = readFile '/tmp/test.txt' //works
        echo "Contents: ${fileContents}" //works, shows file

        archiveArtifacts '/tmp/*.txt' //FAILS
    }

"ERROR: No artifacts found that match the file pattern "/tmp/*.txt". Configuration error?".

Things I've tried:

  • Adding sync and sleep(5) before the archive step in case this is a file sync issue.
  • Trying to archive '/*' and '*' in case it's running on the host (same error).

Any suggestions on archiving files generated in a Docker container?

PS: I opened a bug report... It looks like archiveArtifacts will only work on files in $WORKSPACE in docker containers.

mohan08p
  • 5,002
  • 1
  • 28
  • 36
Akom
  • 1,484
  • 19
  • 25

1 Answers1

7

You seem to have found the solution as reported in the same Jira ticket so I'll post here for everyone:

this works fine:

def image = docker.image("alpine")
 image.inside { 
   sh 'date > /tmp/test.txt'
   sh "cp /tmp/test.txt ${WORKSPACE}"
   archiveArtifacts 'test.txt'
 }
rath
  • 3,655
  • 1
  • 40
  • 53
  • 1
    This workaround merely illustrates that archiving works (only) from the build workspace. I had already mentioned this in the question, but perhaps this will help someone. – Akom Sep 12 '18 at 17:17
  • @Akom it helped me, so... :) – rath Sep 12 '18 at 18:08
  • This only works as far as the previous steps in the image.inside block don't fail. I.e. for the usual case that you want to run some tests and then later archive the result, this only works as long as the tests succeed... Of course there are workaround (like set +e), but that makes this workaround even more verbose. – Kutzi Jun 13 '19 at 08:29