Child pages
  • Jenkins Script Console
40 more child pages
Skip to end of metadata
Go to start of metadata

Jenkins Script Console

Jenkins features a nice Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins master runtime or in the runtime on agents.

Security warnings

It is very important to understand all of the following points because it affects the integrity of your Jenkins installation. The Jenkins Script Console:

  • Access is controlled by the RunScripts permission.  Older versions of the Matrix Authorization Strategy Plugin allow non-Admin users to be granted this permission.  Matrix Authorization Strategy Plugin version 1.5 and later fixed this issue. If any authorization strategy allows this permission to be granted to users other than Admins, then extreme care should be taken not to allow non-admins.
  • Is a web-based Groovy shell into the Jenkins runtime. Groovy is a very powerful language which offers the ability to do practically anything Java can do including:
    • Create sub-processes and execute arbitrary commands on the Jenkins master and agents.
    • It can even read files in which the Jenkins master has access to on the host (like /etc/passwd)
    • Decrypt credentials configured within Jenkins.
  • Offers no administrative controls stop a User (or Admin) once they are able to execute the Script Console from affecting all parts of the Jenkins infrastructure. Granting a normal Jenkins user Script Console Access is essentially the same as giving them Administer rights within Jenkins.
  • Can configure any Jenkins setting. It can disable security, reconfigure security, even open a backdoor on the host operating system completely outside of the Jenkins process. Due to the mission critical importance many organizations place on Jenkins in their infrastructure this point is especially important because it would allow an attacker to move laterally within infrastructure with little effort.
  • Is so powerful because it was originally intended as a debugging interface for Jenkins developers but has since grown into an interface used by Jenkins Admins to configure Jenkins and debug Jenkins runtime issues.

Because of the power offered by the Jenkins Script Console, Jenkins and its agents should never be run as the root user (on Linux) or system administrator on any other flavor of OS.   Videos linked in this wiki page demonstrate and discuss security warnings.

Be sure to secure your Jenkins instance using known good community practices.

Table of Contents

Multiple contexts

The Jenkins Script Console can run either on the master or any configured agents.

Running Script Console on the master

This feature can be accessed from "Manage Jenkins" > "Script Console".  Or visit the sub-URL /script on your Jenkins instance.

Running Script Console on agents

Visit "Manage Jenkins" > "Manage Nodes".  Select any node to view the status page.  In the menu on the left, a menu item is available to open a "Script Console" on that specific agent.

Run scripts from master Script Console on agents

It's also possible to run scripts from the master Script Console on individual agents.  The following script is an example running a script on agents from the master Script Console.

Script executes code on agent from Master Script Console
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins

