89

Is there a way to save/archive multiple artifacts from the same build?

Jenkins only allows a single 'Archive the Artifacts' post build step, and grey's out the option after it has been used once.

Maybe the ArtifactsArchiver's allows multiple patterns?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
thunderbird
  • 2,715
  • 5
  • 27
  • 51
  • 2
    FYI This is not a limitation in Jenkinsfile / pipeline builds. – simon.watts Feb 06 '18 at 11:44
  • 1
    @simon.watts yep. to add for anyone finding this from google, it's totally fine to call `archiveArtifacts` multiple times, and you can even call it before the `post` step. in that case that artifact will be available when `archiveArtifacts` was called, instead of when the build is done – starwarswii Sep 06 '22 at 15:53

6 Answers6

113

You can use Ant-style pattern, e.g. target/*.jar to archive multiple artifacts.

And it is possible to use a comma separated list of patterns if your files can't be matched with one pattern, e.g. target/*.jar, target/*.war.

The ? button next to the input field reveals this info.

daspilker
  • 8,154
  • 1
  • 35
  • 49
  • sorry i think my question is not very clear..what i want is to have two different build artifacts not just one. I know i can archive multiple files using wildcards. What i want is to have two different sets of build artifacts. – thunderbird Sep 28 '15 at 15:57
  • 1
    If i am not wrong you want artifacts from 2 different builds right? If you know the name of the 2nd job and make sure that both runs of same node, then you can give path to 2nd job to archive the artifacts like **/*.jar (From same job) /home/jenkins/workspace//**/*.jar (For different build job). – vishal sahasrabuddhe Sep 28 '15 at 17:16
  • What do you mean by two different sets? Can you give an example? – daspilker Sep 29 '15 at 07:38
  • 3
    just use comma `,` as stated in answer to seperate different file sets. – Larry Cai Apr 13 '16 at 03:16
  • 52
    Just noting that the ? button does not mention comma separated list. Thanks for mentioning it :) – GreenAsJade Jun 21 '16 at 14:31
  • 3
    To archive a folder - `archiveArtifacts '/**` – Mohnish Apr 04 '18 at 20:03
  • 1
    Your answer is is correct. But, I will say that the info that jenkins shows when you click '?' is not very helpful. It does not indicate that you can separate multiple with comma. And the info about ant-style pattern is not helpful since ant is XML and this input is not. After some study of the linked page about ant I see how it's similar, but the jenkins info is less than good. – steve Jun 25 '19 at 15:11
42

You can comma separate the paths, like this:

XXX.UnitTests\bin\Release\**.* , XXX.WriteAPI.Service/bin/Release/**.*

Then you get two separate artifacts.

See http://ant.apache.org/manual/Types/fileset.html for details of the Ant Pattern syntax.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
David Lilljegren
  • 1,799
  • 16
  • 19
  • David, is there anyway we build multiple JARs for different paths within a same repo? Like the way I described here: https://stackoverflow.com/questions/44185868/jenkins-build-pipeline-to-publish-applications-jar-for-multiple-path – Kevin Ghaboosi May 25 '17 at 16:56
  • Is the space after the first pattern actually necessary/relevant? – mmigdol May 08 '19 at 17:49
  • @mmigdol Propbably you can remove the space. Note that according to the Ant documentation you can use space instead of comma. So I guess both _artifact1,artifact2_ and _artifact1 artifact2_ would be valid syntax – David Lilljegren May 10 '19 at 09:55
  • @DavidLilljegren that isn't correct, at least according to the Jenkins help text in the snippet generator: `except that "," (comma) is the only supported separator` – Max Cascone Jul 13 '21 at 02:11
12

If you want to save two different types of files like zip files and html files, then you can use

*.html,*.zip

It will help you to archive all zip files and html files in that directory.

5

No, jenkins does not provide only one artifact to save. You can use wild card pattern to same any number of artifacts, For example All Jars - **/*.jar All War - **/*.war and so on.

**/ means Any directory.

1

Copying @Steven the Easily Amused's comment on one of the bottom-ranked answers for visibility. You can just run it twice:

Notice that the step names are plural. One can run archiveArtifacts - the pipeline step - as often as desired though it's more efficient to run it once with Ant style patterns. When invoked archiveArtifacts transports the selected file(s) back to the master where they are stored. Similarly one can run copyArtifacts multiple times to select all of or a portion of the archived Artifacts.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
-1

All the answers on here show how to combine multiple file patterns into 1 artifact which is not what the OP asked for.

An example of what was asked for is to have something like a Single Page Web app build that has environment specific settings compiled into the JavaScript for QA, Staging and Production.

As you would want to deploy the same build to multiple environments, you would need 3 builds, each with it's own environment settings in it. When you deploy the archive, you would not want to deploy the contents of all 3 to each environment and extract just the content for that one environment, because it is expensive to copy 66% more than is needed each time and could be error prone.

So, it is reasonable to generate 2 or more builds into their own artifacts and deploy 1 of those artifacts, depending on the target environment.

Jenkins should support multiple artifacts, not just making 1 artifact bigger.

Antony Booth
  • 413
  • 4
  • 5
  • No, the OP said "Maybe the ArtifactsArchiver's allows multiple patterns?" so clearly he wants multiple patterns in the same build (which the other answers have covered). Also, it's a mistake to think that somehow an "artifact" is singular. So even in the case that you made up, one could build "test", "integration" and "production" artifacts in the same build, and archive them all at once with a single selector - under separate names or directories. – Steven the Easily Amused Oct 24 '20 at 21:49
  • So you put 'test', 'integration' & 'production' into 1 artifact using subfolders. How do you deploy to 'test' without first pushing the entire contents of the artifact to the agent, or without having to externally create 3 new artifacts from the one generated by Jenkins? If you could create 3 separate artifacts in Jenkins, you wouldn't need to take additional steps to break up one big artifact into the several sub-components you actually wanted. Jenkins being able to only create 1 artifact is a limitation. – Antony Booth Jan 13 '21 at 15:45
  • A job does not have to copy ALL the artifacts from another job. You can select one, or all of the artifacts using ANT style selectors as can be done when archiving them. One can, for example, `copyArtifacts filter: 'production/**', projectName: 'UPSTREAMJOB' ...` assuming production content is all saved in a single folder. Even freestyle jobs have the same selectors for the Copy Artifacts operator. You can also choose to keep the directory structure or flatten it. – Steven the Easily Amused Jan 22 '21 at 20:19
  • If you only use Jenkins to build your code and something more appropriate for release management, Jenkins is only providing 1 artifact to that release management tool, that would need to perform additional tasks of splitting the content of the 1 artifact before pushing to an agent on a target server, otherwise, it is pushing the entire contents to each agent, for the purpose of using a fraction of the artifacts content. That you can't run the archiveArtifacts command multiple times, naming the artifact as a parameter is a limitation of Jenkins. – Antony Booth Feb 16 '21 at 14:57
  • 3
    Notice that the step names are plural. One can run [archiveArtifacts](https://www.jenkins.io/doc/pipeline/steps/core/#archiveartifacts-archive-the-artifacts) - the pipeline step - as often as desired though it's more efficient to run it once with Ant style patterns. When invoked archiveArtifacts transports the selected file(s) back to the master where they are stored. Similarly one can run [copyArtifacts](https://www.jenkins.io/doc/pipeline/steps/copyartifact/#copyartifacts-copy-artifacts-from-another-project) multiple times to select all of or a portion of the archived Artifacts. – Steven the Easily Amused Feb 20 '21 at 00:33