Skip to content

Latest commit

 

History

History
61 lines (41 loc) · 1.69 KB

variable-dsl.md

File metadata and controls

61 lines (41 loc) · 1.69 KB

Using Built in Variables

The Var.Variable type represents a variable in groovy. From pipeline kt you can interpolate a Var.Variable into a string and it will generated interpolated groovy, for example:

val workspace = "workspace".groovyVariable()
sh("ls $workspace")

will generate the groovy:

sh (script: "ls ${workspace}", returnStdout: false)

Accessing Environment Variables and Parameters

You can get an environment variable by calling the String.environmentVar() method and a parameter by calling String.parameter().

These will be objects of type Var.Variable. Var objects can be interpolated into Strings.

val myEnvVar = "SOME_ENVIRONMENT_VARIABLE".environmentVar()
val myParameter = "MY_PARAMETER".parameter()
sh("./build ${myEnvVar} ${myParameter}")

this would generate the following groovy:

sh (script: "./build ${env.SOME_ENVIRONMENT_VARIABLE} ${params.MY_PARAMETER}", returnStdout: false)

Declaring Variables

Maybe you want to declare a variable, say if you want to capture the output of sh or readproperties

val currentPath = def { sh("pwd", true) }

Which will generate something like:

def rAnDomVariablenAmE = sh (script: "pwd", returnStdout: true) //rAnDomVariablenAmE is psuedo code and the name will be random

Reassigning existing variables

You can also reassign existing variables with the assign function

//literal is a step that interpolates literal strings into the jenkinsfile
val currentBuildVar = assign("currentBuild.result") { literal("currentBuild.result ?: 'SUCCESS'") }

which generates

currentBuild.result = currentBuild.result ?: 'SUCCESS'