Artifacts, stash and retention
What survives the workspace, how long, and how to trace it.
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: 84fd3f96e262edda212c9dfdb99094f274b442a27848c67d8c646d306f1d86a7Support 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 / unstash | archiveArtifacts | |
|---|---|---|
| Purpose | Pass files between stages or nodes | Keep a build's output |
| Lifetime | Until the run ends (preserveStashes() keeps them for restarted stages) | Until the build record is discarded |
| Stored on | The controller, for the length of the run | The controller, in JENKINS_HOME |
| Visible to people | No | Yes, on the build page and over REST |
pipeline {agent { label 'linux' }options { buildDiscarder(logRotator(numToKeepStr: '2')) }stages {stage('Package') {steps {sh '''mkdir -p distecho "build $BUILD_NUMBER" > app.txttar --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.
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:
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:
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:
pipeline {agent { label 'linux' }stages {stage('Package') {steps {sh 'mkdir -p dist; echo oops > dist/wrong-name.txt'archiveArtifacts artifacts: 'dist/*.tar.gz'}}}}
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.