Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-36830

Implicit Groovy script args String[] is not recognized when scriptlet executes

XMLWordPrintable

      Trying to use the scriptlet as a regular Groovy script in support of plugins (such as Conditional step) that can only use a Groovy script and not a Scriptler script.

      Thought that I could use the scriptlet as a regualr Groovy script and pass it command line arguments instead of the scriptlet parameters. You need to make small modifications to your scriptlet to work with command line arguments AND parameters. Every Groovy script has an implicit args String[] that represents the command line arguments passed into the script.
      So if we had a hello.groovy scriptlet with one defined vName parameter. We would write a standard scriptlet as:

      name=vName
      println "Hello $name"
      

      We could adapt this as follows to work with one command line argument like this:
      >groovy hello.groovy John

      name=''
      if (args){
      name=args[0]
      }else{
      name=vName
      }
      println "Hello $name"
      

      When the code above is executed in a Jenkins Conditional Build setp as a Groovy script it works fine. But when you try to execute the script as a scriptlet, the implicit 'args' parameter is not recognized

      You get a 'MissingPropertyException: No such property: args for class: Script1' error

            domi Dominik Bartholdi
            ioannis Ioannis Moutsatsos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: