12

The Git Plugin is installed (by default) in my Jenkins but I'm unable to get the env variables that are supposed to be passed in by the Git Plugin. I'm looking for:

GIT_COMMIT
GIT_BRANCH
GIT_PREVIOUS_COMMIT 
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

etc. I'm using the Pipeline Job Item that's pointing at a Github repo with the Jenkinsfile with the following code

stage 'PushToProd'
node {
    git url: "https://github.com/username/fakeurl.git"
    echo "Starting PushToProd"
    sh 'printenv'
    sh 'env'
    sh 'echo $BRANCH_NAME' 
    sh 'echo $GIT_COMMIT'
}

I'm getting plenty of environment variables when I use env or printenv just not the Github plugin ones.
Any tips on how I can get the Git env variables passed in to the job?

Update: I'm able to easily get the Git env variables when I use a Freestyle Project and have a shell step use echo $GIT_COMMIT. Still want to know though how to get it to work using Jenkinsfile + Pipeline job item.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
NateW
  • 2,856
  • 5
  • 26
  • 46
  • 1
    You can access env variables in Jenkinsfile using `env.VARIABLE`. I remember that `env.BRANCH_NAME` works. – StephenKing Jun 27 '16 at 05:43
  • 2
    Possible duplicate of [Jenkins Workflow Checkout Accessing BRANCH\_NAME and GIT\_COMMIT](http://stackoverflow.com/questions/36304208/jenkins-workflow-checkout-accessing-branch-name-and-git-commit) – StephenKing Jun 27 '16 at 05:44

5 Answers5

14

This won't work due to lack of double quotes, missing curly braces, and missing env.:

sh 'echo $BRANCH_NAME' 

This works as expected in a Jenkinsfile:

node {
    sh "echo ${env.BRANCH_NAME}"
}
Konrad Kleine
  • 4,275
  • 3
  • 27
  • 35
  • Hey @Konrad Kleine, I am having the same issue and the printenv is not even printing the git env variables within pipeline. Do you have any hint on this? – Tibin Paul May 16 '18 at 07:36
  • 1
    Hey @TibinPaul, no I have no hints for you and stopped using Jenkins Pipeline. – Konrad Kleine Sep 17 '18 at 15:56
5

So, anyone else who has stumbled onto this Stackoverflow issue should be aware that this is a bug with the current pipeline situation.

Lots of discussion here: https://issues.jenkins-ci.org/browse/JENKINS-35230

Essentially the plugin is not able to correctly add environment variables due to some incompatibilities with pipeline.

Breedly
  • 12,838
  • 13
  • 59
  • 83
3

This is fixed in Git plugin 3.3.1

Version 3.3.1 (Jun 23, 2017)

  • Print first line of commit message in console log (JENKINS-38241)
  • Allow scm steps to return revision (JENKINS-26100)
  • Don't require crumb for POST to /git/notifyCommit even when CSRF is enabled (JENKINS-34350)
  • Fix credentials tracking null pointer exception in pipeline library use (JENKINS-44640)
  • Fix credentials tracking null pointer exception in git parameters use (JENKINS-44087)
Morgan Christiansson
  • 29,280
  • 1
  • 19
  • 13
2

Most of the pipeline examples I've found wrap the code in a "node" closure, which for some reason doesn't allow the Jenkins Git plugin to populate the environment variables.

However, if instead you wrap it like this, the environment variables are set properly:

pipeline {
  agent {
    label ('<AGENT>')
  }
  stages {
    stage('<STAGE>') {
      steps {
        <CODE>
      }
    }
  }
}

I'm not sure which closures are crucial here, but this formatting allowed me to access env variables such as env.GIT_COMMIT

Noah Bar-Shain
  • 739
  • 4
  • 5
1

You may need to do something like this.

node {
   def branch = env.BRANCH_NAME
   sh "My branch name: ${branch}"
}