Certified Jenkins Engineer

Jenkins Administration and Monitoring Part 1

Demo Script Console

Introduction

The Jenkins Script Console enables administrators to execute Groovy scripts directly on the controller or any connected agent. This powerful interface is ideal for debugging, configuration, and maintenance tasks.

Warning

The Script Console provides unrestricted control over your Jenkins instance. Only trusted users with admin privileges should have access.

Accessing the Script Console

You can open the Script Console in two ways:

  1. From the Jenkins dashboard:

    • Go to Manage JenkinsScript Console.
      The image shows a Jenkins dashboard with a dropdown menu open, displaying various options like "New Item," "Build History," and "Script Console." The background lists different Jenkins jobs with their statuses and execution times.
  2. From Manage Nodes:

    • Select the Built-In Node and click Script Console.

Basic Groovy Scripts

Most common Jenkins classes are pre-imported, so you can start scripting immediately:

ScriptDescription
println Jenkins.instance.pluginManager.pluginsList all installed plugins
println System.getenv("PATH")<br>println "uname -a".execute().textShow environment variables & system info
// List installed plugins
println Jenkins.instance.pluginManager.plugins
// Inspect environment & system info
println System.getenv("PATH")
println "uname -a".execute().text

Printing the Jenkins Version

Explicitly import the Jenkins class to fetch the current version:

import jenkins.model.Jenkins

def jenkinsVersion = Jenkins.instance.version
println "Jenkins Version: ${jenkinsVersion}"

Retrieving Job Details

This script iterates through all jobs and prints key attributes:

import jenkins.model.Jenkins

def jobs = Jenkins.instance.getAllItems()

jobs.each { job ->
    println "##########################################"
    println "Job Full Name: ${job.fullName}"
    println "Job Name: ${job.name}"
    println "Job URL: ${job.absoluteUrl}"
    println "Last Build #: ${job.lastBuild?.number}"
    println "Job Class: ${job.getClass().name}"
    println "##########################################"
}
PropertyDescription
fullNameFolder path + job name
nameJob’s own name
absoluteUrlDirect URL to the job
lastBuild?.numberNumber of the last build (nullable)
getClass().nameJob type (e.g., FreeStyleProject, Workflow)

Leveraging Community Scripts

jenkinsci/jenkins-scripts

The jenkinsci/jenkins-scripts repository provides numerous utilities. For example, count executors on each node:

The image shows a GitHub repository page for "jenkinsci/jenkins-scripts" with a list of Groovy script files and their last commit messages.

import jenkins.model.Jenkins

println "All Nodes - executor count:"
Jenkins.instance.computers.eachWithIndex { node, idx ->
    println "[${idx+1}] ${node.displayName}: ${node.numExecutors}"
}
println "\nTotal nodes: ${Jenkins.instance.computers.size()}"
println "Total executors: ${Jenkins.instance.computers.sum { it.numExecutors }}"

Output example:

[1] Built-In Node: 2
[2] ubuntu-agent: 1

Total nodes: 2
Total executors: 3

samrocketman/jenkins-script-console-scripts

Another valuable collection is samrocketman/jenkins-script-console-scripts. The Jenkins stats script gathers metrics on jobs, users, builds, and more:

This image shows a GitHub repository page for "jenkins-script-console-scripts" by "samrocketman," featuring a list of files and directories related to Jenkins script console scripts. The repository is public and includes details like the number of stars, forks, and commits.

// Print Jenkins statistics (assumes variables are defined)
println "GitHub Orgs: ${organizations}"
println "GitHub Projects: ${projects}"
println "Jenkins Jobs: ${count}"
println "Jobs w/ Builds: ${jobs_with_builds}"
println "Users: ${total_users}"
println "Global Builds: ${global_total_builds}"
println "Pull Requests: ${total_pull_requests}"
println "Tag Releases: ${total_tag_releases}"
println "Projects by Type:"
count_by_type.each { type, num -> println "  ${type}: ${num}" }
null

Security Note

Always review third-party scripts before executing to ensure they’re safe and free of malicious code.


Disabling the Jenkins CLI

To disable CLI access (both TCP and /cli URL), run the following Groovy script:

import hudson.remoting.AgentProtocol
import jenkins.model.Jenkins
import hudson.model.RootAction

def changed = false

// Remove CLI protocols over TCP
def protocols = AgentProtocol.all()
protocols.findAll { it.name.contains("CLI") }.each {
    protocols.remove(it)
    changed = true
}

// Remove CLI actions from /cli URL
def removeCliActions = { list ->
    list.findAll { it.class.name.contains("CLIAction") }.each {
        list.remove(it)
        changed = true
    }
}
removeCliActions(Jenkins.instance.getExtensionList(RootAction))

println changed ? "CLI access disabled." : "No CLI protocols found."
null

Conclusion

The Script Console is a powerful Jenkins feature for automation, diagnostics, and configuration. Always proceed with caution and restrict console access to trusted administrators.


Watch Video

Watch video content

Previous
Demo Jenkins Folder Part 4