Skip to content

Instantly share code, notes, and snippets.

@t3knoid
t3knoid / JenkinsPipeline_updateJira.groovy
Last active May 31, 2022 15:27
Jenkins pipeline script to update Jira fields example
/**
* Updates Jira Fixed in build field with the build number and adds a comment in each related Jira issue
* This requires the Jenkins JIRA Pipeline Steps plugin https://jenkinsci.github.io/jira-steps-plugin/getting-started/
* @param build Build number that will be entered in the "Fixed in Build" Jira field
*/
def updateJira(build) {
def jiraServer = 'JIRA-PROD' // Define a Jira server entry in the Jenkins Jira Steps configuration named JIRA-PROD
def jiraIssues = jiraIssueSelector(issueSelector: [$class: 'DefaultIssueSelector'])
jiraIssues.each { issue ->
jiraAddComment comment: "{panel:bgColor=#97FF94}{code}Code was added to address this issue in build ${build}{code} {panel}", idOrKey: issue, site: jiraServer
@t3knoid
t3knoid / JenkinsPipeline_getJiraIssuesList.groovy
Created February 2, 2018 16:15
Jenkins pipeline script that will create an HTML formatted list of Jira issues
def getJiraIssueList() {
/**
* Returns an HTML formatted list of Jira issues related in the build.
* This requires the Jenkins JIRA Pipeline Steps plugin https://jenkinsci.github.io/jira-steps-plugin/getting-started/
* @param None
* @return An HTML string containing Jira issues
*/
def jiraServer = 'JIRA-PROD' // Define a Jira server entry in the Jenkins Jira Steps configuration named JIRA-PROD
def jiraURL = "http://my.jiraserver.com:8080" // Define the Jira URL
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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()