Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and reference a variable in a Jenkinsfile

I have a declarative pipeline script for my multibranch project in which I would like to read a text file and store the result as a string variable to be accessed by a later step in the pipeline. Using the snippet generator I tried to do something like this:

filename = readFile 'output.txt' 

For which filename would be my string.

I get an error in the Jenkins console output:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 30: Expected a step @ line 30, column 5.             filename = readFile 'output.txt' 

Do I need to use a withEnv step to set the output of readFile to a Jenkins environment variable? If so, how?

Thanks

like image 962
Dalton Sweeney Avatar asked Mar 01 '17 19:03

Dalton Sweeney


People also ask

How do you set a variable in Jenkinsfile?

Setting environment variables The environment variables can be set declaratively using environment { } block, imperatively using env. VARIABLE_NAME , or using withEnv(["VARIABLE_NAME=value"]) {} block.

How Jenkinsfile use Jenkins variable?

Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.

How do I set environment variable in Jenkins pipeline stage?

Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.

How do I set environment variables in Groovy?

Download a binary distribution of Groovy and unpack it into some folder on your local file system. Set your GROOVY_HOME environment variable to the directory where you unpacked the distribution. Add GROOVY_HOME/bin to your PATH environment variable. Set your JAVA_HOME environment variable to point to your JDK.


2 Answers

The error is due to that you're only allowed to use pipeline steps inside the steps directive. One workaround that I know is to use the script step and wrap arbitrary pipeline script inside of it and save the result in the environment variable so that it can be used later.

So in your case:

pipeline {     agent any     stages {         stage("foo") {             steps {                 script {                     env.FILENAME = readFile 'output.txt'                 }                 echo "${env.FILENAME}"             }         }     } } 
like image 56
Jon S Avatar answered Sep 30 '22 19:09

Jon S


According to the documentation, you can also set global environment variables if you later want to use the value of the variable in other parts of your script. In your case, it would be setting it in the root pipeline:

pipeline {   ...   environment {     FILENAME = readFile ...   }   ... } 
like image 34
Adam Link Avatar answered Sep 30 '22 18:09

Adam Link