Unable to read Git Jenkins environment variables using Groovy Jenkinsfile

The Git plugin is installed (by default) in my Jenkins, but I cannot get the env variables that must be passed by the Git plugin. I'm looking for:

GIT_COMMIT
GIT_BRANCH
GIT_PREVIOUS_COMMIT 
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

etc .. I am using a pipeline job item pointing to a Github repository with a Jenkins file 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 get many environment variables when I use envor printenvrather than Github plugins.
Any tips on how I can get env git env variables in a job?

Refresh . I can easily get enw Git variables when I use the Freestyle project and use the shell step echo $GIT_COMMIT. Still want to know how to make it work using the Jenkinsfile + Pipeline job item.

+4
5

- , env.:

sh 'echo $BRANCH_NAME' 

Jenkins:

node {
    sh "echo ${env.BRANCH_NAME}"
}
+8
+4

- .

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

Git 3.3.1

3.3.1 (23 2017 .)

  • (JENKINS-38241)
  • scm (JENKINS-26100)
  • POST / git/notifyCommit, CSRF (JENKINS-34350)
  • (JENKINS-44640)
  • git (JENKINS-44087)
+1

, , "node", - Jenkins Git .

However, if you instead transfer it like this, the environment variables are set correctly:

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

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

0
source

All Articles