Skip to content

Instantly share code, notes, and snippets.

View macg33zr's full-sized avatar
💭
Working part time...

Bill Dennis macg33zr

💭
Working part time...
View GitHub Profile
@macg33zr
macg33zr / CustomTools.groovy
Last active September 29, 2017 15:16
Bill's Jenkins System Groovy Script KB gist
//
// Custom tools
//
import com.cloudbees.jenkins.plugins.customtools.CustomTool
import hudson.tools.InstallSourceProperty
import hudson.tools.ZipExtractionInstaller
def tools = Jenkins.instance.getDescriptorByType(CustomTool.DescriptorImpl.class)
@macg33zr
macg33zr / pipeline-disable-early-exit.groovy
Created November 28, 2016 10:43
Jenkins pipeline model definition - how to disable with an environment variable
// Check for early exit of a pipeline. The env check has to be inside a node.
boolean earlyExit = false
node {
echo "env.PIPE_DISABLED = ${env.PIPE_DISABLED}"
if(env?.PIPE_DISABLED == 'true') {
earlyExit = true
}
}
if(earlyExit) {
echo "Early exit pipeline as disabled"
@macg33zr
macg33zr / pipeline-model-multiple.groovy
Last active November 27, 2016 15:38
Pipeline model - multiple pipelines with processing outside the pipeline sections
// First pipeline declaration
pipeline {
// Run on any node
agent label: ""
stages {
stage('Section 1') {
steps {
@macg33zr
macg33zr / pipeline-job.groovy
Created November 23, 2016 02:06
Jenkins pipeline with script section calling to a vars global helper DSL
pipeline {
agent label: ""
stages {
stage('Bar') {
steps {
script {
helloTest {
message = "hello this is a test"
@macg33zr
macg33zr / pipeline-model-timestamps.groovy
Created November 22, 2016 18:06
Jenkins Pipeline Model Definition - Timestamps
pipeline {
environment {
FOO_OUT = "Foo"
}
agent label:"LNX64"
wrappers {
timestamps()
@macg33zr
macg33zr / HelloControllerTests.groovy
Last active August 29, 2015 14:27
Spring Boot Mocking Groovy REST Controller
/**
*
* How to Unit test Spring Boot REST Controller with mocks
*
* Requires Groovy mockito support dependency in build.gradle:
*
* testCompile("org.springframework.boot:spring-boot-starter-test")
*
* Otherwise mocks crash with NPE..
* See https://github.com/cyrusinnovation/mockito-groovy-support
@macg33zr
macg33zr / jenkinsMatrixToOnlineSlaves.groovy
Created August 3, 2013 23:48
Dynamically set configuration matrix on a Jenkins multi-configuration job to individual nodes with all online slaves. This would be a system groovy script in a separate job to configure the multi-configuration job. #JenkinsCI
// Name of the matrix job to configure
def jobName = 'MATRIX'
// Get the matrix job
def job = hudson.model.Hudson.instance.getItem(jobName)
assert job != null, "The job $jobName could not be found"
// Check it is a matrix job
assert job.getClass() == hudson.matrix.MatrixProject.class, "The job $jobName is of class '${job.getClass().name}', but expecting 'hudson.matrix.MatrixProject'"
@macg33zr
macg33zr / upstream-correlate-on-param.groovy
Created July 11, 2013 01:47
Jenkins System Groovy script to correlate upstream jobs on named parameter with the same value. This is to correlate jobs that are triggered externally.
//
// System groovy script to correlate upstream jobs on a parameter value matching a parameter value on this job
//
// - Have 1 - N upstream jobs which are called externally with a parameter "FOO"
// - The upstream jobs each call this job with the same parameter "FOO"
// - In this job, I want to continue some process when "FOO" has the same value for any builds of the upstream jobs
// - Hence correlating on the FOO parameter value
// - I don't want to look back too many builds on the upstream jobs
// How many builds to look back on. Set to zero for all builds.
@macg33zr
macg33zr / upstream.groovy
Created July 11, 2013 00:22
Jenkins System Groovy Script - Enumerate Upstream Projects. If the project is triggered by other projects and you need to enumerate them, this is how I found out to do it in a System Groovy build step
def upStreamProjects = build.getParent().getUpstreamProjects()
println "Found ${upStreamProjects.size()} upstream projects"
upStreamProjects.each { p -> println "upstream project: ${p.getName()}" }
@macg33zr
macg33zr / build.gradle
Last active April 28, 2018 00:53
Jenkins CLI Gradle Skeleton - what is needed to get started to use the Jenkins CLI from Gradle scripts
import hudson.cli.CLI
buildscript {
repositories {
maven {
url 'http://repo.jenkins-ci.org/repo/'
}
}
dependencies {
classpath 'org.jenkins-ci.main:cli:1.519'