Artifacts, stash and retention

What survives the workspace, how long, and how to trace it.

Intermediate12 min · lesson 9 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (18 files, 7 KB): artifacts.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/artifacts.tar.gz && tar -xzf artifacts.tar.gz, which creates ~/lab/artifacts/. SHA-256: 84fd3f96e262edda212c9dfdb99094f274b442a27848c67d8c646d306f1d86a7

Support asks which build produced the file a customer is running. You have a tarball and a date. The agent that built it has reused its workspace a dozen times since then, and the disk on the controller is at 94% because every build ever made is still there. Workspaces are temporary, and everything a build produces needs a deliberate decision about whether to keep it and for how long. This lesson makes those decisions explicit.

Stash and archive do different jobs

stash / unstasharchiveArtifacts
PurposePass files between stages or nodesKeep a build's output
LifetimeUntil the run ends (preserveStashes() keeps them for restarted stages)Until the build record is discarded
Stored onThe controller, for the length of the runThe controller, in JENKINS_HOME
Visible to peopleNoYes, on the build page and over REST
app/Jenkinsfile
pipeline {
agent { label 'linux' }
options { buildDiscarder(logRotator(numToKeepStr: '2')) }
stages {
stage('Package') {
steps {
sh '''
mkdir -p dist
echo "build $BUILD_NUMBER" > app.txt
tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz app.txt
(cd dist && sha256sum app.tar.gz > app.tar.gz.sha256)
'''
stash name: 'dist', includes: 'dist/*'
}
}
stage('Check in a clean workspace') {
steps {
cleanWs()
unstash 'dist'
sh 'cd dist && sha256sum -c app.tar.gz.sha256'
}
}
}
post {
success { archiveArtifacts artifacts: 'dist/*', fingerprint: true, onlyIfSuccessful: true }
cleanup { cleanWs() }
}
}

The second stage wipes the workspace and restores only what was stashed, so it checks exactly the files that later stages would see, not leftovers. archiveArtifacts runs only on success, and fingerprint: true records a hash of each file. cleanup { cleanWs() } empties the workspace at the end, so the next build on this agent starts clean and old files can't leak into it. Stashed files travel from the agent to the controller and back, so stash small things. A stash of a large directory slows every build and takes controller disk while the run lasts.

ubuntu@secopslog-cicd:~/lab/artifacts · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./seed.sh app app/ && ./newjob.sh pkg app && ./newjob.sh missing app Jenkinsfile.missing && ./run-job.sh pkg
pushed app: 207e8ac first commit job pkg reads Jenkinsfile from git://git/app.git job missing reads Jenkinsfile.missing from git://git/app.git Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/pkg + mkdir -p dist + echo build 1 + tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz app.txt + cd dist + sha256sum app.tar.gz [Pipeline] stash Stashed 2 file(s) [Pipeline] cleanWs [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Deferred wipeout is used... [WS-CLEANUP] done [Pipeline] unstash + cd dist + sha256sum -c app.tar.gz.sha256 app.tar.gz: OK [Pipeline] archiveArtifacts Archiving artifacts Recording fingerprints [Pipeline] cleanWs [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Deferred wipeout is used... [WS-CLEANUP] done Finished: SUCCESS build #1 result: SUCCESS
$ ./jk.sh '/job/pkg/lastSuccessfulBuild/api/json?tree=number,artifacts[relativePath]' | jq -c '{number, artifacts: [.artifacts[].relativePath]}'
{"number":1,"artifacts":["dist/app.tar.gz","dist/app.tar.gz.sha256"]}
$ mkdir -p out; for f in app.tar.gz app.tar.gz.sha256; do ./jk.sh /job/pkg/lastSuccessfulBuild/artifact/dist/$f -o out/$f; done; (cd out && sha256sum -c app.tar.gz.sha256)
app.tar.gz: OK

The artifact came back over REST, and its checksum still matches. Note where it lives now: on the controller, in JENKINS_HOME, regardless of the agent that built it. Big artifacts will fill your controller's disk, so production setups keep them in an artifact repository or object storage and archive only small reports and metadata in Jenkins.

Which build made this file?

Jenkins fingerprints by MD5 and treats it as an identifier, not as a security check. Given any copy of the file, you can ask which job produced it:

