Execute Groovy script in Jenkins with an API call


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?


  • CloudBees Jenkins Enterprise > 1.596.1.1
  • Jenkins LTS > 1.596.1
  • Jenkins > 1.600


Jenkins supports scripting with the Groovy language. You can access the scripting console by opening in a browser the URL /script of your Jenkins instance. (i.e: http://localhost:8080/script)

The advantage of the Groovy language is that those Groovy scripts are executed within Jenkins and have access to everything (config, plugins, jobs, etc).

To automate the execution of this script, you can save it to a file (let’s say /tmp/script.groovy) and run the following curl command:

curl -d "script=$(cat /tmp/script.groovy)" -v --user username:ApiToken http://localhost:8080/scriptText
curl -d "script=println 'this script works'" -v --user username:ApiToken http://localhost:8080/scriptText

Have more questions?


  • 0
    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
    Praveen Haldipur

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

  • 0
    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'

    >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.