Ayush walia
Ayush walia

Reputation: 111

How to create parameterised jenkins pipeline using jenkinsfile only and trigger it remotely using curl

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?

Upvotes: 0

Views: 1165

Answers (2)

Vijay_Yadav
Vijay_Yadav

Reputation: 242

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.

Upvotes: 0

Sourav
Sourav

Reputation: 3392

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

Upvotes: 1

Related Questions