Skip to content

Instantly share code, notes, and snippets.

View ansig's full-sized avatar

Anders Sigfridsson ansig

View GitHub Profile
@ansig
ansig / createUserApiToken.groovy
Created August 16, 2019 10:51
Create Jenkins user API token
import hudson.model.User
import jenkins.security.ApiTokenProperty
def username = 'my-username'
def tokens = ['my-api-token']
user = User.get(username, false, Collections.emptyMap())
if (user == null) {
println "Did not find existing user '${username}', creating it..."
user = User.get(username, true, Collections.emptyMap())
@ansig
ansig / configure-lockable-resources.groovy
Created December 22, 2018 12:18
Jenkins init script to add LockableResources specified in a config file (specified in INIT_JENKINS_CONFIG var)
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
def lockableResourcesConfigFile = new File("${System.getenv('INIT_JENKINS_CONFIG')}/lockableResources.yml")
if (lockableResourcesConfigFile.exists()) {
println "INFO: Adding LockableResources from configuration: ${lockableResourcesConfigFile.absolutePath}"
Yaml parser = new Yaml()
@ansig
ansig / runInitScripts.groovy
Created December 22, 2018 12:16
Jenkins Groovy script that executes all scripts in init.groovy.d
import groovy.io.FileType
def initGroovyDir = new File("${System.getenv('JENKINS_HOME')}/init.groovy.d")
initGroovyDir.eachFileMatch(FileType.FILES, ~/.*\.groovy/) {
println "Running: ${it}"
new GroovyShell(this.class.classLoader).evaluate(it)
}
println "Done!"
@ansig
ansig / setCSPForMavenSite.groovy
Created November 30, 2018 06:24
Setting the Content Security Config in Jenkins to properly show Maven site html
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox; default-src 'none'; img-src 'self'; style-src 'self' 'unsafe-inline';")
@ansig
ansig / collect-resources-data-for-graphite.groovy
Created August 12, 2018 11:59
Groovy script to collect data from Lockable Resource and format it to be passed into a Graphite instance.
import org.jenkins.plugins.lockableresources.LockableResources
import org.jenkins.plugins.lockableresources.LockableResource
import java.util.Date
def resourceManager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
timestamp = new Date().time / 1000 as Integer
data = [:]
resourceManager.allLabels.each { label ->
@ansig
ansig / cleanLog.sh
Last active May 8, 2018 06:04
Clean Jenkins logs extended by AnsiColor plugin
#!/usr/bin/env bash
cat -vet log | sed -r 's/\^\[\[8m[a-z\:\/0-9A-Z\+]+={1,2}?\^\[\[0m//g' | sed 's/\$$//g'
@ansig
ansig / findPreviousSuccessfulBuild.groovy
Created April 20, 2018 11:30
Find previous successful Jenkins build
def findPreviousSuccessfulBuild(build) {
previousBuild = build.previousBuild
while (previousBuild != null && previousBuild.result != Result.SUCCESS) {
previousBuild = previousBuild.previousBuild
}
return previousBuild
}
@ansig
ansig / printClasspath.groovy
Created April 19, 2018 10:48
Print classpath in Groovy for debugging
def printClassPath(classLoader) {
println "=== ${classLoader} ==="
if (classLoader instanceof java.net.URLClassLoader) {
classLoader.getURLs().each {url->
println "- ${url.toString()}"
}
}
if (classLoader.parent) {
printClassPath(classLoader.parent)
}
@ansig
ansig / listPluginsSorted.groovy
Last active January 2, 2019 12:10
List Jenkins plugins in sorted order
def plugins = Jenkins.instance.pluginManager.plugins.sort(mutate = false) { a, b -> a <=> b }.each {
println "${it.shortName}:${it.version}"
}
println "Done!"
@ansig
ansig / listFailedBranches.groovy
Created March 13, 2018 06:51
List failing branches of failing builds in a Jenkins Pipeline
jobs = Jenkins.instance.allItems(hudson.model.Job).findAll { it.fullName == ("<JOBNAME>") }
jobs.each { job ->
job.builds.findAll { !it.building }.each { build ->
finished = new Date(build.timestamp.timeInMillis + build.duration)
println "${build} - ${build.result} (${finished})"
action = build.getAction(org.jenkinsci.plugins.workflow.job.views.FlowGraphAction)
action.getNodes().findAll { it.displayName.startsWith("") }.each {
if (it.displayName.startsWith("Building") && it.getError() != null) {
println "\t${it.displayName}"
}