Problem Statement
Explain pipeline parameters in Jenkins. Include different parameter types, usage, and building flexible, reusable pipelines with parameters.
Explanation
Parameters make pipelines flexible by accepting user input at build time. Define parameters in parameters block (Declarative) or properties block (Scripted). Parameter types include string (text input), text (multi-line text), booleanParam (checkbox), choice (dropdown), password (masked input), file (file upload), and others.
Declarative parameter definition:
```groovy
pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '1.0.0', description: 'Version to deploy')
choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'prod'], description: 'Target environment')
booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests?')
text(name: 'CHANGELOG', defaultValue: '', description: 'Changelog')
}
stages {
stage('Deploy') {
steps {
echo "Deploying version ${params.VERSION} to ${params.ENVIRONMENT}"
script {
if (params.RUN_TESTS) {
sh 'npm test'
}
}
sh "./deploy.sh ${params.ENVIRONMENT} ${params.VERSION}"
}
}
}
}
```
Access parameters with params object: params.VERSION, params.ENVIRONMENT. First build uses default values, subsequent builds prompt user for input.
Advanced parameters:
```groovy
parameters {
string(
name: 'GIT_BRANCH',
defaultValue: 'main',
description: 'Branch to build',
trim: true // Trim whitespace
)
choice(
name: 'BUILD_TYPE',
choices: ['debug', 'release'],
description: 'Build configuration'
)
password(
name: 'API_KEY',
defaultValue: '',
description: 'API Key for deployment'
)
validatingString(
name: 'EMAIL',
defaultValue: 'user@example.com',
regex: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
failedValidationMessage: 'Invalid email format',
description: 'Notification email'
)
}
```
Dynamic parameters using Active Choices plugin enable parameters depending on other parameters or external data. Example: environment choice determines available regions.
Build with parameters: first run establishes parameter definitions, subsequent runs show "Build with Parameters" button. Trigger via API:
```bash
curl -X POST http://jenkins/job/myjob/buildWithParameters \
--user user:token \
--data VERSION=1.2.0 \
--data ENVIRONMENT=production
```
Conditional logic based on parameters:
```groovy
stage('Performance Tests') {
when {
expression { params.RUN_PERF_TESTS == true }
}
steps {
sh 'npm run test:performance'
}
}
```
Multibranch pipeline parameters: Multibranch pipelines don't support parameters block (parameters would differ per branch). Use properties step:
```groovy
properties([
parameters([
string(name: 'VERSION', defaultValue: '1.0.0')
])
])
```
Best practices: provide sensible defaults, add clear descriptions, validate input (regex, constraints), use choice for limited options preventing invalid input, avoid sensitive data in parameters (use credentials instead), document required parameters, limit parameter count (too many becomes confusing), use boolean for feature flags, consider parameter naming conventions. Understanding parameters enables building flexible, reusable pipelines accommodating different scenarios without code changes.