0
0
Jenkinsdevops~5 mins

Avoiding hard-coded values in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Hard-coded values in Jenkins pipelines make it hard to change settings later and reuse code. Avoiding them helps keep pipelines flexible and easier to maintain by using variables and parameters instead.
When you want to run the same pipeline with different branch names without changing the code.
When you need to deploy to different environments like staging or production using the same pipeline.
When you want to reuse the pipeline code across multiple projects with different settings.
When you want to update a server address or credentials without editing the pipeline script every time.
When you want to make your pipeline easier to understand by clearly naming important values.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  parameters {
    string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Git branch to build')
    choice(name: 'ENVIRONMENT', choices: ['staging', 'production'], description: 'Deployment environment')
  }
  stages {
    stage('Checkout') {
      steps {
        echo "Checking out branch ${params.BRANCH_NAME}"
        git branch: "${params.BRANCH_NAME}", url: 'https://github.com/example/my-app.git'
      }
    }
    stage('Deploy') {
      steps {
        script {
          if (params.ENVIRONMENT == 'production') {
            echo 'Deploying to production server'
          } else {
            echo 'Deploying to staging server'
          }
        }
      }
    }
  }
}

This Jenkinsfile uses parameters to avoid hard-coded values.

The BRANCH_NAME parameter lets you choose which Git branch to build.

The ENVIRONMENT parameter lets you select the deployment target.

Using params.BRANCH_NAME and params.ENVIRONMENT in the script replaces fixed values with flexible inputs.

Commands
This command updates the Jenkins job with the new Jenkinsfile that uses parameters instead of hard-coded values.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command starts the Jenkins pipeline named 'my-pipeline' with parameters to build the 'feature-xyz' branch and deploy to the staging environment.
Terminal
jenkins-cli build my-pipeline -p BRANCH_NAME=feature-xyz -p ENVIRONMENT=staging
Expected OutputExpected
Started build #42 Building on master [Pipeline] Start of Pipeline [Pipeline] echo Checking out branch feature-xyz [Pipeline] git Fetching changes from https://github.com/example/my-app.git [Pipeline] echo Deploying to staging server [Pipeline] End of Pipeline Finished: SUCCESS
-p BRANCH_NAME=feature-xyz - Sets the Git branch to build
-p ENVIRONMENT=staging - Sets the deployment environment
This command runs the same pipeline but builds the 'main' branch and deploys to production, showing how parameters replace hard-coded values.
Terminal
jenkins-cli build my-pipeline -p BRANCH_NAME=main -p ENVIRONMENT=production
Expected OutputExpected
Started build #43 Building on master [Pipeline] Start of Pipeline [Pipeline] echo Checking out branch main [Pipeline] git Fetching changes from https://github.com/example/my-app.git [Pipeline] echo Deploying to production server [Pipeline] End of Pipeline Finished: SUCCESS
-p BRANCH_NAME=main - Sets the Git branch to build
-p ENVIRONMENT=production - Sets the deployment environment
Key Concept

If you remember nothing else from this pattern, remember: use parameters and variables in Jenkins pipelines to replace fixed values and make your pipeline flexible and reusable.

Common Mistakes
Hard-coding branch names or server addresses directly in the Jenkinsfile.
This makes the pipeline rigid and requires code changes for every small update or environment switch.
Use pipeline parameters and variables to pass these values when running the pipeline.
Forgetting to reference parameters with 'params.' prefix inside the Jenkinsfile.
The pipeline will not recognize the parameter values and may fail or use defaults unexpectedly.
Always access parameters using 'params.PARAMETER_NAME' syntax inside the pipeline script.
Not providing default values for parameters.
This can cause the pipeline to fail if no value is provided when starting the build.
Always set sensible default values for parameters to ensure smooth pipeline runs.
Summary
Use parameters in Jenkinsfiles to avoid hard-coded values and make pipelines flexible.
Pass values like branch names and environments as parameters when triggering builds.
Access parameters inside the pipeline script using the 'params' object.