Skip to content

Instantly share code, notes, and snippets.

@epishan
Last active September 7, 2016 13:05
Show Gist options
  • Select an option

  • Save epishan/2fd33944541585cbb38a094cec58aea5 to your computer and use it in GitHub Desktop.

Select an option

Save epishan/2fd33944541585cbb38a094cec58aea5 to your computer and use it in GitHub Desktop.

Revisions

  1. epishan revised this gist Sep 7, 2016. 1 changed file with 11 additions and 12 deletions.
    23 changes: 11 additions & 12 deletions get_last_branch.groovy
    Original file line number Diff line number Diff line change
    @@ -6,18 +6,17 @@ println "currentJob --> ${currentJob}"
    println "currentBuild --> ${currentBuild}"

    if (branch=="") {
    try {
    println "setting BRANCH from the last build"
    def lastBuild = currentJob.lastSuccessfulBuild
    println "lastBuild -> ${lastBuild}"
    //branch = lastBuild.getBuildVariables().get("BRANCH");
    def git_branch = lastBuild.envVars['GIT_BRANCH']
    branch = git_branch.split('/',2)[1]
    } catch (error) {
    println "ERROR -> ${error}"
    branch=""
    } finally {
    }
    try {
    println "setting BRANCH from the last build"
    def lastBuild = currentJob.lastSuccessfulBuild
    println "lastBuild -> ${lastBuild}"
    //branch = lastBuild.getBuildVariables().get("BRANCH");
    def git_branch = lastBuild.envVars['GIT_BRANCH']
    branch = git_branch.split('/',2)[1]
    } catch (error) {
    println "ERROR -> ${error}"
    branch=""
    }
    }

    println "currentBranch --> ${branch}"
  2. epishan created this gist Sep 7, 2016.
    25 changes: 25 additions & 0 deletions get_last_branch.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    // this script will reuse GIT_BRANCH variable from the last successful job, if current BRANCH parameter is empty

    def branch="$BRANCH"

    println "currentJob --> ${currentJob}"
    println "currentBuild --> ${currentBuild}"

    if (branch=="") {
    try {
    println "setting BRANCH from the last build"
    def lastBuild = currentJob.lastSuccessfulBuild
    println "lastBuild -> ${lastBuild}"
    //branch = lastBuild.getBuildVariables().get("BRANCH");
    def git_branch = lastBuild.envVars['GIT_BRANCH']
    branch = git_branch.split('/',2)[1]
    } catch (error) {
    println "ERROR -> ${error}"
    branch=""
    } finally {
    }
    }

    println "currentBranch --> ${branch}"
    def map = [BRANCH: "${branch}"]
    return map