What is the difference between `environment` blocks and `withEnv`?
IntermediateConceptJenkins
Answer
The declarative environment { } directive sets variables for the whole pipeline or a single stage, evaluated when the scope starts, and supports credentials(). withEnv([...]) { } is a step that sets variables only for the enclosed block and is evaluated dynamically, which is handy in scripted code or loops. Built-in variables include BUILD_NUMBER, JOB_NAME, WORKSPACE, GIT_COMMIT, BRANCH_NAME and CHANGE_ID.
More on Jenkins
What is Jenkins and how is its architecture organised?BeginnerWhat are executors, nodes and labels in Jenkins?IntermediateWhy should you not run builds on the Jenkins controller?BeginnerHow do inbound (JNLP/WebSocket) and SSH agents connect to the controller?IntermediateWhat is a Jenkinsfile and where does it live?BeginnerDeclarative versus Scripted Pipeline: what are the differences?Intermediate