ubuntu@secopslog-cicd:~/lab/artifacts · Jenkins 2.580.2
$ ./jk.sh "/fingerprint/$(md5sum out/app.tar.gz | cut -d" " -f1)/api/json" | jq -c "{fileName, original: .original.name, usage: [.usage[].name]}"
{"fileName":"app.tar.gz","original":"pkg","usage":["pkg"]}

That answers the support question from the opening. It tells you what Jenkins recorded and it doesn't prove anything to an outsider, since MD5 can be forged. Proving where a file came from is the job of signatures, two lessons from now.

Retention

The pipeline sets buildDiscarder(logRotator(numToKeepStr: '2')). Run two more builds:

ubuntu@secopslog-cicd:~/lab/artifacts · Jenkins 2.580.2
$ ./run-job.sh pkg >/dev/null; ./run-job.sh pkg >/dev/null; ./jk.sh "/job/pkg/api/json?tree=builds[number]" | jq -c "[.builds[].number]"
[3,2]

Only builds 3 and 2 remain, and build 1's logs and artifacts have gone with it. The discarder runs after each build. Keep enough history to debug and audit, and no more; if your compliance rules need releases kept for years, store them outside Jenkins, since a discarder setting in one Jenkinsfile can delete them.

A missing artifact is a failure

If the path is wrong, it's better to find out now than when someone goes looking for the file:

app/Jenkinsfile.missing
pipeline {
agent { label 'linux' }
stages {
stage('Package') {
steps {
sh 'mkdir -p dist; echo oops > dist/wrong-name.txt'
archiveArtifacts artifacts: 'dist/*.tar.gz'
}
}
}
}
ubuntu@secopslog-cicd:~/lab/artifacts · Jenkins 2.580.2
$ ./run-job.sh missing
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/missing + mkdir -p dist + echo oops [Pipeline] archiveArtifacts Archiving artifacts ‘dist/*.tar.gz’ doesn’t match anything: ‘dist’ exists but not ‘dist/*.tar.gz’ ERROR: No artifacts found that match the file pattern "dist/*.tar.gz". Configuration error? Finished: FAILURE build #1 result: FAILURE
$ ./down.sh
lab stopped and its data removed

archiveArtifacts failed the build because the pattern matched nothing, which is the default and a good one. allowEmptyArchive: true turns that off, and it makes sense for optional outputs such as failure screenshots. Don't use it for the thing you're shipping.

Hands-on

In a fresh lab, run pkg twice and compare the SHA-256 of app.tar.gz from each build. Each build's artifacts have their own URL, such as ./jk.sh /job/pkg/1/artifact/dist/app.tar.gz | sha256sum. The tar flags aim for reproducible output, so what differs between the two, and why? (Look at what the Package stage writes into app.txt.) Then change that line so the content no longer depends on the build number, and compare again. Finish with ./down.sh. One way to do it is in solutions/Jenkinsfile.

What you should see: builds 1 and 2 give different hashes (ed1e2a6a… and 10981c4a… in the lab run), because app.txt contains the build number. With content that doesn't depend on it, builds 3 and 4 give the same hash, deab829f…. The tar flags pin the file order, timestamps and owners, so identical input gave identical bytes.

Remember

Stash moves files inside a run, and archive keeps them after it. Both go through the controller, so keep them small and set a discarder on every job. A fingerprint is Jenkins's own record; proof for outsiders needs a signature.

Quick check
01A later stage on a different agent needs files that an earlier stage built. Which step fits?
Incorrect — Archiving does not move them to the other agent's workspace, and the first workspace is not reachable from there.
Correct — Stash exists to carry files between stages and nodes within a run.
Incorrect — A fingerprint is a recorded hash. It does not copy any file.
Incorrect — Each agent has its own filesystem and workspace.
02A team archives 2 GB container tarballs on every build and keeps every build. Which part of the system suffers first?
Incorrect — Archived files are transferred away. Agents do not keep them permanently.
Incorrect — Archiving does not write to Git.
Incorrect — Stored files hold no executor.
Correct — Set a discarder and send large outputs to an artifact repository.
03What can a Jenkins fingerprint tell a customer's security auditor?
Correct — It helps internal traceability. Proof for outsiders needs a signature that they can verify.
Incorrect — Jenkins records no such thing in a fingerprint.
Incorrect — MD5 collisions are practical. Jenkins uses it as an identifier.
Incorrect — Fingerprinting only hashes the file. It scans nothing.

Related