Skip to content
Latchkey

How to Move Files Between Agents With stash and unstash in Jenkins

stash saves a named set of files on the controller so a later stage on any agent can unstash them.

Use stash name: 'x', includes: '...' in the producing stage, then unstash 'x' in a consuming stage that may run on a different agent. Stashes are scoped to a single build.

Steps

  • In the build stage, stash the artifacts you need later.
  • In a downstream stage (possibly another agent), call unstash with the same name.
  • Keep stashes small; they transit through the controller.

Jenkinsfile

Jenkinsfile
pipeline {
  agent none
  stages {
    stage('Build') {
      agent { label 'linux' }
      steps {
        sh 'make build'
        stash name: 'dist', includes: 'dist/**'
      }
    }
    stage('Deploy') {
      agent { label 'deployer' }
      steps {
        unstash 'dist'
        sh './deploy.sh dist/'
      }
    }
  }
}

Gotchas

  • Stashes live only for the current build; use archiveArtifacts to keep files after it ends.
  • Large stashes are slow and pressure the controller; prefer an artifact repository for big payloads.

Related guides

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