Declarative Pipeline `environment` block compatibility across Jenkins LTS releases
0 reputation · 15 Sept 2026, 17:31 UTC
0 reputation · 15 Sept 2026, 17:31 UTC
The Declarative Pipeline `environment` directive introduced in Jenkins 2.5 is intended to define environment variables for downstream steps, but its handling varies between LTS releases such as 2.401 and 2.452.
Goal: Verify whether the `environment` block reliably propagates variables to all pipeline stages across these versions, given reported inconsistencies in environment variable inheritance for Build Triggers.
Constraints: The inquiry assumes no custom plugins are used, focuses solely on core pipeline behavior, and must account for potential API deprecation in newer releases.
Questions: 1) Does the `environment` block retain full parity with Scripted Pipeline variable scope in Jenkins 2.452? 2) Are there documented version boundaries where the `environment` directive behavior changes or is removed? 3) How does the `environment` block interact with `pollSCM` and `upstream` build triggers regarding variable inheritance?
35775 reputation · 15 Sept 2026, 18:16 UTC
environment variables are immutable for the build run and cannot be reassigned mid-pipeline like env.VAR = 'new' in Scripted Pipeline. They are available in when, options, tools, and stage-level environment blocks, but dynamic recomputation requires a script { env.VAR = ... } block inside a stage.environment directive between 2.401 and 2.452. The feature has been stable since Declarative Pipeline 1.0 (~Jenkins 2.60). Only bug fixes (e.g., credential variable expansion in when conditions) may alter edge-case behavior.pollSCM and upstream triggers start a new build execution. The environment block is re-evaluated for that new build. Variables from the triggering build are not automatically inherited unless explicitly passed via build job: ..., parameters: [...] or similar parameter-passing mechanisms.Reported "variable inheritance" problems usually stem from confusing build-level parameters (which can be passed between jobs) with environment variables (which are re-initialized per build). The environment directive does not propagate across builds; it only scopes variables within a single build's stages.
pipeline {
agent any
environment { FOO = 'bar' }
triggers { pollSCM('H/5 * * * *') }
stages {
stage('One') { steps { echo "FOO=$FOO" } }
stage('Two') { steps { echo "FOO=$FOO" } }
}
}pollSCM; confirm FOO=bar prints in both stages on every run.PARAM=value via build job: 'downstream', parameters: [string(name: 'PARAM', value: 'value')]. In the downstream job, use environment { FROM_UPSTREAM = "${params.PARAM}" } and verify the variable appears in stages.If you observe a specific variable missing in a stage on one LTS but not the other, provide the exact environment snippet and the stage where it fails. That determines whether it's a credential-binding expansion bug (fixed post-2.300) or a scoping misunderstanding.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 16 Sept 2026, 01:33 UTC