How to Archive Build Artifacts in a Jenkins Pipeline
Jenkins keeps build output with archiveArtifacts, reports tests with junit, and moves files between stages with stash/unstash.
Use archiveArtifacts in a post block to keep downloadable files, junit to surface test results, and stash/unstash to hand files to another stage or agent.
Archive, report, and stash
Archive the build output, publish the JUnit report, and stash files for a later stage.
Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm ci && npm run build && npm test'
stash name: 'dist', includes: 'dist/**'
}
post {
always {
junit 'test-results/*.xml'
archiveArtifacts artifacts: 'dist/**', fingerprint: true
}
}
}
stage('Deploy') {
steps {
unstash 'dist'
sh './deploy.sh dist/'
}
}
}
}Gotchas
archiveArtifactskeeps files on the Jenkins controller for download;stashis per-build transfer between stages/agents.- Put
junit/archiveArtifactsin apost { always {} }block so reports upload even when the build fails. - Large stashes are slow - stash only what the next stage needs, not the whole workspace.
Related guides
How to Upload Build Artifacts in GitHub ActionsUpload and download build artifacts in GitHub Actions with actions/upload-artifact and download-artifact - pa…
How to Run Parallel Stages in a Jenkins PipelineRun parallel stages in a Jenkins declarative pipeline with the parallel block, plus matrix for fanning a stag…