Link Search Menu Expand Document

Jenkins Cheat Sheet

JSON API Calls

# Print name of all known jobs
GET /api/json?tree=jobs[name]&pretty=true

# Enable a job
POST /job/enable

# Run job without parameters
POST /<name>/build

# Run job with parameters
POST /<name>/buildWithParameters?<params>

List all job paths

Jenkins.instance.getAllItems(AbstractItem.class).each {
  	  println(it.fullName)
};

Cancel all jobs

import hudson.model.*

def q = Jenkins.instance.queue

q.items.each { q.cancel(it.task) }

Dump Plugin Versions

Jenkins.instance.pluginManager.plugins.each{
  plugin -> 
    println("${plugin.getShortName()}:${plugin.getVersion()}")
}

Search all build logs

for (job in Jenkins.instance.getAllItems()) {
  try {
    if (job.hasProperty("builds")) {
      if (!job.builds.isEmpty()) {
        // Search latest build log
        if (job.builds[0].hasProperty("log")) {
          if (job.builds[0].log =~ /pattern/) {
            println "${job.fullName}: ${job.builds[-1].id}"
          }
        }
      }
    }
  } catch (Exception e) {
    println "Skipping ${job.fullName}"
  }
}

Tracking builds

Jenkins 1.x

  1. First get the queue number return by the POST that started the call
  2. Wait some seconds! Yes, honestly!
  3. Fetch the build id using

    GET /<job name>/lastBuild/buildNumber
    
  4. Once you have the build id poll the status with

    GET /<job name>/<buildNumber>/api/json
    

CasC Plugin

OIDC Plugin

Configure escape hatch for admin basic auth

securityRealm:
  oic:
     [...]
     escapeHatchEnabled: true
     escapeHatchGroup: "admin"
     escapeHatchSecret: "${ADMIN_PASSWORD}"
     escapeHatchUsername: "admin"

Scripted API Calls

How to use API tokens with scripts