Which of the following is the best reason to avoid hard-coded values in Jenkins pipeline scripts?
Think about how changing values in many places affects your work.
Avoiding hard-coded values helps you change settings in one place without editing many scripts, making maintenance easier and pipelines more flexible.
What will be the output of the following Jenkins pipeline snippet?
pipeline {
agent any
environment {
GREETING = 'Hello'
}
stages {
stage('Print') {
steps {
script {
echo "${env.GREETING}, World!"
}
}
}
}
}Check how environment variables are referenced inside double quotes.
The environment variable GREETING is set to 'Hello' and referenced correctly with ${env.GREETING}, so the output is 'Hello, World!'.
Which Jenkins pipeline snippet correctly uses a parameter to avoid hard-coded values for a branch name?
Parameters allow users to input values when starting the build.
Option D defines a parameter BRANCH and uses it in the checkout step, avoiding hard-coded branch names and allowing flexibility.
Given this Jenkins pipeline snippet, why does it fail to print the environment variable?
pipeline {
agent any
environment {
MY_VAR = 'value'
}
stages {
stage('Test') {
steps {
echo '$MY_VAR'
}
}
}
}Think about how single and double quotes work in Groovy strings.
Single quotes in Groovy do not expand variables, so $MY_VAR is printed as is. Double quotes are needed for expansion.
Which approach best avoids hard-coding sensitive secrets like passwords in Jenkins pipelines?
Consider secure storage and controlled access for secrets.
Jenkins credentials store securely manages secrets and allows pipelines to access them safely without exposing values in scripts.