0
0
Jenkinsdevops~10 mins

Avoiding hard-coded values in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Avoiding hard-coded values
Start Jenkins Pipeline
Define Parameters or Variables
Use Variables in Pipeline Steps
Run Pipeline with Dynamic Values
Output Results Using Variables
End Pipeline
This flow shows how Jenkins pipelines avoid hard-coded values by defining variables or parameters first, then using them dynamically in pipeline steps.
Execution Sample
Jenkins
pipeline {
  agent any
  parameters {
    string(name: 'GREETING', defaultValue: 'Hello', description: 'Greeting message')
  }
  stages {
    stage('Example') {
      steps {
        echo "${params.GREETING}, Jenkins!"
      }
    }
  }
}
A Jenkins pipeline that uses a parameter GREETING instead of a hard-coded greeting message.
Process Table
StepActionVariable/ParameterValueOutput
1Start pipeline---
2Read parameter GREETINGparams.GREETINGHello (default)-
3Execute echo stepparams.GREETINGHelloHello, Jenkins!
4Pipeline ends---
💡 Pipeline ends after executing all stages using dynamic parameter values instead of hard-coded strings.
Status Tracker
Variable/ParameterStartAfter Step 2After Step 3Final
params.GREETINGundefinedHello (default)HelloHello
Key Moments - 2 Insights
Why do we use parameters like params.GREETING instead of writing 'Hello' directly?
Using parameters lets us change the greeting without editing the pipeline code. As seen in step 2 and 3 of the execution table, the value can be set dynamically, making the pipeline flexible.
What happens if we change the parameter value when running the pipeline?
The pipeline uses the new value instead of the default. This is shown in the execution table where params.GREETING holds the value used in the echo step, allowing different outputs without code changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of params.GREETING at Step 2?
A"Goodbye"
B"Hello (default)"
C"Jenkins"
Dundefined
💡 Hint
Check the 'Variable/Parameter' and 'Value' columns at Step 2 in the execution table.
At which step does the pipeline output the greeting message?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in the execution table to find when the message is printed.
If we change the parameter GREETING to 'Hi' before running, what changes in the execution table?
ANo change, it stays 'Hello'
BPipeline fails because of unknown value
CValue at Step 2 and Step 3 changes to 'Hi', output changes to 'Hi, Jenkins!'
DOutput changes but variable value stays 'Hello'
💡 Hint
Refer to how params.GREETING value affects the output in the execution table and variable tracker.
Concept Snapshot
Avoid hard-coded values in Jenkins pipelines by using parameters or variables.
Define parameters in the pipeline block.
Use ${params.PARAM_NAME} to access values dynamically.
This makes pipelines flexible and easier to maintain.
Change values at runtime without editing code.
Full Transcript
This Jenkins pipeline example shows how to avoid hard-coded values by defining a parameter named GREETING with a default value 'Hello'. The pipeline reads this parameter at runtime and uses it in the echo step to print a greeting message. The execution table traces each step: starting the pipeline, reading the parameter, executing the echo command with the parameter value, and ending the pipeline. The variable tracker shows how params.GREETING changes from undefined to the default value and is used in the output. Key moments clarify why using parameters is better than hard-coding and how changing parameter values affects the pipeline output. The visual quiz tests understanding of parameter values at different steps and the effect of changing parameters before running the pipeline.