Skip to content

Instantly share code, notes, and snippets.

@thorntonrose
thorntonrose / jenkins-remote-script.sh
Created August 11, 2022 18:18
Run Jenkins script remotely
COOKIE_JAR=/tmp/cookies
CRUMB=$(curl -s --cookie-jar $COOKIE_JAR -u $JENKINS_USER:$JENKINS_TOKEN $JENKINS_URL/crumbIssuer/api/json | \
jq -r .crumb)
curl -i --cookie $COOKIE_JAR -u $JENKINS_USER:$JENKINS_TOKEN \
-H "Jenkins-Crumb:${CRUMB}" -d "script=println 'foo'" \
$JENKINS_URL/scriptText \
@thorntonrose
thorntonrose / Jenkins.md
Last active August 9, 2022 15:34
Jenkins scripts

get secret text:

echo withCredentials([string(credentialsId: "<id>", variable: "s")]) { s }

get username and password:

println withCredentials([usernamePassword(credentialsId: "<id>", usernameVariable: "u", passwordVariable: "p")]) { [u, p] }
@thorntonrose
thorntonrose / ToAlphanumericTest.groovy
Last active July 31, 2018 19:35
Jenkins Lib Testing - ToAlphanumericTest.groovy
import org.junit.*
import com.lesfurets.jenkins.unit.*
import static groovy.test.GroovyAssert.*
class ToAlphanumericTest extends BasePipelineTest {
def toAlphanumeric
@Before
void setUp() {
super.setUp()
@thorntonrose
thorntonrose / ConvertLongTime.groovy
Created April 21, 2018 16:37
Convert long time value to Java or ISO string format
import groovy.util.*
import java.text.*
def cli = new CliBuilder(usage: "ConvertLongTime [options] <time>")
cli._(longOpt: "iso", "ISO format")
def opts = cli.parse(args)
if (!opts) { return }
if (!opts.arguments()) {
cli.usage()
@thorntonrose
thorntonrose / VelocityToHours.groovy
Last active April 21, 2018 16:32
Convert velocity to hours
// calculate hoursPerPoint based on velocity
println "Base:"
daysPerSprint = 10
println "daysPerSprint = $daysPerSprint"
teamSize = 10 // not incl. ben, otis, adrian, support person
println "teamSize = $teamSize"
@thorntonrose
thorntonrose / Time.groovy
Created March 28, 2016 20:19
Time utility methods
class Time {
static final INSTANT_PATTERN =
~/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z(|[\-\+]{1}[0-9]{4})?/
static timingEnabled = false
static console = true
private Time() {}
static toGregorianCalendar(Instant instant) {
instant ? GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneOffset.UTC)) : null
@thorntonrose
thorntonrose / RestClient.groovy
Last active August 12, 2022 08:45
Simple Groovy REST client
class RestClient {
def proxy = Proxy.NO_PROXY
def basicAuth
// ???: Bake this into request()?
static def encodePath(path) {
path.replaceAll(" ", "%20")
}
def getContent(String url) {
@thorntonrose
thorntonrose / Dates.groovy
Created March 28, 2016 19:57
Date utility methods
import java.text.*
class Dates {
static final DATE_TIME = "yyyy-MM-dd HH:mm:ss"
static final EUROPEAN = "dd/MM/yyyy"
static final HSQLDB = "yyyy-MM-dd HH:mm:ss.SSS"
static final ISO = "yyyy-MM-dd'T'HH:mm:ss.SSS"
static final YEAR_MONTH_DAY = "yyyy-MM-dd"
static final YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd (HH:mm)"
static final MONTH_DAY_YEAR = "MM/dd/yyyy"
@thorntonrose
thorntonrose / CsvDoc.groovy
Created March 28, 2016 19:55
Class that represents a CSV (comma separated values) document
class CsvDoc {
def doc = []
def setHeader(titles) {
doc = [ toCsv(titles) ] + doc
}
def addRow(values) {
doc << toCsv(values)
}
@thorntonrose
thorntonrose / SmtpMailer.java
Last active March 27, 2016 01:03
Send mail via SMTP
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
/**
* SmtpMailer is used to send mail using SMTP.
*
* @author Thornton Rose
* @version 1.0