0
0
Jenkinsdevops~5 mins

Environment directive in Jenkins - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the environment directive in a Jenkins pipeline?
The environment directive is used to define environment variables that will be available to all steps within the pipeline or stage. It helps set values like paths, credentials, or flags that the build process needs.
Click to reveal answer
beginner
How do you define a simple environment variable named GREETING with the value Hello in a Jenkins declarative pipeline?
You define it inside the environment block like this:<br>
environment {
  GREETING = 'Hello'
}
Click to reveal answer
beginner
Can environment variables defined in the environment directive be used in shell script steps?
Yes, environment variables defined in the environment directive are accessible inside shell steps. For example, you can use $GREETING in a shell script to get the value.
Click to reveal answer
intermediate
What happens if you define an environment variable with the same name in both the pipeline-level and stage-level environment blocks?
The stage-level environment variable overrides the pipeline-level one for that stage only. This allows customizing variables for specific stages.
Click to reveal answer
intermediate
How can you use credentials securely with the environment directive in Jenkins?
You can use the credentials() helper inside the environment block to inject sensitive data like passwords or tokens securely. For example:<br>
environment {
  MY_SECRET = credentials('my-credential-id')
}
Click to reveal answer
Where do you place the environment directive in a Jenkins declarative pipeline?
AOutside the <code>pipeline</code> block
BOnly inside <code>steps</code>
CInside <code>post</code> block
DInside the <code>pipeline</code> or <code>stage</code> blocks
How do you reference an environment variable named VERSION inside a shell step?
AUse <code>env.VERSION</code>
BUse <code>VERSION()</code>
CUse <code>$VERSION</code>
DUse <code>#VERSION</code>
What is the correct syntax to define multiple environment variables in Jenkins pipeline?
Aenvironment { VAR1 = 'one'; VAR2 = 'two' }
Benvironment { VAR1 = 'one' VAR2 = 'two' }
Cenvironment (VAR1 = 'one', VAR2 = 'two')
Denvironment { VAR1 = 'one' VAR2 = 'two' }
If you want to use a Jenkins credential as an environment variable, which function do you use?
Acredentials()
Bsecret()
Cenv()
Dsecure()
What is the scope of environment variables defined inside a stage's environment block?
AOnly available within that stage
BAvailable globally to all stages
CAvailable only in post steps
DAvailable only in parallel stages
Explain how the environment directive works in Jenkins pipelines and how it affects the build steps.
Think about how you set variables that all commands can use during the build.
You got /4 concepts.
    Describe how to securely use credentials with the environment directive in Jenkins.
    Consider how Jenkins manages passwords or tokens safely.
    You got /4 concepts.