Complete the code to access the environment variable named 'BUILD_NUMBER' in a Jenkins pipeline.
echo "Build number is: $[1]"
The environment variable BUILD_NUMBER holds the current build number in Jenkins pipelines.
Complete the code to set a custom environment variable named 'MY_VAR' with value 'hello' in a Jenkins pipeline.
environment {
MY_VAR = '[1]'
}To set a custom environment variable, assign the desired string value. Here, MY_VAR is set to hello.
Fix the error in the code to correctly print the environment variable 'MY_VAR' in a Jenkins pipeline script.
echo "Value is: $[1]"
In Jenkins scripted pipelines, environment variables are accessed via env.VAR_NAME. So env.MY_VAR is correct.
Fill both blanks to create a map of environment variables with keys as variable names and values uppercased, filtering only variables starting with 'BUILD'.
def buildVars = env.getEnvironment().findAll { k, v -> k.[1]('BUILD') } return buildVars.collectEntries { k, v -> [k, v[2]] }
The startsWith method filters keys starting with 'BUILD'. The toUpperCase() ensures values are uppercase.
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.
stage('Check Env') { steps { script { def envVar = env.[1] if (envVar == [2]) { error('[3]') } echo "Deploying to: ${envVar}" } } }
The environment variable DEPLOY_ENV is accessed via env.DEPLOY_ENV. If it is null, the build fails with the error message.