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