Stage‑level environment variables in Declarative Pipeline: Do they propagate to the post section?
0 reputation · 24 May 2024, 13:59 UTC
0 reputation · 24 May 2024, 13:59 UTC
In a Declarative Pipeline, the environment directive can be placed at the pipeline, stage, or step level. Pipeline‑level variables are universally available, but the spec does not state whether a variable declared inside a stage’s environment block is also visible in that stage’s post block.
The goal is to determine if the post section inherits the stage‑level scope, which affects cleanup logic that relies on those variables. Current documentation is silent, and behavior may differ between Jenkins 2.400+ and older releases.
post block?post when the variable is missing?35275 reputation · 24 May 2024, 22:30 UTC
Yes, but only on Jenkins 2.400 and later. Stage‑level environment variables are visible inside that stage’s post block starting with Jenkins 2.400. On earlier releases (2.300–2.399 and older) they are not injected, so references in post resolve to empty or undefined values.
post sections. Variables defined in stage('X') { environment { VAR = 'value' } } are available in post { always { echo "${VAR}" } }.post block runs in a narrower scope that only inherits pipeline‑level and global environment variables. Stage‑level variables are absent.The change was introduced as part of the Declarative Pipeline 1.9 / Pipeline: Declarative 2.0 work that shipped with Jenkins 2.400. It is not back‑ported.
If your post block relies on a stage‑level variable (e.g., a dynamically generated artifact path, a feature flag, or a credentials ID), the script will behave differently depending on the Jenkins version:
sh "rm -f ${ARTIFACT_PATH}" expands to rm -f (no argument), potentially deleting nothing or, worse, the current directory if the command is malformed.when { expression { return env.DEPLOY == 'true' } } inside post will always evaluate false on older versions.withCredentials([...]) that pull a secret from a stage‑level variable will fail with “variable not set” errors.Workarounds for pre‑2.400 instances include promoting the variable to pipeline level, writing the value to a file in the workspace and reading it in post, or using a shared library to stash the value in a global map.
Jenkinsfile:
pipeline {
agent any
stages {
stage('Test') {
environment {
STAGE_VAR = 'stage-value'
}
steps {
echo "In steps: ${STAGE_VAR}"
}
post {
always {
echo "In post: ${STAGE_VAR}"
}
}
}
}
}
In post: line.
In post: stage-value, propagation works (Jenkins ≥ 2.400).In post: (empty) or throws a missing‑property exception, propagation is absent (Jenkins < 2.400).What is the exact Jenkins version (core + Declarative plugin) you are targeting? The recommendation changes completely between 2.399 and 2.400, so confirming the version lets me confirm whether you can rely on propagation or must apply a workaround.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.