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,
stashthe artifacts you need later. - In a downstream stage (possibly another agent), call
unstashwith 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
archiveArtifactsto keep files after it ends. - Large stashes are slow and pressure the controller; prefer an artifact repository for big payloads.
Related guides
How to Set the Build Display Name and Description in JenkinsRename a Jenkins build and set its description from the pipeline with currentBuild.displayName and currentBui…
How to Use the environment Block in a Jenkins PipelineSet environment variables in a Declarative Jenkins pipeline with the environment block at pipeline or stage s…