Skip to content

Instantly share code, notes, and snippets.

@t3knoid
t3knoid / getEnvironmentVars.groovy
Last active February 13, 2018 22:32
Jenkins Groovy script to return build environment variables
// This does not work if Use Groovy Sandbox is enabled
// Execute this as a system Groovy script in Jenkins
def thr = Thread.currentThread()
def build = thr?.executable
def env = build.parent.builds[0].properties.get("envVars")
env.each{
println it
}
@t3knoid
t3knoid / JenkinsStopJob.groovy
Created February 13, 2018 20:52
Jenkins groovy script to stop a running job
// Post to stop a running Jenkins job
build = 63
jobName = // Set job name here
jlc=JenkinsLocationConfiguration.get() // Get Jenkins base URL from configuration
jenkinsBase = jlc.getUrl()
addr = "${jenkinsBase}job/${jobName}/${build}/stop" // Stop URL
authString = "frefol:d0882d0d535f52cc87f3ac3694d7ad61".getBytes().encodeBase64().toString() // Use authentication token
println("${addr}")
try {
@t3knoid
t3knoid / JenkinsGetURL.groovy
Last active February 13, 2018 19:47
Jenkins get the Jenkins URL from the configuration
// The following snippet shows how to get the Jenkins Application URL
// from the Jenkins configuration settings.
// The following must be approved in the Jenkins In-process Script Approval
//
// staticMethod jenkins.model.JenkinsLocationConfiguration get
// method jenkins.model.JenkinsLocationConfiguration getUrl
jlc=JenkinsLocationConfiguration.get()
echo jlc.getUrl()
@t3knoid
t3knoid / makeLNK.vbs
Created February 13, 2018 14:41
A VBScript that creates a shortcut in Windows
set objWSHShell = CreateObject("WScript.Shell")
'===========================================
' usage: cscript makeLNK.vbs shortcut target
'============================================
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
set objSC = objWSHShell.CreateShortcut(sShortcut)
@t3knoid
t3knoid / JenkinsGetLastJobBuildNumber.groovy
Last active April 3, 2021 08:31
A Groovy script that will store the build number of a Jenkins job's last build into a variable
def address = "http://{jenkinsBase}/job/${jobName}/lastBuild/buildNumber"
try {
println("${address}")
def urlInfo = address.toURL()
response = urlInfo.openConnection()
if (response.getResponseCode() != 200) {
throw new Exception("Unable to connect to " + address) // Throw an exception to get out of loop if response is anything but 200
}
buildNum= response.getInputStream().getText()
@t3knoid
t3knoid / JenkinsTailJobConsole.py
Last active February 12, 2018 21:19
Pregressively read a Jenkins job console using Python
import urllib
import urllib2
import time
import sys
JENKINS_BASE = # Set to Jenkins base URL
JENKINS_JOB = # Set to Jenkins job
start = 0 #
job_number='lastBuild' # Get the last build
@t3knoid
t3knoid / JenkinsTailJobConsole.groovy
Created February 12, 2018 18:28
Progressively read a Jenkins job console output using Groovy
def jenkinsBase = // Set to Jenkins base URL
def jenkinsJob = // Set to Jenkins job name
def address = null
def response = null
def start = 0 // Start at offset 0
def cont = true // This semaphore holds the value of X-More-Data header value
try {
while (cont == true) { // Loop while X-More-Data value is equal to true
address = "${jenkinsBase}/job/${jenkinsJob}/lastBuild/logText/progressiveText?start=${start}"
println("${address}")
@t3knoid
t3knoid / DirToExcel.txt
Created February 2, 2018 17:00
Tip on how to get the directory output into Microsoft Excel
These are instructions on how to get DOS directory output into Microsoft Excel.
1. Pipe directory output to a file, dir /A-D > files.txt
2. Open the file into Notepad and copy the rows containing the file listing.
3. Open Microsoft Excel.
4. Open the Home menu and click on the Paste down arrow to show past options.
5. Select "Use Text Import Wizard."
From this point on, use the Text Import Wizard to import the text into Microsoft Excel. Defaults should be sufficient.
@t3knoid
t3knoid / JenkinsPipeline_printParams.groovy
Created February 2, 2018 16:50
Jenkins pipeline script that prints environment variables to the console
/**
* Prints environment variables and their values in the console
*/
def printParams() {
bat 'set > env.txt'
for (String i : readFile('env.txt').split("\r?\n")) {
println i
}
}
@t3knoid
t3knoid / JenkinsPipeline_deployToCIFS.groovy
Created February 2, 2018 16:39
Jenkins pipeline script that will copy build artifacts to a CIFS folder
def deployToCIFS(prefix = "", cifsConfig, destination) {
/**
* Copies file artifacts to a given CIFS share and folder.
* This requires the Jenkins Publish over CIFS plugin https://wiki.jenkins.io/display/JENKINS/Publish+Over+CIFS+Plugin
* @param prefix The file artifact's folder prefix. Everything after this prefix will be copied to the destination. The destination will not include this folder structure. Note that this is case-sensitive.
* @cifsConfig The CIFS configuration defined in the Jenkins Publish over CIFS system configuration.
* @destination The folder appended to the CIFS share to create a fully qualified path of the destination.
*/
cifsConfig = 'myShare' // Define a share named "myShare" in the Jenkins Publish over CIFS system configuration
srcFiles = "${prefix}/**/**" // Copy everything after prefix