Jenkins Script Console
Jenkins features a nice Groovy script console which allows to run arbitrary scripts on the Jenkins server or on slave nodes. This feature can be accessed from the "manage Jenkins" link, typically at your http://server/jenkins/script.
Shortcut key on script console to submit
You can submit a script without mouse. Jenkins has a shortcut key which enables to submit with keyboard.
- Windows / Linux : Ctrl + Enter
- Mac : Command + Enter
User can execute groovy scripts remotely sending post request to
/script/ url or
/scriptText/ to have response returned without the html wrapping.
Also, Jenkins CLI offers the possibility to execute groovy scripts remotely using
groovy command or execute groovy interactivelly via
groovysh. However, once again curl can be used to execute groovy scripts by making use of bash Command Substitution. In the following example
somescript.groovy is a groovy script in the current working directory.
If security is configured in Jenkins, then curl can be provided options to authenticate using the
curl --user option.
Remote access with CSRF protection enabled
There's an extra step which must be performed to configure Jenkins via the Script Console when CSRF protection is enabled. The extra step is to get a CSRF token. The token provides an extra security measure in Jenkins to ensure the script console is not being configured from an unauthorized source. It basically comes down to a two step process.
- Authenticate and get a CSRF token for submitting script console scripts.
- Authenticate and use the CSRF token when submitting script console scripts.
Here's an example. Get a CSRF token.
Then use the
mytoken environment variable to submit the token along with your authentication to the script console.
Additionally, you can
curl the root of the Jenkins API to determine if CSRF protection is enabled.
The above command will return
false. If CSRF protection is enabled then it will return
Sample Groovy scripts
Browse all Scriptler Plugin Groovy Scripts: https://github.com/jenkinsci/jenkins-scripts/tree/master/scriptler
please share your scripts with the Scriptler Plugin
Write Groovy scripts for Jenkins with Code completion - The gist of this is to create a Maven project within your IDE and to depend on org.jenkins-ci.main:jenkins-core (and any other plugins that you expect present). You can then write a Groovy script with code completion of Jenkins API objects and methods.
Plugins enabling Groovy usage
Config File Provider Plugin — Adds the ability to provide configuration files (i.e., settings.xml for maven, XML, groovy, custom files, etc.) loaded through the Jenkins UI which will be copied to the job's workspace.
Global Post Script Plugin — Execute a global configured groovy script after each build of each job managed by the Jenkins.
This is typical for cases when you need to do something based on a shared set of parameters, such as triggering downstream jobs managed by the same Jenkins or remote ones based on the parameters been passed to the parameterized jobs.
Notice: jython script support removed since 1.1.0
Groovy plugin — This plugin adds the ability to directly execute Groovy code.
Groovy Postbuild Plugin — This plugin executes a groovy script in the Jenkins JVM. Typically, the script checks some conditions and changes accordingly the build result, puts badges next to the build in the build history and/or displays information on the build summary page.
Matrix Groovy Execution Strategy Plugin — A plugin to decide the execution order and valid combinations of matrix projects.
Pipeline Classpath Step Plugin — Pipeline DSL step to add path to the groovy classpath
Scriptler Plugin — Scriptler allows you to store/edit groovy scripts and execute it on any of the slaves/nodes... no need to copy/paste groovy code anymore.
SnowGlobe Plugin — This plugin provides the ability to define Infrastructure as Code. Create, update and tear down clusters of related docker containers for builds, testing or continuous delivery.
Snowglobe plugin for Jenkins
This allows Jenkins jobs to control a SnowGlobe instance (see https://nirima.github.io/SnowGlobe/).
The operations are relatively simple:
snowglobe_clone createAction: true, sourceId: 'ci-template', targetId: 'new-globe-name'
snowglobe_set_variables globeId: 'my-globe', variables: 'key="value"'
data = snowglobe_get_variables globeId: 'my-globe'
snowglobe_apply createAction: true, globeId: 'my-globe'
data = snowglobe_state createAction: false, globeId: 'my-globe'
snowglobe_clone remove: true, globeId: 'my-globe'
Remove: set to true to also remove the SnowGlobe after destruction.
In all cases - createAction controls whether to add an action to the build, which will also remove the globe when the CI build is complete.