Skip to content

Instantly share code, notes, and snippets.

@gcaaa31928
Forked from ftclausen/Jenkinsfile
Created February 24, 2020 05:26
Show Gist options
  • Save gcaaa31928/0a7d84b7a69e9bc204dba60bcb45ff00 to your computer and use it in GitHub Desktop.
Save gcaaa31928/0a7d84b7a69e9bc204dba60bcb45ff00 to your computer and use it in GitHub Desktop.

Revisions

  1. @ftclausen ftclausen created this gist Apr 13, 2017.
    37 changes: 37 additions & 0 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // -*- mode: groovy -*-
    // vim: set filetype=groovy :

    node( 'some_node' ) {
    stage( "Phase 1" ) {
    sshagent( credentials: [ 'some_creds' ] ) {
    checkout scm
    def lastSuccessfulCommit = getLastSuccessfulCommit()
    def currentCommit = commitHashForBuild( currentBuild.rawBuild )
    if (lastSuccessfulCommit) {
    commits = sh(
    script: "git rev-list $currentCommit \"^$lastSuccessfulCommit\"",
    returnStdout: true
    ).split('\n')
    println "Commits are: $commits"
    }
    }
    }
    }

    def getLastSuccessfulCommit() {
    def lastSuccessfulHash = null
    def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
    if ( lastSuccessfulBuild ) {
    lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
    }
    return lastSuccessfulHash
    }

    /**
    * Gets the commit hash from a Jenkins build object, if any
    */
    @NonCPS
    def commitHashForBuild( build ) {
    def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
    return scmAction?.revision?.hash
    }