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.

All CI/CD questions

More on Jenkins