String agent_name = 'your agent name'
//groovy script you want executed on an agent
groovy_script = '''
println System.getenv("PATH")
println "uname -a".execute().text

String result
Jenkins.instance.slaves.find { agent -> == agent_name
}.with { agent ->
    result = RemotingDiagnostics.executeGroovy(groovy_script,
println result

Remote access

A Jenkins Admin can execute groovy scripts remotely by sending an HTTP POST request to /script/ url or /scriptText/.

curl example via bash
curl -d "script=<your_script_here>" https://jenkins/script
# or to get output as a plain text result (no HTML)
curl -d "script=<your_script_here>" https://jenkins/scriptText

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.

curl submitting groovy file via bash
curl --data-urlencode "script=$(< ./somescript.groovy)" https://jenkins/scriptText

If security is configured in Jenkins, then curl can be provided options to authenticate using the curl --user option.

curl submitting groovy file providing username and password via bash
curl --user 'username:password' --data-urlencode "script=$(< ./somescript.groovy)" https://jenkins/scriptText

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.

  1. Authenticate and get a CSRF token for submitting script console scripts.
  2. Authenticate and use the CSRF token when submitting script console scripts.

Here's an example. Get a CSRF token.

Getting CSRF token and saving it to a bash variable
mytoken=$(curl --user 'username:password' -s https://jenkins/crumbIssuer/api/json | python -c 'import sys,json;j=json.load(sys.stdin);print j["crumbRequestField"] + "=" + j["crumb"]')

More examples of getting a CSRF token can be found in the Remote access API wiki page.

Then use the mytoken environment variable to submit the token along with your authentication to the script console.

curl example submitting script with username, password, and CSRF token
curl --user 'username:password' -d "$mytoken" --data-urlencode "script=$(<./somescript.groovy)" https://jenkins/scriptText

Additionally, you can curl the root of the Jenkins API to determine if CSRF protection is enabled.

Use curl to determine if CSRF protection is enabled in Jenkins
curl --user 'username:password' -s https://jenkins/api/json 2> /dev/null | python -c 'import sys,json;exec "try:\n  j=json.load(sys.stdin)\n  print str(j[\"useCrumbs\"]).lower()\nexcept:\n  pass"'

The above command will return true or false. If CSRF protection is enabled then it will return true.

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

Video Tutorials and additional learning materials

Here are some recorded videos on the Jenkins Script Console:

To expand your ability to write script console scripts the following references are recommended:

Example Groovy scripts

Out of date scripts

Due to the nature of Groovy scripts accessing Jenkins source code directly, Script Console scripts are easily out of date from the Jenkins source code. It is possible to run a script and get exceptions because public methods and interfaces in Jenkins core or Jenkins plugins have changed. Keep this in mind when trying out examples. Jenkins is easily started from a local development machine via the following command:

Starting a local copy of Jenkins
export JENKINS_HOME="./my_jenkins_home"
java -jar jenkins.war

Use CTRL+C to stop Jenkins. It is not recommended to try Script Console examples in a production Jenkins instance.

The following repositories offer solid examples of Groovy scripts for Jenkins.

Browse all Scriptler Plugin Groovy Scripts and please share your scripts with the Scriptler Plugin.

Plugins enabling Groovy usage

  • Page:
    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.
  • Page:
    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

  • Page:
    Groovy plugin — This plugin adds the ability to directly execute Groovy code.
  • Page:
    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.
  • Page:
    Groovy Remote Control Plugin — This plugin provides Groovy Remote Control's receiver, and allows to control external application from Jenkins.
  • Page:
    Matrix Groovy Execution Strategy Plugin — A plugin to decide the execution order and valid combinations of matrix projects.
  • Page:
    Pipeline Classpath Step Plugin — Pipeline DSL step to add path to the groovy classpath
  • Page:
    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.

  • No labels


  1. Does anyone have any advice for writing these scripts? Is it just a case of blindly trying to find relevant source code/javadocs and then hand coding and running, or is there a sensible way of testing them in advance? 

    1. I would take a look at this web page:

      It's a reference with all the things you can do.  It's a little daunting at first, but once you check out the examples on this page and learn Groovy (which is similar like Java), you can figure out which methods you need to call.

      Also, I found downloading the source code for various plugins very useful.  I hope this helps, and good luck!

    2. The wiki page was updated to include videos and other resources to help you learn.

  2. The built-in groovy engine (for running system scripts / script console) seems to be a bit outdated:

    groovy.lang.GroovySystem.getVersion() returns 1.8.5 for me,

    while an external script can be run with an installation of 2.1.5 or whatever external groovy is available.

    I have a fairly recent version of Jenkins itself - 1.518

    Is it possible to update Jenkins's internal groovy engine? Is it planned to be udpated in a future release of Jenkins?

    1. It is not possible to customize or upgrade the Groovy engine yourself without recompiling Jenkins.  Good news, as of the timestamp of this post the embedded groovy version is 2.4.11 in Jenkins 2.99 LTS.  I imagine this will regularly see updates in the future.

  3. Hello

    Id like to know how i can parse the console output of a build from another job through groovy script please?

    Not a postbuild one but build groovy script.

    trying something like that

    import hudson.model.\*
    import hudson.\*
    wantedJobs = hudson.model.Hudson.instance.items.findAll{ job->"MyINTEGRATION_TEST")== true && job.lastBuild \!= null }
    for (run in wantedJobs ){
    nb_matcher =log.contains("number of failure \:(.*)/");
    if(nb_matcher?.matches()) {
  4. Hi.  the description of this page says it allows you to "run arbitrary scripts on the Jenkins server or on slave nodes".  If i access this feature through the  "Manage Jenkins" link and type in a script there,  I'm assuming this groovy script is running on the master,.  How can I get it run on a slave?

    1. The wiki page was updated to include examples of running scripts on agents.

  5. Hi, I'm facing issues while importing plugin classes into my groovy code. I use openstack plugin (Plugin is installed and it has respective jar inside my jenkins plugin directory) but still my groovy script shows . "unable to resolve class jenkins.plugins.openstack.slaveopts.BootSource". It's the same case for all the classes. Tried injecting this jar in my classpath via env inject plugin but still no luch. Any suggestions or suitable links for understanding these basics?? Pls help.

    1. It doesn't sound like you're using the script console if you're using the env inject plugin to modify some classpath (I assume for a build).  I recommend using the community forums and mailing lists for help.