0
0
Jenkinsdevops~10 mins

Avoiding hard-coded values 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 use a parameter instead of a hard-coded value in Jenkins pipeline.

Jenkins
pipeline {
  agent any
  parameters {
    string(name: 'BRANCH', defaultValue: '[1]', description: 'Git branch to build')
  }
  stages {
    stage('Build') {
      steps {
        echo "Building branch: ${params.BRANCH}"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Amain
Bdevelop
Cmaster
Dfeature
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed branch name directly in the steps instead of a parameter.
Not defining the parameter in the pipeline.
2fill in blank
medium

Complete the code to read the environment variable instead of hard-coding the Docker image name.

Jenkins
pipeline {
  agent any
  environment {
    IMAGE_NAME = '[1]'
  }
  stages {
    stage('Build') {
      steps {
        echo "Using image: ${env.IMAGE_NAME}"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
ADOCKER_IMAGE
Bdocker_image
CimageName
DdockerImage
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase for environment variable names.
Hard-coding the image name directly in the steps.
3fill in blank
hard

Fix the error in the code by replacing the hard-coded timeout value with a parameter.

Jenkins
pipeline {
  agent any
  parameters {
    string(name: 'TIMEOUT_MINUTES', defaultValue: '30', description: 'Timeout in minutes')
  }
  stages {
    stage('Test') {
      steps {
        timeout(time: [1], unit: 'MINUTES') {
          echo 'Running tests'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
ATIMEOUT_MINUTES
Bparams.TIMEOUT_MINUTES
Cenv.TIMEOUT_MINUTES
Dtimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Using the parameter name directly without 'params.' prefix.
Using environment variable syntax instead of parameters.
4fill in blank
hard

Fill both blanks to replace hard-coded credentials with Jenkins credentials binding.

Jenkins
pipeline {
  agent any
  environment {
    USERNAME = credentials('[1]')
    PASSWORD = credentials('[2]')
  }
  stages {
    stage('Deploy') {
      steps {
        echo "Deploying with user ${env.USERNAME}"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
ADEPLOY_USER
BDEPLOY_PASS
Cdeploy_user
Ddeploy_pass
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect credential IDs.
Hard-coding username and password directly in the pipeline.
5fill in blank
hard

Fill all three blanks to create a map of environment variables avoiding hard-coded values.

Jenkins
pipeline {
  agent any
  environment {
    vars = [
      '[1]': params.BRANCH,
      '[2]': env.DOCKER_IMAGE,
      '[3]': credentials('DEPLOY_PASS')
    ]
  }
  stages {
    stage('Print Vars') {
      steps {
        echo "Branch: ${vars.BRANCH_NAME}, Image: ${vars.IMAGE_NAME}, Password: ${vars.PASSWORD}"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
ABRANCH_NAME
BIMAGE_NAME
CPASSWORD
DDEPLOY_PASS
Attempts:
3 left
💡 Hint
Common Mistakes
Using credential IDs as keys instead of descriptive names.
Hard-coding values instead of using parameters or environment variables.