execute-groovy-with-a-rest-call

Issue

I want to use some Jenkins features that are not available at REST API, Could I execute they as I do in the console script but through REST API?

Environment

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

Resolution

Jenkins supports scripting with the Groovy language. You can get a 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? Submit a request

2 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!

Please sign in to leave a comment.