How to add multiple groovyScript lines in active choices parameters

4,735 views
Skip to first unread message

Jozsef Sin

unread,
Sep 29, 2016, 7:10:01 AM9/29/16
to job-dsl-plugin
Hi,

I`d like to create a seed job with some active choices parameter which populates the list with groovy script. 
My problem is how to use multiple lines in groovy script part? The #2 would be more readable but not works.

#1
This will work but hard to read!
parameters {
        activeChoiceParam('NODE_NAME') {
            description('Select Node')
            filterable() 
            choiceType('SINGLE_SELECT')
            groovyScript {
                script(' import jenkins.*\r\n import jenkins.model.*\r\n import hudson.*\r\n import hudson.model.*\r\n def nodes = Jenkins.instance.nodes.findAll()\r\n return nodes*.name')
                fallbackScript('"Error in script"')
            }
        }
  }

#2
This won`t work but nicer!

parameters {
        activeChoiceParam('NODE_NAME') {
            description('Select Node')
            filterable()
            choiceType('SINGLE_SELECT')
            groovyScript {
                script('
                  import jenkins.*
                  import jenkins.model.*
                  import hudson.*
                  import hudson.model.*
                  def nodes = Jenkins.instance.nodes.findAll()
                  return nodes*.name
                ')
                fallbackScript('"fallback choice"')
            }
        }
  }

Matt Sheehan

unread,
Sep 29, 2016, 9:32:39 AM9/29/16
to job-dsl...@googlegroups.com
You can use triple single quotes for multiline strings:

parameters {
    activeChoiceParam('NODE_NAME') {
        description('Select Node')
        filterable()
        choiceType('SINGLE_SELECT')
        groovyScript {
            script('''
                import jenkins.*
                import jenkins.model.*
                import hudson.*
                import hudson.model.*
                def nodes = Jenkins.instance.nodes.findAll()
                return nodes*.name
            ''')
            fallbackScript('"fallback choice"')
        }
    }
}

and if you want to clean up the whitespace:

parameters {
    activeChoiceParam('NODE_NAME') {
        description('Select Node')
        filterable()
        choiceType('SINGLE_SELECT')
        groovyScript {
            script('''
                import jenkins.*
                import jenkins.model.*
                import hudson.*
                import hudson.model.*
                def nodes = Jenkins.instance.nodes.findAll()
                return nodes*.name
            '''.stripIndent().trim())
            fallbackScript('"fallback choice"')
        }
    }
}

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/ffa33ce8-4426-4549-8ec7-96ad5c577313%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jozsef Sin

unread,
Sep 29, 2016, 9:59:09 AM9/29/16
to job-dsl-plugin
Thanks Matt works great!

Sai Kishore

unread,
Dec 13, 2016, 9:25:54 AM12/13/16
to job-dsl-plugin
hey this groovy is not generating any parameter in my jenkins job configuration ,
Do we need to have any plugin installed ?



On Thursday, 29 September 2016 19:29:09 UTC+5:30, Jozsef Sin wrote:
Thanks Matt works great!
Reply all
Reply to author
Forward
0 new messages