0
0
JenkinsHow-ToBeginner · 4 min read

How to Pass Parameters to Build in Jenkins: Simple Guide

In Jenkins, you pass parameters to a build by defining build parameters in the job configuration and then triggering the build with those parameters. You can also pass parameters programmatically using the build step in a Jenkins pipeline script.
📐

Syntax

To pass parameters in Jenkins, you first define them in the job configuration or pipeline. Then you trigger the build with those parameters.

  • Declarative Pipeline: Use parameters {} block to declare parameters.
  • Trigger build with parameters: Use build job: 'job-name', parameters: [string(name: 'PARAM', value: 'value')] in pipeline scripts.
groovy
pipeline {
  agent any
  parameters {
    string(name: 'GREETING', defaultValue: 'Hello', description: 'Greeting message')
  }
  stages {
    stage('Example') {
      steps {
        echo "Message: ${params.GREETING}"
      }
    }
  }
}
💻

Example

This example shows a Jenkins declarative pipeline that accepts a string parameter called GREETING. When you run the build, you can enter a custom greeting message. The pipeline prints the message during the build.

groovy
pipeline {
  agent any
  parameters {
    string(name: 'GREETING', defaultValue: 'Hello, Jenkins!', description: 'Enter your greeting')
  }
  stages {
    stage('Print Greeting') {
      steps {
        echo "Greeting is: ${params.GREETING}"
      }
    }
  }
}
Output
[Pipeline] echo Greeting is: Hello, Jenkins!
⚠️

Common Pitfalls

Common mistakes when passing parameters in Jenkins include:

  • Not defining parameters in the job or pipeline before using them.
  • Using incorrect parameter names or case sensitivity issues.
  • Trying to pass parameters to freestyle jobs without enabling "This project is parameterized" option.
  • For scripted pipelines, forgetting to wrap parameters in the parameters list when triggering other jobs.
groovy
/* Wrong: Missing parameters block */
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        echo "Param: ${params.MY_PARAM}"
      }
    }
  }
}

/* Right: Parameters declared */
pipeline {
  agent any
  parameters {
    string(name: 'MY_PARAM', defaultValue: 'default', description: 'A parameter')
  }
  stages {
    stage('Test') {
      steps {
        echo "Param: ${params.MY_PARAM}"
      }
    }
  }
}
📊

Quick Reference

ActionSyntax / LocationDescription
Define parameterIn pipeline: parameters { string(name: 'PARAM', defaultValue: '', description: '') }Declares a build parameter
Trigger build with parametersbuild job: 'job-name', parameters: [string(name: 'PARAM', value: 'value')]Starts another job with parameters
Access parameter${params.PARAM}Use parameter value inside pipeline
Enable parameters in freestyleCheck 'This project is parameterized' in job configAllows parameter input in freestyle jobs

Key Takeaways

Always declare build parameters in your Jenkins job or pipeline before using them.
Use the parameters block in declarative pipelines to define inputs.
Access parameters inside the pipeline with ${params.PARAM_NAME}.
Enable 'This project is parameterized' for freestyle jobs to accept parameters.
Use the build step with parameters list to trigger other jobs with inputs.