Skip to content

Instantly share code, notes, and snippets.

@grem11n
Last active March 18, 2023 12:54
Show Gist options
  • Save grem11n/eb22d8411cba32b8f1a95ea74b404b4e to your computer and use it in GitHub Desktop.
Save grem11n/eb22d8411cba32b8f1a95ea74b404b4e to your computer and use it in GitHub Desktop.

Revisions

  1. grem11n revised this gist Dec 26, 2018. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    #!/usr/bin/env groovy
    // Load Shared Libraries
    // Load the shared libraries
    @Library('jenkins-shared-libraries')_

    import static groovy.io.FileType.FILES

    def homeChange = false // holds status of the check if anything changed in the repo root

    // Load child Jenkinsfiles based on diff
    def loadDiff() {
    dirs = []
    loads = [:]
    matches = sh(returnStdout:true, script: "git diff --name-only `git merge-base origin/${env.CHANGE_TARGET} HEAD`") // Check diff with best parent hash, not with origin/branch
    homeChange = false
    files = findFiles(glob: '**/Jenkinsfile')
    matches = sh(returnStdout:true, script: "git diff --name-only `git merge-base origin/${env.CHANGE_TARGET} HEAD`")
    match_list = matches.tokenize('\n')
    match_list.each { match ->
    if (match.tokenize('/').size() == 1) {
    @@ -19,20 +19,23 @@ def loadDiff() {
    dirs << match.tokenize('/').take(1).join('/') // Each service should be in its own directory within monorepo
    }
    }
    uniqDirs = dirs.unique()
    files = findFiles(glob: '**/Jenkinsfile')
    uniqDirs.each() { dir ->
    files.each() { file ->
    if (file.path =~ dir) {
    loads["Service ${dir}"] = { load file.path }
    if (homeChange) {
    loadAll()
    } else {
    uniqDirs = dirs.unique()
    uniqDirs.each() { dir ->
    files.each() { file ->
    if (file.path =~ dir) {
    loads["Service ${dir}"] = { load file.path }
    }
    }
    }
    loads.failFast = false // Build other services even if one of them failed
    parallel loads // Build all services in parallel
    }
    loads.failFast = false // Build other services even if one of them failed
    parallel loads // Build all services in parallel
    }

    // Load all Jenkinsfiles within the repo. Useful for master branch builds, etc.
    // Load all Jenkinsfiles within the repo for master branch builds, etc.
    def loadAll() {
    loads = [:]
    files = findFiles(glob: '**/Jenkinsfile')
    @@ -80,7 +83,7 @@ def stopOnDeploy() {
    }

    node {
    cleanWs disableDeferredWipeout:true // clean workspace before checkout. Workspace-cleanup plugin is required
    cleanWs disableDeferredWipeout:true // clean workspace before checkout. Workspace-cleanup plugin is required
    checkout scm
    try {
    if (env.CHANGE_ID != null) {
    @@ -102,6 +105,6 @@ node {
    }
    throw err
    } finally {
    cleanWs disableDeferredWipeout:true
    cleanWs disableDeferredWipeout:true // clean workspace before checkout. Workspace-cleanup plugin is required
    }
    }
  2. grem11n revised this gist Dec 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def loadAll() {
    files = findFiles(glob: '**/Jenkinsfile')
    files.each() { file ->
    if (file.path.tokenize('/').size() != 1) {
    name = file.path.replaceAll('/Jenkinsfile', '')
    name = file.path.replaceAll('/Jenkinsfile', '') // Assuming service directory name == service name
    loads["Service ${name}"] = { load file.path }
    }
    }
  3. grem11n created this gist Dec 20, 2018.
    107 changes: 107 additions & 0 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    #!/usr/bin/env groovy
    // Load Shared Libraries
    @Library('jenkins-shared-libraries')_

    import static groovy.io.FileType.FILES

    def homeChange = false // holds status of the check if anything changed in the repo root

    // Load child Jenkinsfiles based on diff
    def loadDiff() {
    dirs = []
    loads = [:]
    matches = sh(returnStdout:true, script: "git diff --name-only `git merge-base origin/${env.CHANGE_TARGET} HEAD`") // Check diff with best parent hash, not with origin/branch
    match_list = matches.tokenize('\n')
    match_list.each { match ->
    if (match.tokenize('/').size() == 1) {
    homeChange = true
    } else {
    dirs << match.tokenize('/').take(1).join('/') // Each service should be in its own directory within monorepo
    }
    }
    uniqDirs = dirs.unique()
    files = findFiles(glob: '**/Jenkinsfile')
    uniqDirs.each() { dir ->
    files.each() { file ->
    if (file.path =~ dir) {
    loads["Service ${dir}"] = { load file.path }
    }
    }
    }
    loads.failFast = false // Build other services even if one of them failed
    parallel loads // Build all services in parallel
    }

    // Load all Jenkinsfiles within the repo. Useful for master branch builds, etc.
    def loadAll() {
    loads = [:]
    files = findFiles(glob: '**/Jenkinsfile')
    files.each() { file ->
    if (file.path.tokenize('/').size() != 1) {
    name = file.path.replaceAll('/Jenkinsfile', '')
    loads["Service ${name}"] = { load file.path }
    }
    }
    loads.failFast = false // Build other services even if one of them failed
    parallel loads // Build all services in parallel
    }

    // Stop running build for PullRequest if something new was pushed. Just like Travis-CI does.
    def abortPreviousBuilds() {
    /*
    Author: Isaac S Cohen
    Modification: Abort only previous builds
    */

    jobname = env.JOB_NAME
    buildnum = env.BUILD_NUMBER.toInteger()

    job = Jenkins.instance.getItemByFullName(jobname)
    for (build in job.builds) {
    if (buildnum > build.getNumber().toInteger()) { build.doStop(); }
    }
    }

    // Check if build for Tag is in progress and abort the build to avoid race conditions.
    def stopOnDeploy() {
    /*
    Check if Another Tag build is in progress
    */
    identifier = "${env.TAG_NAME}".replaceAll("[0-9]+|-", "") + "-[0-9]+"
    runningJobs = Jenkins.instance.getView('All').getBuilds().findAll() { it.getResult().equals(null) }

    runningJobs.each { job ->
    if (job.getParent().getDisplayName() =~ identifier && job.getParent().getDisplayName() != "${env.TAG_NAME}" && job.getId().toInteger() < "${env.BUILD_NUMBER}".toInteger()) {
    println "Someone deploying Prod. Aborting"
    currentBuild.result = 'ABORTED'
    error('Another Prod deploy is in progess. Aborting')
    }
    }
    }

    node {
    cleanWs disableDeferredWipeout:true // clean workspace before checkout. Workspace-cleanup plugin is required
    checkout scm
    try {
    if (env.CHANGE_ID != null) {
    abortPreviousBuilds()
    loadDiff()
    } else {
    loadAll()
    }
    if (env.BRANCH_NAME == 'master') {
    slackSend (color: '#28aa78', message: "SUCCESSFUL: Job '${env.JOB_NAME} for '${env.BRANCH_NAME}' [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    } else if (env.BRANCH_NAME =~ 'PR-.*') {
    slackSend (color: '#28aa78', message: "SUCCESSFUL: Congrats, ${env.CHANGE_AUTHOR}! Job '${env.JOB_NAME} for 'PR-${env.CHANGE_ID}' [${env.BUILD_NUMBER}]' (${env.BUILD_URL}) passed")
    }
    } catch(err) {
    if (env.BRANCH_NAME == 'master') {
    slackSend (color: '#a00000', message: "FAILED: Job '${env.JOB_NAME} for '${env.BRANCH_NAME}' [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    } else if (env.BRANCH_NAME =~ 'PR-.*') {
    slackSend (color: '#a00000', message: "FAILED: Hey! ${env.CHANGE_AUTHOR}! Job '${env.JOB_NAME} for 'PR-${env.CHANGE_ID}' [${env.BUILD_NUMBER}]' (${env.BUILD_URL}) failed!")
    }
    throw err
    } finally {
    cleanWs disableDeferredWipeout:true
    }
    }