Create a gist now

Instantly share code, notes, and snippets.

@dnozay /README.md
Last active Jul 17, 2018

Embed
What would you like to do?
jenkins groovy scripts collection.
// Licensed under MIT
// author : Damien Nozay
// for all builds from build-flow-plugin whose parameters include a GIT_BRANCH paramater,
// change the displayName to include branch and build number
import com.cloudbees.plugins.flow.*;
jobs = Jenkins.instance.getAllItems(BuildFlow);
jobs.each { it ->
it.builds.each { b ->
GIT_BRANCH = b.envVars['GIT_BRANCH']
( GIT_BRANCH =~ /(?:refs\/remotes\/)?(.+)/ ).each { full,branch ->
b.displayName = branch + ' (#' + b.number + ')'
}
}
}
// Licensed under MIT
// author : Damien Nozay
// ---------------------------------------------------------
// This script goes through all the jobs and checks the disk usage.
// prints some disk usage stats as well as the retention policy.
// ---------------------------------------------------------
// e.g.:
//
// JOB: playground/test-python
// -> lastbuild: #1 = FAILURE, time: 2015-02-12T20:56:16Z
// -> builds=12, average=8 KB, max=9 KB, total=97 KB, worstCase=113 KB
//
//
def printDiscarder(job) {
d = job.buildDiscarder
if (d) {
println(" -> keep: builds=(${d.daysToKeep} days, ${d.numToKeep} total); artifacts=(${d.artifactDaysToKeep} days, ${d.artifactNumToKeep} total)")
} else {
println(" -> no retention policy.")
}
}
import hudson.plugins.disk_usage.BuildDiskUsageAction
import hudson.plugins.disk_usage.DiskUsageUtil
def getDiskUsage(build) {
usage = null
build.getTransientActions().each {
action ->
if (action instanceof BuildDiskUsageAction) {
// println action.buildUsageString
// println action.allDiskUsage
usage = action
}
}
return usage
}
def printDiskUsage(job) {
maxUsage = 0
totalUsage = 0
numBuilds = 0
job.builds.each() {
build ->
usage = getDiskUsage(build)
if (usage.allDiskUsage > maxUsage) { maxUsage = usage.allDiskUsage }
totalUsage += usage.allDiskUsage
numBuilds += 1
// println(" * build ${build.number} - ${usage.buildUsageString}")
}
averageUsage = 0
if (numBuilds) { averageUsage = (totalUsage / numBuilds).longValue() }
worstCase = numBuilds * maxUsage
println(" -> builds=${numBuilds}, average=${DiskUsageUtil.getSizeString(averageUsage)}, max=${DiskUsageUtil.getSizeString(maxUsage)}, total=${DiskUsageUtil.getSizeString(totalUsage)}, worstCase=${DiskUsageUtil.getSizeString(worstCase)}")
}
jobs = Jenkins.instance.getAllItems()
jobs.each { j ->
if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) { return }
numbuilds = j.builds.size()
println 'JOB: ' + j.fullName
if (numbuilds == 0) {
println ' -> no build'
} else {
lastbuild = j.builds[numbuilds - 1]
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', time: ' + lastbuild.timestampString2
}
printDiscarder(j)
printDiskUsage(j)
println ''
}
''
// Licensed under MIT
// author : Damien Nozay
// scan all jobs and check if the last build was aborted (e.g. maintenance)
// and output user / timestamp
jobs = Jenkins.instance.getAllItems()
jobs.each { j ->
if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) { return }
numbuilds = j.builds.size()
if (numbuilds == 0) { return }
lastbuild = j.builds[numbuilds - 1]
if (lastbuild.result == Result.ABORTED) {
println 'JOB: ' + j.fullName
abortCause = lastbuild.getAction(InterruptedBuildAction).getCauses()[0]
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', cause: ' + abortCause.shortDescription + ', time: ' + lastbuild.timestampString2
}
}
''
// Licensed under MIT
// author : Damien Nozay
// list jobs and their last build.
jobs = Jenkins.instance.getAllItems()
jobs.each { j ->
if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) { return }
println 'JOB: ' + j.fullName
numbuilds = j.builds.size()
if (numbuilds == 0) {
println ' -> no build'
return
}
lastbuild = j.builds[numbuilds - 1]
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', time: ' + lastbuild.timestampString2
}
// returns blank
''
// Licensed under MIT
// author : Damien Nozay
// list jobs not run in the last N days / last N months
import groovy.time.TimeCategory
use ( TimeCategory ) {
// e.g. find jobs not run in last 3 months
sometimeago = (new Date() - 3.months)
}
jobs = Jenkins.instance.getAllItems()
lastabort = null
jobs.each { j ->
if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) { return }
numbuilds = j.builds.size()
if (numbuilds == 0) {
println 'JOB: ' + j.fullName
println ' -> no build'
return
}
lastbuild = j.builds[numbuilds - 1]
if (lastbuild.timestamp.getTime() < sometimeago) {
println 'JOB: ' + j.fullName
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', time: ' + lastbuild.timestampString2
}
}
''
// Licensed under MIT
// author : Damien Nozay
// ---------------------------------------------------------
// This script cleans a subdir in all existing workspaces for a selected job.
// node -> check workspace (concurrent too) -> check subdir -> delete
// ---------------------------------------------------------
job = Jenkins.instance.getItemByFullName('SomeJobFolder/myJob')
subdir = 'dist'
println "Looking for job: " + job.fullName
import hudson.slaves.WorkspaceList
combinator = System.getProperty(WorkspaceList.class.getName(),"@");
for (node in Jenkins.instance.getNodes()) {
println "Node: '" + node.getSelfLabel().toString() + "' (" + node.getAssignedLabels().join(",") + ")"
workspacePathBase = node.getWorkspaceFor(job)
// handle concurrent workspaces
for (int i=1; ; i++) {
// they are suffixed...
workspacePath = i==1 ? workspacePathBase : workspacePathBase.withSuffix(combinator+i);
// stop checking (unlikely to have higher suffix)
if (!workspacePath.exists()) {
break;
} else {
println " * found workspace: " + workspacePath.getRemote()
targetDir = workspacePath.child(subdir)
if (targetDir.exists()) {
println " * found target directory"
if (!job.isBuilding()) {
println " * removing directory (job is not building)"
targetDir.deleteRecursive()
} else {
println " * not removing directory (job is building)"
}
}
}
}
}
// Licensed under MIT
// author : Damien Nozay
// ---------------------------------------------------------
// Retroactively add badges to promoted builds.
// ---------------------------------------------------------
import hudson.plugins.promoted_builds.*;
import org.jvnet.hudson.plugins.groovypostbuild.*;
// customize these
project_name = "project/full/name"
promotion_name = "promotion_process_name"
// look up promoted builds for project + promotion process.
project = Jenkins.instance.getItemByFullName(project_name)
action = project.getAction(PromotedProjectAction.class)
promotion = action.getProcess(promotion_name)
promoted_builds = action.getPromotions(promotion)
// check this other gist:
// https://gist.github.com/dnozay/fc528b43cf27755017cc
def add_release_version(promo_build) {
target = promo_build.target;
// access the promotion build environment and the RELEASE_VERSION parameter.
release_version = promo_build.environment.get('RELEASE_VERSION');
// create the summary with the gold star icon and attach to target.
GroovyPostbuildSummaryAction action = new GroovyPostbuildSummaryAction("star-gold.png");
target.getActions().add(action);
// customize text for the summary.
action.appendText("RELEASE VERSION = " + release_version, false);
// also add a short text that will appear in the build history
target.getActions().add(GroovyPostbuildAction.createShortText(release_version));
// save build
target.save();
}
// do stuff; e.g. add release version in the description.
for (Promotion promo: promoted_builds) {
add_release_version(promo)
}
// Licensed under MIT
// author : Damien Nozay
// ---------------------------------------------------------
// This script goes through all the jobs and checks if they are using the Groovy Postbuild
// if they are, then it computes the hash value, and checks against the ones that are approved.
// ---------------------------------------------------------
import org.jenkinsci.plugins.scriptsecurity.scripts.*;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.*;
import hudson.model.RootAction;
import org.jvnet.hudson.plugins.groovypostbuild.*;
// instance containing the approvals
// list of approved hashes: println instance.approvedScriptHashes
ScriptApproval instance = Jenkins.getInstance().getExtensionList(RootAction.class).get(ScriptApproval.class);
approvedScriptHashes = instance.approvedScriptHashes
import java.util.*
import java.security.MessageDigest;
def hash(String script, String language) {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(language.getBytes("UTF-8"));
digest.update((byte) ':');
digest.update(script.getBytes("UTF-8"));
return Util.toHexString(digest.digest());
}
jobs = hudson.model.Hudson.instance.getAllItems(FreeStyleProject)
for (job in jobs) {
for (publisher in job.publishersList) {
if (publisher instanceof GroovyPostbuildRecorder) {
hashval = hash(publisher.script.script, "groovy")
println "#" * 80
println "job: " + job.getFullName()
println "script hash: " + hashval
println "approved: " + (hashval in approvedScriptHashes)
println "script: "
println "-" * 80
println publisher.script.script
println "#" * 80
}
}
}
@nee2reddy

