How to archive only the latest files in Jenkins job build

Hi There,

How can I archive only the latest generated files from the jenkin’s build.

I’m using something like below but it’s archiving all older files which were generated from older builds. But I only need latest files.

**/screenshots/*.png

Can someone help me on this.

Perhaps you should be asking, “why do you have remnant artifacts for prior build runs in your workspace?”

You can use the Workspace Cleanup plugin ( pipeline step cleanWs() ) to clean the entire workspace, or specific directories or patterns. See the plugin docs for use cases.

It can also be applied prior to beginning a build.

Then you end up archiving only the latest artifacts since that’s all that’s in the workspace.

Best practice for both maven and ant is to have a clean target which removes either all build artifacts, or the intermediary artifacts.

2 Likes

I am using cleanWS() to clean the workspace, but I am not sure if it can be used to delete artifacts… I still have artifacts archived here (which is what I want).

Archived artifacts are stored on the controller; they are copied from the workspace on the node to the controller in a folder within the build log in an archive directory:
${JENKINS_HOME}/jobs/path/jobs/to/jobs/MYJOB/builds/##/archive
or each run as a post-build-step. You can access them from the UI’s specific build job log page:
(${JENKINS_JOB_URL}/##/artifacts).

Once archived, you can delete the workspace.
If you delete the build log, you will lose the artifacts, as well as if you delete the job itself.

You can use the Copy Artifacts plugin to retrieve previously archived artifacts back into your workspace if you need them for any job’s build step.

2 Likes