Skip to content
Latchkey

How to Pass Data Between Stages in a Jenkins Pipeline

Jenkins carries values between stages via env, and files between stages or agents via stash/unstash.

Assign to env.NAME (or a script-scoped Groovy variable) to share a value across stages on the same run. To move files across stages or agents, stash then unstash.

Share a value and a file

The build stage sets an env value and stashes the artifact; the deploy stage reads both.

Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          env.APP_VERSION = sh(script: 'node -p "require(\'./package.json\').version"', returnStdout: true).trim()
        }
        sh 'npm ci && npm run build'
        stash name: 'dist', includes: 'dist/**'
      }
    }
    stage('Deploy') {
      steps {
        unstash 'dist'
        sh './deploy.sh "$APP_VERSION" dist/'
      }
    }
  }
}

Gotchas

  • Set env.NAME inside a script {} block for a computed value; a plain environment {} entry is evaluated once up front.
  • stash/unstash move files within a single build across stages/agents - they are not a cross-build cache.
  • Large stashes are slow; stash only what the downstream stage needs.

Verify it actually works

Validate the pipeline definition against the running controller before committing. Jenkins parses declarative pipelines strictly, and a syntax error surfaces as a failed build rather than a clear parse message.

Terminal
# validate a Jenkinsfile against the live controller
curl -X POST -F "jenkinsfile=<Jenkinsfile" \
  https://your-jenkins/pipeline-model-converter/validate

# replay a build with modified script to test a change without committing
# Build page -> Replay -> edit -> Run

Agent and workspace assumptions that break in CI

  • An agent label that matches no online agent leaves the build queued indefinitely rather than failing.
  • Workspaces are reused between builds by default, so stale files from a previous run can mask or cause failures. Use cleanWs() or a fresh workspace when correctness matters.
  • Tools resolved from the controller PATH are not necessarily on the agent PATH. Declare them in a tools block or install them in the pipeline.
  • Credentials bound with withCredentials are masked in logs but still visible to any process you launch; avoid passing them as command-line arguments.

Frequently asked questions

How do I pass Data Between Stages in a Jenkins Pipeline?
Assign to env.NAME (or a script-scoped Groovy variable) to share a value across stages on the same run. To move files across stages or agents, stash then unstash.
Share a value and a file?
The build stage sets an env value and stashes the artifact; the deploy stage reads both.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card