Help with Groovy syntax

classic Classic list List threaded Threaded
11 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Help with Groovy syntax

Chris Fouts
I'm 4 days old new to Groovy. I bought a book, but I just want to learn what this syntax mean for today.

We use Groovy to run a Jenkins file in our Jenkins build. One stage has these statements in it.

    def mainScmDetails

    stage("Checkout") {

        mainScmDetails = checkout scm

        dir("some-dir") {

            git url: 'git@.../path/project.git', credentialsId: 'some_creds', branch: 'develop'

        }

    }


Does this define a code block named mainScmDetails? Does the statement...

mainScmDetails = checkout scm

...call two functions, namely, checkout and scm?

Thanks,

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

ericksn

Looks to me like stage is a method or a closure that takes 2 args. The first is a string, the second is a closure.

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Friday, May 25, 2018 at 6:35 AM
To: "[hidden email]" <[hidden email]>
Subject: Help with Groovy syntax

 

I'm 4 days old new to Groovy. I bought a book, but I just want to learn what this syntax mean for today.

 

We use Groovy to run a Jenkins file in our Jenkins build. One stage has these statements in it.

 

    def mainScmDetails

    stage("Checkout") {

        mainScmDetails = checkout scm

        dir("some-dir") {

            git url: 'git@.../path/project.git', credentialsId: 'some_creds', branch: 'develop'

        }

    }

 

Does this define a code block named mainScmDetails? Does the statement...

mainScmDetails = checkout scm

...call two functions, namely, checkout and scm?

Thanks,

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

Chris Fouts
Firstly, what does the...

def mainScmDetails

...without anything after it mean in Groovy? "stage" defines a Jenkins pipeline stage that I see in my pipeline browser, that is, I see "Checkout" as one of the stages.

Chris

On Fri, May 25, 2018 at 9:47 AM Nelson, Erick <[hidden email]> wrote:

Looks to me like stage is a method or a closure that takes 2 args. The first is a string, the second is a closure.

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

<a href="tel:(858)%20740-6523" value="+18587406523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Friday, May 25, 2018 at 6:35 AM
To: "[hidden email]" <[hidden email]>
Subject: Help with Groovy syntax

 

I'm 4 days old new to Groovy. I bought a book, but I just want to learn what this syntax mean for today.

 

We use Groovy to run a Jenkins file in our Jenkins build. One stage has these statements in it.

 

    def mainScmDetails

    stage("Checkout") {

        mainScmDetails = checkout scm

        dir("some-dir") {

            git url: 'git@.../path/project.git', credentialsId: 'some_creds', branch: 'develop'

        }

    }

 

Does this define a code block named mainScmDetails? Does the statement...

mainScmDetails = checkout scm

...call two functions, namely, checkout and scm?

Thanks,

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

ysb33r
In reply to this post by Chris Fouts
That is Jenkins pipleine which is a sandboxed Groovy DSL. Most Groovy rules still apply though.


As to your question and not having looked ever at how the DSL has been implemented, then 'scm' is either field called 'scm' or a method called 'getScm()'.

You are right about 'checkout'. It will be a method by the same name. 'checkout(scm)' should achive the same result.

Stuff like stage("checkout") { ... } and dir("some-dir") { ...} are usually just method calls of the format:

    stage(String name, Closure configurator)
    dir(String name, Closure configurator)


On 25/05/2018 15:34, Chris Fouts wrote:
I'm 4 days old new to Groovy. I bought a book, but I just want to learn what this syntax mean for today.

We use Groovy to run a Jenkins file in our Jenkins build. One stage has these statements in it.

    def mainScmDetails

    stage("Checkout") {

        mainScmDetails = checkout scm

        dir("some-dir") {

            git url: 'git@.../path/project.git', credentialsId: 'some_creds', branch: 'develop'

        }

    }


Does this define a code block named mainScmDetails? Does the statement...

mainScmDetails = checkout scm

