0

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?

2 Answers 2

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, i tried triggering it remotely using curl :- curl -X POST -u "USER_NAME:API_KEY "localhost:8080/job/test-pipeline/build" --form json='{"parameter": [{"name":"DEPLOY_ENV", "value":"PRODUCTION"}]}' , but i'm still getting TESTING once job executes
Hi @Ayushwalia, I have updated my answer with curl command and output of the build.
Hi, I tried this as well, but got "HTTP/1.1 500 Server Error", then i ticked this build is parameterised and added this DEPLOY_ENV there, then it worked fine. But my question is can we do this without adding DEPLOY_ENV under this build is parameterised option?
No, I afraid not. There is no such way in Jenkins I am aware of.
oh i just checked, if I trigger build manually once from gui, it gets populated automatically, i'm fine with that too, but instead of triggering for the first time from gui, can we trigger that from curl, so that it gets populated automatically?
|
0

The answer by souravatta is correct but i use a different approach and will like to share with an example.

//define the variable globally so that can be used by a fuction as well as pipeline somevalue1=""; somevalue2=""; somevalue3=""; somevalue4=""; pipeline{ environment{ releasevalue = "r5" depenv = "dev" servicename = "Myservice" setecsvalues = settingvalues(releasevalue,depenv,servicename) } agent any stages{ stage(testing){ steps{ echo "${releasevalue}" echo "${depenv}" echo "${servicename}" echo "${somevalue1}" echo "${somevalue2}" echo "${somevalue3}" echo "${somevalue4}" } } } } //Setting Values from this function, you can write any program here to dynamically set values or maybe read from an external file def settingvalues(releasevalue,depenv,servicename){ //some code to set values dynamically somevalue1 = value1 somevalue2 = value2 somevalue3 = value3 somevalue4 = value4 } 

It contains some more things from what you have asked for so bear with me :)

In environment block I have written 3 values and one function. You can simply write the values and use it inside any stage.

You can write a function and return a value by setting it dynamically. Here I am not returning anything because I wanted to set 4 values instead of returning a single one.

On top I have defined 4 values, that can be used by the function as well as inside the pipeline.

TIP

If you hardcode the values of variables inside the Jenkinsfile, that is not a good solution. Instead what you need to do is create a file, JSON or Yaml. Put it in a common location and use readJSON or readYaml to read those files and extract values dynamically. this way if you have to manage a lot of applications with many values then you can simply use one file to place all the values. This can come in handy and saves a lot of time as you do not want to edit your Jenkisnfile very often.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.