Issue
I was trying to create parameterised jenkins pipeline. I checked online we have to select "this project is parameterised" in configuration and add parameters there, but is it possible to add those parameters configuration in jenkinsfile instead?
I know we can use this in jenkinsfile:
parameters {
string(name: 'name')
string(name: 'env')
}
but using this, when i'm trying to trigger job remotely using curl, the parameters are null. On the other hand if i add parameters configuration using "this project is parameterised", it works fine. Is it possible to have those configurations in jenkinsfile only?
Solution
Let’s take the example of String parameter.
Below pipeline snippet will help you to include the Straing parameter in jenkinsfile.
properties([
parameters([
string(name: 'DEPLOY_ENV', defaultValue: 'TESTING', description: 'The target environment')
])
])
So, your jenkinsfile will be like:
properties([
parameters([
string(name: 'DEPLOY_ENV', defaultValue: 'TESTING', description: 'The target environment', )
])
])
pipeline {
agent any
stages {
stage ("Example") {
steps {
script{
echo "Hello World"
echo "${params.DEPLOY_ENV}"
}
}
}
}
}
Edit:
Using the below command, I triggered the jenkins job.
curl -vvv -u "xxx:xxxx" -X POST "http://xx.xxx.xxx.xxx:8080/job/test-param/buildWithParameters?DEPLOY_ENV=SOURAV"
Output:
Answered By - Sourav Atta