prabhakar Reddy G
prabhakar Reddy G

Reputation: 1069

How to access stringParam in groovy Jenkins pipeline

I have a job which takes stringParam and i am trying to access that stringParam from workflow, however i am getting No such property exception.

parameters {
        stringParam("COMMIT_SHA", "", "[Required] Short SHA of the commit used to build the image")
    }
 print "The commit sha val is: $env.COMMIT_SHA"

I have tried different options like ${params.COMMIT_SHA}, ${COMMIT_SHA}

Upvotes: 1

Views: 3103

Answers (2)

Jolly Roger
Jolly Roger

Reputation: 4080

If you're using the scripted pipeline, try:

properties([
  parameters([
    stringParam(name:"COMMIT_SHA", defaultValue: "", description: "[Required] Short SHA of the commit used to build the image")
  ])
])

Upvotes: 0

Chris Maggiulli
Chris Maggiulli

Reputation: 3814

Solution

Modify your parameters block as follows

parameters { 
    string(name: 'COMMIT_SHA', defaultValue: '', description: '[Required] Short SHA of the commit used to build the image') 
}

You should then be able to reference it using ${params.COMMIT_SHA} or ${COMMIT_SHA} or ${env.COMMIT_SHA}

Your syntax is for the Jobs DSL plugin but you stated you are using a pipeline. If you are, in fact, using a Freestyle Job to create other jobs instead of a Pipeline please provide the entire script and I will update my answer. However, your post appears to want to set the parameter for a pipeline ( not specify the metadata for the creation of a separate job via the Jobs DSL plugin )

The following Job DSL script, when executed, will create a Job called "example" with COMMIT_SHA as a parameter. It won't be added as a parameter to the job that contains the Job DSL script

job('example') {
    parameters {
        stringParam('myParameterName', 'my default stringParam value', 'my description')
    }
}

In other words, there is nothing to print in the job that contains the stringParam code because that code configures a different job to contain a string parameter, it doesn't add that parameter to the current job

Upvotes: 1

Related Questions