Access jenkins data in groovy scripts, vs the Script Console

1,357 views
Skip to first unread message

Morgan Fletcher

unread,
Sep 2, 2013, 8:07:08 PM9/2/13
to jenkinsci-users
I want to be able to access the first seven characters of the current git commit SHA-1 hash, from a gant groovy script I run via a build step in my project (Build With Grails -> Use Grails Wrapper -> Targets = the default target in the gant script.) I can get that string in the Manage Jenkins -> Script Console like so:

    def job = Hudson.instance.getJob('job-name')
    def run = job.getLastBuild()

    println run.getEnvironment()["GIT_COMMIT"][0..6]

But I cannot figure out how to replicate that functionality in my gant script. If I put this into a script, and run it with "Execute system Groovy script", it works:

    import hudson.model.*

    def job = Hudson.instance.getJob('job-name')
    def run = job.getLastBuild()
    println run.getEnvironment()["GIT_COMMIT"][0..6]

But if I phrase it as a gant script, I can't run it as a "system Groovy script":

    groovy.lang.MissingMethodException: No signature of method: Script1.target()

If I run the gant script as a grails target, with this code in place:

    import hudson.model.*

    target(getGitHash: 'Get the current git commit hash') {

        def job = Hudson.instance.getJob('job-name')
        def run = job.getLastBuild()

        println "current git hash is:"
        println run.getEnvironment()["GIT_COMMIT"][0..6]
    }

    setDefaultTarget(getGitHash)

I get:

    No such property: Hudson for class: 

How can I get the current git hash from my gant script?

Thanks,

Morgan

Morgan Fletcher

unread,
Sep 2, 2013, 8:39:09 PM9/2/13
to jenkinsci-users
Er, nevermind:

+    def gitHash = ("git rev-parse HEAD".execute().text[0..6])
+    println "Git hash is $gitHash"
+

Morgan
Reply all
Reply to author
Forward
0 new messages