This comment has been minimized.

Show comment
Hide comment
@nee2reddy

nee2reddy Sep 8, 2016

Hi dnozay, that's a great collection of scripts you got. I am working on something similar. With the help of your lastbuild.groovy, I can get the job list but how do I get the workspace for it ? Instead of looking for last build I am searching for .lastSuccessfulBuild.workspace . Any pointers would be great help.Thanks

Hi dnozay, that's a great collection of scripts you got. I am working on something similar. With the help of your lastbuild.groovy, I can get the job list but how do I get the workspace for it ? Instead of looking for last build I am searching for .lastSuccessfulBuild.workspace . Any pointers would be great help.Thanks

@devishreee

This comment has been minimized.

Show comment
Hide comment
@devishreee

devishreee Dec 17, 2016

You ave got amazing collection of groovy scripts please do provide me a script to delete manually promoted build in Jenkins.
Script to clear the workspace in a slave of a manually aborted job connected through jnlp protocol.

devishreee commented Dec 17, 2016

You ave got amazing collection of groovy scripts please do provide me a script to delete manually promoted build in Jenkins.
Script to clear the workspace in a slave of a manually aborted job connected through jnlp protocol.

@vikaschenny

This comment has been minimized.

Show comment
Hide comment
@vikaschenny

vikaschenny Jul 20, 2017

