0
0
Jenkinsdevops~20 mins

Avoiding hard-coded values in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Avoiding Hard-Coded Values
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why avoid hard-coded values in Jenkins pipelines?

Which of the following is the best reason to avoid hard-coded values in Jenkins pipeline scripts?

AIt makes the pipeline faster to execute.
BIt reduces the number of pipeline stages.
CIt improves pipeline flexibility and easier maintenance.
DIt ensures the pipeline runs only on specific machines.
Attempts:
2 left
💡 Hint

Think about how changing values in many places affects your work.

💻 Command Output
intermediate
1:30remaining
Output of Jenkins environment variable usage

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!"
        }
      }
    }
  }
}
A${GREETING}, World!
BHello, World!
CError: GREETING not defined
DHello World
Attempts:
2 left
💡 Hint

Check how environment variables are referenced inside double quotes.

Configuration
advanced
2:00remaining
Correct way to use parameters instead of hard-coded values

Which Jenkins pipeline snippet correctly uses a parameter to avoid hard-coded values for a branch name?

A
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        script {
          def branch = 'main'
          checkout([$class: 'GitSCM', branches: [[name: branch]], userRemoteConfigs: [[url: 'https://repo.git']]])
        }
      }
    }
  }
}
B
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://repo.git']]])
      }
    }
  }
}
C
pipeline {
  agent any
  environment {
    BRANCH = 'main'
  }
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: BRANCH]], userRemoteConfigs: [[url: 'https://repo.git']]])
      }
    }
  }
}
D
pipeline {
  agent any
  parameters {
    string(name: 'BRANCH', defaultValue: 'main', description: 'Branch to build')
  }
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: params.BRANCH]], userRemoteConfigs: [[url: 'https://repo.git']]])
      }
    }
  }
}
Attempts:
2 left
💡 Hint

Parameters allow users to input values when starting the build.

Troubleshoot
advanced
1:30remaining
Why does this Jenkins pipeline fail to use environment variable?

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'
      }
    }
  }
}
ASingle quotes prevent variable expansion, so $MY_VAR is printed literally.
BEnvironment variables must be declared inside stages, not pipeline.
CThe variable name MY_VAR is invalid in Jenkins environment block.
DThe echo step requires double dollar signs to expand variables.
Attempts:
2 left
💡 Hint

Think about how single and double quotes work in Groovy strings.

Best Practice
expert
2:00remaining
Best practice to avoid hard-coded secrets in Jenkins pipelines

Which approach best avoids hard-coding sensitive secrets like passwords in Jenkins pipelines?

AStore secrets in Jenkins credentials store and access them via credentials() function.
BEncrypt secrets manually and store them as plain text in pipeline scripts.
CHard-code secrets in environment block with restricted access.
DUse global environment variables on Jenkins master node.
Attempts:
2 left
💡 Hint

Consider secure storage and controlled access for secrets.