0
0
Jenkinsdevops~20 mins

Environment variables in builds in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Jenkins environment variable usage in shell step
In a Jenkins pipeline, you have set an environment variable MY_VAR with value hello. What will be the output of the following shell step?
sh 'echo $MY_VAR'
Jenkins
pipeline {
  agent any
  environment {
    MY_VAR = 'hello'
  }
  stages {
    stage('Print') {
      steps {
        sh 'echo $MY_VAR'
      }
    }
  }
}
ASyntaxError
B$MY_VAR
Cnull
Dhello
Attempts:
2 left
💡 Hint
Remember how shell variables are accessed inside shell steps in Jenkins.
🧠 Conceptual
intermediate
1:30remaining
Scope of environment variables in Jenkins pipeline
Which statement about environment variables in Jenkins declarative pipelines is true?
AEnvironment variables set inside a <code>script</code> block are automatically global.
BEnvironment variables set in the <code>environment</code> block are available to all stages and steps.
CEnvironment variables cannot be accessed in <code>sh</code> steps.
DEnvironment variables must be declared in the Jenkins system configuration to be used in pipelines.
Attempts:
2 left
💡 Hint
Think about where you declare environment variables in a declarative pipeline.
Troubleshoot
advanced
2:00remaining
Why does a Jenkins shell step not see a custom environment variable?
You set an environment variable MY_VAR inside a script block using env.MY_VAR = 'test'. But when you run sh 'echo $MY_VAR' in the next step, it prints empty. Why?
ABecause environment variables set in <code>script</code> blocks are not automatically exported to shell steps.
BBecause <code>sh</code> steps cannot access any environment variables.
CBecause <code>env.MY_VAR</code> must be declared in the <code>environment</code> block to be visible.
DBecause shell steps require variables to be passed as parameters explicitly.
Attempts:
2 left
💡 Hint
Consider how Jenkins passes environment variables to shell steps.
🔀 Workflow
advanced
2:00remaining
Passing environment variables between Jenkins pipeline stages
You want to set an environment variable VERSION in one stage and use it in a later stage. Which approach works correctly?
ASet <code>env.VERSION = '1.0'</code> in the first stage inside a <code>script</code> block; it will be available in later stages.
BUse the <code>environment</code> block at the pipeline level and update <code>VERSION</code> dynamically in the first stage.
CWrite the variable to a file in the first stage and read it in the next stage.
DSet <code>VERSION</code> as a global variable in Jenkins system configuration.
Attempts:
2 left
💡 Hint
Think about how Jenkins pipeline stages run in separate processes.
Best Practice
expert
2:30remaining
Secure handling of sensitive environment variables in Jenkins builds
Which is the best practice to handle sensitive environment variables like passwords in Jenkins pipelines?
AStore sensitive variables in Jenkins Credentials and inject them using the <code>credentials()</code> helper.
BHardcode sensitive variables directly in the <code>environment</code> block for easy access.
CPrint sensitive variables in console logs for debugging purposes.
DStore sensitive variables in plain text files inside the workspace.
Attempts:
2 left
💡 Hint
Think about security and Jenkins features for secrets.