hi can any one explain this Problem statement?

During CI/CD, we need to deploy builded artifacts to one of the node in a pool of 1..N nodes where N
is some integer number.
Write a script in any language to allocate/free up node number.

Script name manage_node.[extension]

Argument (Mandatory): An data file where node state are maintained, say file name is

“node_db.txt”

Argument (Optional)

function allocate_node(file):
// read file and allocate node number which is AVAILABLE and mark it BUSY
// return <node_number>
function freeup_node(number, file):
// read file and mark node number AVAILABLE
// return True/False (boolean)

Initial data file contain two columns <NODE_NUMBER> , e.g as below
1 BUSY
2 AVAILABLE
3 AVAILABLE
4 BUSY
5 BUSY
6 AVAILABLE
Test case A (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “2 BUSY” in node_db.txt
OUTPUT: 2
After test case A run, content of data file
1 BUSY
2 BUSY
3 AVAILABLE
4 BUSY
5 BUSY
6 AVAILABLE
Test case B (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “3 BUSY” in node_db.txt
OUTPUT: 3
After test case B run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 AVAILABLE
Test case C (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “6 BUSY” in node_db.txt
OUTPUT: 6
After test case C run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case D (free up node):
INPUT: node_db.txt 2
BEHAVIOR: Free up node number 2 by writing “2 AVAILABLE” in node_db.txt
OUTPUT: True
After test case D run, content of data file
1 BUSY
2 AVAILABLE
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case E: (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate first available node and write “2 BUSY” to file
OUTPUT: 2
After test case E run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case F: (Allocate node):
INPUT: node_db.txt
BEHAVIOR: No nodes available
OUTPUT: -1

hi can any one explain this Problem statement?

During CI/CD, we need to deploy builded artifacts to one of the node in a pool of 1..N nodes where N
is some integer number.
Write a script in any language to allocate/free up node number.

Script name manage_node.[extension]

Argument (Mandatory): An data file where node state are maintained, say file name is

“node_db.txt”

Argument (Optional)

function allocate_node(file):
// read file and allocate node number which is AVAILABLE and mark it BUSY
// return <node_number>
function freeup_node(number, file):
// read file and mark node number AVAILABLE
// return True/False (boolean)

Initial data file contain two columns <NODE_NUMBER> , e.g as below
1 BUSY
2 AVAILABLE
3 AVAILABLE
4 BUSY
5 BUSY
6 AVAILABLE
Test case A (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “2 BUSY” in node_db.txt
OUTPUT: 2
After test case A run, content of data file
1 BUSY
2 BUSY
3 AVAILABLE
4 BUSY
5 BUSY
6 AVAILABLE
Test case B (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “3 BUSY” in node_db.txt
OUTPUT: 3
After test case B run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 AVAILABLE
Test case C (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate node number which is first AVAILABLE and write “6 BUSY” in node_db.txt
OUTPUT: 6
After test case C run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case D (free up node):
INPUT: node_db.txt 2
BEHAVIOR: Free up node number 2 by writing “2 AVAILABLE” in node_db.txt
OUTPUT: True
After test case D run, content of data file
1 BUSY
2 AVAILABLE
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case E: (Allocate node):
INPUT: node_db.txt
BEHAVIOR: Allocate first available node and write “2 BUSY” to file
OUTPUT: 2
After test case E run, content of data file
1 BUSY
2 BUSY
3 BUSY
4 BUSY
5 BUSY
6 BUSY
Test case F: (Allocate node):
INPUT: node_db.txt
BEHAVIOR: No nodes available
OUTPUT: -1

@Jimilian

This comment has been minimized.

Show comment
Hide comment
@Jimilian

Jimilian Jul 31, 2017

Just small reminder for all users numbuilds = j.builds.size(); j.builds[numbuilds - 1] is very expensive operation, better to use j.getLastBuild that comes for free.

Just small reminder for all users numbuilds = j.builds.size(); j.builds[numbuilds - 1] is very expensive operation, better to use j.getLastBuild that comes for free.

@devisr

This comment has been minimized.

Show comment
Hide comment
@devisr

devisr Jan 5, 2018

Hi guys,

Can someone please help me with a script to disconnect & reconnect a jenkins single node(windows slave) connected with JNLP. This script should be executed on a particular windows slave not on Scriptler as I am not the Jenkins administrator.

Thanks in advance

devisr commented Jan 5, 2018

Hi guys,

Can someone please help me with a script to disconnect & reconnect a jenkins single node(windows slave) connected with JNLP. This script should be executed on a particular windows slave not on Scriptler as I am not the Jenkins administrator.

Thanks in advance

@vikramgudi89

This comment has been minimized.

Show comment
Hide comment
@vikramgudi89

vikramgudi89 Feb 19, 2018

hi can any one help me on
Pipeline capabilities usage (how many use sonar, how many use nexus, etc)
How many apps have a pipeline at all
jenkins usage (# of builds per week, lines of code, etc whatever we can get)

hi can any one help me on
Pipeline capabilities usage (how many use sonar, how many use nexus, etc)
How many apps have a pipeline at all
jenkins usage (# of builds per week, lines of code, etc whatever we can get)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment