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.
Cool Tip: Define conditional variables in a Jenkins pipeline! Read more →
Define a Variable in Jenkins Declarative Pipeline
Here is an example of how to define a variable in a Jenkinsfile
and print this variable in a Jenkins declarative pipeline:
// Define variable def myVariable = "foo" // Print variable pipeline { agent any stages { stage ("Print variable") { steps { echo "My variable is ${myVariable}" } } } }
But how do I change the value of the variable? And what if the variable is not a string?
you do it inside a script { } block
i.e.
In the following sample, env.MYVAR will contain the main.sh stdoutput
Thanks , it worked, the variable assignment should be inside script block.
How to use jenkins paramters in Jenkinsfile.groovy.
yes