How does the `environment` directive work, and what values can it hold?
IntermediateConceptDeclarative pipeline syntax
Answer
environment sets environment variables for the whole pipeline or a single stage. Values must be single-quoted or double-quoted strings, or function calls such as credentials().
environment {
APP = 'shack-api'
IMAGE = "docker.io/devopsshack/${APP}"
SONAR = credentials('sonar-token')
}
To compute a value with logic, use script { env.VERSION = sh(script: 'git describe --tags', returnStdout: true).trim() } in a step. Stage-level environment overrides pipeline-level for that stage.
More on Declarative pipeline syntax
What is the minimum valid declarative pipeline?BeginnerWhat agent types exist in declarative pipelines?IntermediateWhen do you use `agent none` at the top level?IntermediateHow do nested sequential stages work and why are they useful?IntermediateList the `post` conditions and when each fires.IntermediateWhat are the most useful `when` conditions?Intermediate