Skip to content

Instantly share code, notes, and snippets.

View tizki's full-sized avatar

Tidhar Klein Orbach tizki

View GitHub Profile
@tizki
tizki / JenkinsJobInfo.groovy
Last active September 7, 2016 18:52
This script shows how to get basic information about a job and its builds
/*This script shows how to get basic information about a job and its builds*/
def jenkins = Jenkins.getInstance()
def jobName = "myJob"
def job = jenkins.getItem(jobName)
println "Job type: ${job.getClass()}"
println "Is building: ${job.isBuilding()}"
println "Is in queue: ${job.isInQueue()}"
println "Last successfull build: ${job.getLastSuccessfulBuild()}"
println "Last failed build: ${job.getLastFailedBuild()}"
@tizki
tizki / JenkinsJobActions.groovy
Last active September 7, 2016 19:04
This script shows how to get see which Actions a job has, and how to get an Action instance
/* This script shows how to get see which Actions a job has, and how to get an Action instance */
def jenkins = Jenkins.getInstance()
def jobName = "myJob"
def job = jenkins.getItem(jobName)
println "Job's actions: ${job.getActions()}" //The result is depended on which Jenkins plugins are installed
println "ParametersDefinitionProperty instance: ${job.getAction(hudson.model.ParametersDefinitionProperty.class)}"
@tizki
tizki / JenkinsJobBuildsByResult.groovy
Last active September 7, 2016 18:52
This script prints builds of a job by their result
/* This script prints builds by their result */
def jenkins = Jenkins.getInstance()
def jobName = "myJob"
def job = jenkins.getItem(jobName)
results = ["SUCCESS","UNSTABLE","FAILURE","ABORTED"]
def builfdByResult = { result -> job.getBuilds().findAll{ it.getResult().toString() == result} }
results.each { res ->
println "Builds ended with result ${res}"
println builfdByResult(res)
@tizki
tizki / JenkinsBuildInfo.groovy
Created September 7, 2016 19:38
This scripts shows how to get and print basic information about a specific build
/* This scripts shows how to get and print basic information about a specific build */
def jenkins = Jenkins.getInstance()
def jobName = "myJob"
int buildNumber = 186
def job = jenkins.getItem(jobName)
//get the build
def bld = job.getBuildByNumber(buildNumber)
//print some of the builds info
println "Build type: ${bld.getClass()}"
@tizki
tizki / JenkinsGetLabelFromNodes.groovy
Created September 13, 2016 20:27
This script prints all labels of each unix slave
/* This script prints all labels of each unix slave */
def jenkins = Jenkins.getInstance()
def nodes = jenkins.getNodes()
def unixNodes = nodes.findAll{ it.getComputer().isUnix()}
unixNodes.each{
println it.getLabelString()
}
def jenkins = Jenkins.getInstance()
def jobList = ["job1", "job2", "job3"]
//time frame is for example, you should choose a start build and an end build and take their timestamps
long startFrameTime = 1475758795672L
long endFrameTime = 1475779098244L
jobList.each{ jobName ->
def job = jenkins.getItem(jobName)
def bld = job.getBuilds().byTimestamp(startFrameTime,endFrameTime)
//The following script will add an information badge to the build. (use with groovy post build plugin)
def gitRepo = manager.build.buildVariableResolver.resolve('GIT_REPOSITORY')
def gitBranch = manager.build.buildVariableResolver.resolve('GIT_BRANCH')
manager.addBadge("text.gif", "$gitRepo / $gitBranch")
@tizki
tizki / enableDisableJobsInFolder.groovy
Created March 28, 2017 08:08
The scripts show how to enable and disable all jobs in a Jenkins folder
//The scripts show how to enable and disable all jobs in a Jenkins folder
def jenkins = Jenkins.getInstance()
def folder = jenkins.getItem("test-folder")
def jobs = folder.getAllJobs()
disableJobs = jobs.each {
println "Disabled: ${it.isDisabled()}"
@tizki
tizki / jenkinsDecrypt.groovy
Created April 2, 2017 13:10
Decrypt Jenkins hashed passwords
//This script can be used in order to decrypt password from Jenkins' credentials.xml file
//The passwords in the file are hashed using Jenkins' key. In order to un-hash them:
// 1.Copy the required password
// 2. open Jenkins script console http://your-jenkins/script
// 3. exectue the following code:
def ENCRYPTED_PASSWORD = "paste the password here"
println( hudson.util.Secret.decrypt("${ENCRYPTED_PASSWORD}") )
@tizki
tizki / getTestClassesDuration.groovy
Created September 28, 2017 14:41
prints test class FQDN name and its duration in seconds
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.junit.ClassResult;
import hudson.tasks.test.TabulatedResult;
import hudson.tasks.test.TestResult;
def jenkins = Jenkins.instance
def job = jenkins.getItem("your job")
def buildNum = 123
def bld = job.getBuildByNumber(buildNum)
//println bld.actions