Jack of Some Trades
Jack of Some Trades

Reputation: 75

How to pass optional Jenkins parameter into a Jenkinsfile

I have two Jenkins jobs. Neither uses a Jenkinsfile under source control. Instead, each job has a pipeline script saved inside the Jenkins job itself. The pipeline scripts of the two jobs differ in only one way - one of them prompts the user to choose a parameter called VERSION when running the job and the other one does not. The pipeline scripts are identical in all other respects.

I am trying to consolidate the two pipeline scripts into a single Jenkinsfile under source control. I have defined the VERSION variable thus:

def version = (VERSION != "") ? "version=${VERSION}" : ""

As I understand it (I might be wrong), this means "if VERSION is entered by the user running the job, set $version to $VERSION. If VERSION is not entered by the user running the job, set $version to null". I then call $version further down my Jenkinsfile.

When I run the Jenkins job where VERSION is NOT selected by the user, the job fails with this error:

groovy.lang.MissingPropertyException: No such property: VERSION for class: 

Any thoughts on how I can optionally pass VERSION into my Jenkinsfile depending on whether the user has selected this option when running the job or not?

Upvotes: 5

Views: 6275

Answers (1)

yong
yong

Reputation: 13722

If VERSION is the job's parameter, you should change as following:

def version = params.VERSION ? "version=${params.VERSION}" : ""

Upvotes: 6

Related Questions