Jerinaw
Jerinaw

Reputation: 5529

What is a Jenkins Stage in terms of Groovy?

I'm trying to figure out what is a stage in terms of Grooy syntax. What is that syntax:

stage 'stage 1'
    statement 1
    statement 2
    statement n

stage 'stage 2'
    statement 1
    statement 2
    statement n

or I think this is the newer way to create a stage?

stage ('stage'){
    statement 1
    statement 2
    statement n
}

The first code block almost looks like labeled statements but it's missing the colon after the name.

If I wanted to do something like this in Groovy, how would I do it?

I'm a Jenkins and Groovy noob.

[Attempt to clarify my question]
I wanted to know what a stage construct is in terms of Groovy code.

For example, if I asked what is this block of Groovy code

def somefnc(){
   ..statements
}

Someone would say "That is how you define a function in Groovy, you use the keyword def give the function a name..."

My question is what is this

stage ('stage'){
    statement 1
    statement 2
    statement n
}

Is this part of the Groovy language? What is it called? My understanding is that a Jenkins file is Groovy code. So is there some Jenkins preprocessor running? Or is the above valid Groovy and I can use it some how in my plain Groovy? I don't mean "stage", can I define whatever that is and have a

steve('some arguments'){
    statments...
}

Upvotes: 3

Views: 2052

Answers (2)

S Ben
S Ben

Reputation: 46

If the last argument of a function is of type Closure you can add the closure after the arguments so:

def stage(String name, Closure body) {
        println "stage(${name})"
        body.call()
    }

can be called like this:

stage("stage 1"){
   println "inside stage 1"
}

Upvotes: 3

Michael Kemmerzell
Michael Kemmerzell

Reputation: 5256

Your second example is the almost correct syntax but missing a steps{}-block. The official Jenkins documentation is a great help regarding syntaxes and other questions.

So regarding your example this would be the 100% correct syntax if you want to use a stage in a declarative pipeline (I think this is what you referd as 'newer way', the 'old way' would be a scripted pipeline).

stage ('stage'){
    steps {
       statement 1
       statement 2
       statement n
   }
}

Upvotes: 0

Related Questions