0
0
Jenkinsdevops~20 mins

Environment directive in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline environment variable?
Consider this Jenkins pipeline snippet:
pipeline {
  agent any
  environment {
    GREETING = 'Hello'
    TARGET = 'World'
  }
  stages {
    stage('Print') {
      steps {
        script {
          echo "${GREETING}, ${TARGET}!"
        }
      }
    }
  }
}

What will be printed in the console output?
Jenkins
pipeline {
  agent any
  environment {
    GREETING = 'Hello'
    TARGET = 'World'
  }
  stages {
    stage('Print') {
      steps {
        script {
          echo "${GREETING}, ${TARGET}!"
        }
      }
    }
  }
}
AHello, ${TARGET}!
B${GREETING}, ${TARGET}!
CHello, World!
DGREETING, TARGET!
Attempts:
2 left
💡 Hint
Remember that environment variables defined in the environment block are accessible inside steps using ${VARIABLE_NAME}.
Configuration
intermediate
2:00remaining
Which environment directive syntax correctly sets a variable with a shell command output?
You want to set an environment variable CURRENT_DATE to the output of the shell command date in a Jenkins pipeline environment block. Which option correctly does this?
A
environment {
  CURRENT_DATE = "${sh 'date'}"
}
B
environment {
  CURRENT_DATE = sh(script: 'date', returnStdout: true).trim()
}
C
environment {
  CURRENT_DATE = "$(date)"
}
D
environment {
  CURRENT_DATE = sh('date')
}
Attempts:
2 left
💡 Hint
In Jenkins declarative pipeline, environment variables cannot directly run shell commands. You must use a script block or special syntax.
Troubleshoot
advanced
2:00remaining
Why does this environment variable not get set as expected?
Given this Jenkins pipeline snippet:
pipeline {
  agent any
  environment {
    PATH = '/custom/path'
  }
  stages {
    stage('Check') {
      steps {
        sh 'echo $PATH'
      }
    }
  }
}

The output shows only the default PATH, not '/custom/path'. Why?
Jenkins
pipeline {
  agent any
  environment {
    PATH = '/custom/path'
  }
  stages {
    stage('Check') {
      steps {
        sh 'echo $PATH'
      }
    }
  }
}
AThe environment directive overrides PATH, but the shell step resets PATH to default for security.
BThe shell step runs in a separate shell that does not inherit Jenkins environment variables.
CThe PATH variable is overridden but the shell uses a cached PATH ignoring Jenkins variables.
DThe environment directive overrides PATH only inside the Jenkins process, not the shell environment.
Attempts:
2 left
💡 Hint
Some environment variables like PATH are treated specially by Jenkins for security reasons.
🔀 Workflow
advanced
2:00remaining
How to pass environment variables between stages in a declarative pipeline?
You want to set an environment variable in one stage and use it in a later stage in a Jenkins declarative pipeline. Which approach works?
AUse the stash/unstash steps to pass environment variables between stages.
BSet the variable in one stage's environment block and it will automatically be available in later stages.
CUse script block to set a global variable and then assign it to environment in later stages.
DSet the variable in the environment block at pipeline level so all stages can access it.
Attempts:
2 left
💡 Hint
Environment variables defined at the pipeline level are global to all stages.
Best Practice
expert
2:00remaining
What is the best practice for securely handling sensitive environment variables in Jenkins pipelines?
You need to use a secret API key in your Jenkins pipeline environment. Which method is the best practice to handle this securely?
AStore the API key in Jenkins credentials and use the credentials binding plugin to inject it as an environment variable.
BHardcode the API key in the environment block of the Jenkinsfile for easy access.
CStore the API key in a plain text file in the repository and read it during the pipeline.
DPass the API key as a parameter to the pipeline and print it in the logs for debugging.
Attempts:
2 left
💡 Hint
Avoid exposing secrets in code or logs. Use Jenkins built-in secure storage.