Interview Prep/Jenkins/Declarative pipeline syntax

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.

All Jenkins questions Roadmap: Jenkins

More on Declarative pipeline syntax