Execute Groovy script in Jenkins remotely

Issue

I want to automate the use of some Jenkins features that are not available via the REST API. Can I run Groovy in the Jenkins script console via a REST request or Jenkins CLI?

Environment

Resolution

Jenkins supports scripting with the Groovy language. A Jenkins admin user can get a scripting console by opening in a browser the URL /script of your Jenkins instance. (i.e: https://jenkins.example/script). Reference Script Console.

Besides, users with admin permission can execute groovy code remotely and this article is going through the diferent available options. To explain the different options we are using the same script (system-message-example.groovy) as an example, which updates the System Message from Manage Jenkins > Configure System.

import jenkins.model.Jenkins
message="foo"
Jenkins jenkins = Jenkins.get()
jenkins.setSystemMessage(message)
jenkins.save()

To automate the execution of the above script, save it to a file (let’s say system-message-example.groovy).

Via REST API

Reading the script

curl -d "script=$(cat /tmp/system-message-example.groovy)" -v --user username:ApiToken https://jenkins.example.com/scriptText

Introducing the script manually and separate by ;

curl -d "script=message='foo'; Jenkins jenkins = Jenkins.get(); jenkins.setSystemMessage(message); jenkins.save()" -v --user username:ApiToken https://jenkins.example.com/scriptText

Via Jenkins CLI

The following examples uses the jenkins-cli alias as explained here.

groovy

It requires a script passed as parameter

$> jenkins-cli groovy = < /tmp/system-message-example.groovy

groovysh

It creates an interactive groovy shell, where you need to type every line of the script.

jenkins-cli groovysh
Groovy Shell (2.4.12, JVM: 1.8.0_252)
Type ':help' or ':h' for help.
-------------------------------------------------------------------------------
groovy:000> import jenkins.model.Jenkins
===> hudson.model.*, jenkins.model.Jenkins
groovy:000> message="foo"
===> foo
groovy:000> Jenkins jenkins = Jenkins.get()
===> hudson.model.Hudson@42694fdc
groovy:000> jenkins.setSystemMessage(message)
===> null
groovy:000> jenkins.save()
===> null
groovy:000>

Tested product/plugin versions

The latest update of this article has been tested with

Have more questions?

3 Comments

  • 0
    Avatar
    Praveen Haldipur

    Wrote a groovy script works from the web console but fails with parsing errors when used from REST API. Why the diff?

    it fails on '&' i have embedded in some HTML that I am adding to summary action for a build.

     

  • 0
    Avatar
    Praveen Haldipur

    Need to use curl --data-urlencode when using REST API. That worked!

  • 0
    Avatar
    Ed Mason

    When I try this on Windows with a multi-line script I find that it only executes the first line.  Any ideas?

    script test.groovy

    println 'starting script'
    println(Jenkins.instance.getAllItems(Job.class))
    println(Jenkins.instance.pluginManager.plugins)

    >curl.exe -d "script=$(type /temp/test.groovy)" -v --user <my_userid> https://<my_jenkins_server>/scriptText

     

    * upload completely sent off: 129 out of 129 bytes
    < HTTP/1.1 200 OK
    < Date: Wed, 27 Nov 2019 15:32:57 GMT
    < Server: Jetty(9.4.z-SNAPSHOT)
    < X-Content-Type-Options: nosniff
    < Content-Type: text/plain;charset=utf-8
    < Content-Length: 16
    <
    starting script
    * Connection #0 to host <my_jenkins_server> left intact

     

     

     

Please sign in to leave a comment.