...call two functions, namely, checkout and scm?

Thanks,

Chris


-- 
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r
Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

ysb33r
In reply to this post by Chris Fouts
On 25/05/2018 15:55, Chris Fouts wrote:
> Firstly, what does the...
>
> def mainScmDetails
>

Equivalent of:  Object mainScmDetails = null



--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

ericksn
In reply to this post by Chris Fouts

It just defines a variable outside the scope of the closure that is passed to stage. This variable is used in that closure.

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Friday, May 25, 2018 at 6:55 AM
To: "[hidden email]" <[hidden email]>
Subject: Re: Help with Groovy syntax

 

Firstly, what does the...

 

def mainScmDetails

 

...without anything after it mean in Groovy? "stage" defines a Jenkins pipeline stage that I see in my pipeline browser, that is, I see "Checkout" as one of the stages.

 

Chris

 

On Fri, May 25, 2018 at 9:47 AM Nelson, Erick <[hidden email]> wrote:

Looks to me like stage is a method or a closure that takes 2 args. The first is a string, the second is a closure.

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

<a href="tel:(858)%20740-6523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Friday, May 25, 2018 at 6:35 AM
To: "[hidden email]" <[hidden email]>
Subject: Help with Groovy syntax

 

I'm 4 days old new to Groovy. I bought a book, but I just want to learn what this syntax mean for today.

 

We use Groovy to run a Jenkins file in our Jenkins build. One stage has these statements in it.

 

    def mainScmDetails

    stage("Checkout") {

        mainScmDetails = checkout scm

        dir("some-dir") {

            git url: 'git@.../path/project.git', credentialsId: 'some_creds', branch: 'develop'

        }

    }

 

Does this define a code block named mainScmDetails? Does the statement...

mainScmDetails = checkout scm

...call two functions, namely, checkout and scm?

Thanks,

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

Chris Fouts
In reply to this post by ysb33r
Got'\ ya, thanks!

On Fri, May 25, 2018 at 9:56 AM Schalk Cronjé <[hidden email]> wrote:
On 25/05/2018 15:55, Chris Fouts wrote:
> Firstly, what does the...
>
> def mainScmDetails
>

Equivalent of:  Object mainScmDetails = null



--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r

Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

Daniel.Sun
In reply to this post by Chris Fouts
Hi Chris,

      If the last parameter of method is a closure, we can write code like
as follows:

someMethod("abc") {
    // this is a closure
}

the above code is equal to

someMethod("abc", {
    // this is a closure
})

As you can see, the former code is much more readable and groovy ;-)

Cheers,
Daniel.Sun






--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: Help with Groovy syntax

Chris Fouts
Makes sense. I started reading about closures before I posted, and will keep reading.

On Fri, May 25, 2018 at 9:58 AM Daniel.Sun <[hidden email]> wrote:
Hi Chris,

      If the last parameter of method is a closure, we can write code like
as follows:

someMethod("abc") {
    // this is a closure
}

the above code is equal to

someMethod("abc", {
    // this is a closure
})

As you can see, the former code is much more readable and groovy ;-)

Cheers,
Daniel.Sun






--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html
Reply | Threaded
Open this post in threaded view
|

Help with docker calls in Groovy

Chris Fouts
 I days old with Groovy so please bear with me. I bought a Groovy book but this subject is of course not discussed in it.

We use a Jenkins file to set up stages, and we have this stage

def demoImage
stage("Build ECS Docker Image") {

         dir('demo') {

            docker.withRegistry("https://${dockerUrl}", ecrCredentialsId) {

              demoImage = docker.build("${dockerUrl}/some-name-${env.EnvironmentKey}-${env.DBName}")

            }

          }

}

I assume demoImage contains some image information, like the image ID that I need for later? How do I get the image ID from demoImage? Where would this be documented online? I googled, but I don't necessarily how to look what I'm looking for.

Thanks,
Chris
12