Skip to content
Latchkey

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

  • archiveArtifacts keeps files on the Jenkins controller for download; stash is per-build transfer between stages/agents.
  • Put junit/archiveArtifacts in a post { 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

Run this faster and cheaper on Latchkey managed runners. Start free →