0
0
Jenkinsdevops~10 mins

Environment variables in builds in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the environment variable named 'BUILD_NUMBER' in a Jenkins pipeline.

Jenkins
echo "Build number is: $[1]"
Drag options to blanks, or click blank then click option'
ABUILD_NUMBER
BJOB_NAME
CBUILD_ID
DWORKSPACE
Attempts:
3 left
💡 Hint
Common Mistakes
Using JOB_NAME instead of BUILD_NUMBER
Using WORKSPACE which is the build directory path
2fill in blank
medium

Complete the code to set a custom environment variable named 'MY_VAR' with value 'hello' in a Jenkins pipeline.

Jenkins
environment {
  MY_VAR = '[1]'
}
Drag options to blanks, or click blank then click option'
Ahello
Bworld
Cgreetings
Dhi
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect value like 'world' or 'hi'
Not using quotes around the value
3fill in blank
hard

Fix the error in the code to correctly print the environment variable 'MY_VAR' in a Jenkins pipeline script.

Jenkins
echo "Value is: $[1]"
Drag options to blanks, or click blank then click option'
Aenvironment.MY_VAR
BMY_VAR
Cenv['MY_VAR']
Denv.MY_VAR
Attempts:
3 left
💡 Hint
Common Mistakes
Using just MY_VAR without env prefix
Using environment.MY_VAR which is invalid
4fill in blank
hard

Fill both blanks to create a map of environment variables with keys as variable names and values uppercased, filtering only variables starting with 'BUILD'.

Jenkins
def buildVars = env.getEnvironment().findAll { k, v -> k.[1]('BUILD') }
return buildVars.collectEntries { k, v -> [k, v[2]] }
Drag options to blanks, or click blank then click option'
AstartsWith
B.toUpperCase()
C.toLowerCase()
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains instead of startsWith
Using toLowerCase instead of toUpperCase
5fill in blank
hard

Fill all three blanks to define a Jenkins pipeline stage that prints the value of an environment variable 'DEPLOY_ENV' and fails the build if it is not set.

Jenkins
stage('Check Env') {
  steps {
    script {
      def envVar = env.[1]
      if (envVar == [2]) {
        error('[3]')
      }
      echo "Deploying to: ${envVar}"
    }
  }
}
Drag options to blanks, or click blank then click option'
ADEPLOY_ENV
Bnull
CDEPLOY_ENV is not set!
DBUILD_ENV
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name like BUILD_ENV
Comparing to empty string instead of null
Not providing an error message