9

I have a Jenkins pipeline.

In stage A, I have a step in which I need to archive or to save my artifacts because I need to reuse those in a different stage on a different slave:

    stage('Save artifacts'){
        steps {
            archiveArtifacts artifacts: '**/**/target/app*.ear'
        }
    }

Archiving seems to work. I see the artifacts in the UI when the build finishes and can download them. But how can I access/download these artifacts in a later stage?

Adam Delarosa
  • 901
  • 1
  • 10
  • 20
DenCowboy
  • 13,884
  • 38
  • 114
  • 210
  • 2
    I don't know if you can do that with archiveArtifacts or not, but my understanding is that stash/unstash might be useful in the use case you are describing. have you considered that? [link](https://stackoverflow.com/questions/43050248/correct-usage-of-stash-unstash-into-a-different-directory) – Brad Albright Sep 18 '18 at 13:45
  • Yes, but it's taking to long (too many artifacts) so I was thinking about another "solution". I do have an Artifactory but the policy here is to store snapshots which are working in it, while now I have the deploy snapshots in it (and in a later stage I download the snapshot to execute tests against it to see if it really works). – DenCowboy Sep 18 '18 at 13:47
  • 1
    Gotcha. The only other idea I would personally have is the Copy Artifact plugin. I've used it to copy artifacts from a completely different Jenkins job, but I haven't copied from within the same job, however, someone described doing it [here](https://stackoverflow.com/questions/40930718/copy-artifact-within-a-jenkins-pipeline) – Brad Albright Sep 18 '18 at 13:53
  • Did you try copying the artifacts yet using the Copy Artifacts plugin? This will work. However I doubt that it‘ll be faster than stash/unstash. You may want to zip your files before stashing then. For many small files this will speed up things. You can even write a wrapper in some global shared library which automatically does that for you. – Joerg S Sep 19 '18 at 04:31

2 Answers2

5

Instead of archiveArtifacts you should use stash and unstash. E.g.:

stage("Build") {
    steps {
        // ...
        stash(name: "ear", includes: '**/**/target/app*.ear')
    }
}

stage("Deploy") {
    steps {
        unstash("ear")
        // ...
    }
}

Not that stash does not only stash the files, but also their paths. So unstash will put the files exactly in the same places they were (e.g. my-service/target/app.ear).

Michael
  • 2,443
  • 1
  • 21
  • 21
  • 3
    Note that `stash` is not recommended for files larger than 5MB, [see here](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#stash-stash-some-files-to-be-used-later-in-the-build) – omerfarukdogan Jan 30 '19 at 08:38
  • 1
    But if the artifact that you need in the later stage has already been archived in an earlier stage of your pipeline (because required elsewhere for another use), wouldn't it be more efficient to use that archived artifact, instead of stashing/unstashing it? – clipitar Jul 30 '20 at 13:50
  • In that case maybe, but stash/unstash expresses your intend a bit better. I'm not sure what's happening under the hood, but I'd consider to do both, if the effect on performance is not too bad. – Michael Jul 30 '20 at 14:02
0

you can get artifact in this path: $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/archive/xxxx/appxx.ear

vishun
  • 79
  • 6