Git Variables in Jenkins Workflow plugin

I’d like to access git variables such as GIT_COMMIT and GIT_BRANCH when I have checked out a repository from git further down in the build stream. Currently I find no available variable to access these two parameters.

node {
    git git+ssh://git.com/myproject.git
    echo "$GIT_COMMIT - $BRANCH_NAME"
}

Is such variables available and in case, where would I find them. I don’t mind if they are available through some groovy variables or wherever, just that I can access them.

Maybe I lack the debugging skills in Groovy and this is easy to find, but I just can’t find it with my limited skills.

  • Tag a Repo from a Jenkins Workflow Script
  • Jenkins Workflow Build Information
  • Start a job multiple times concurrently with workflow plugin
  • Jenkins Build Flow versus Workflow (Pipeline) plugin
  • Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT
  • Jenkins Build Periodically - Schedule
  • how to configure job in jekins if first project is build successful then trigger another project and so on with “Multi-configuration project” job
  • How to get stdout and stderr from single Jenkins Pipeline parallel blocks?
  • 4 Solutions collect form web for “Git Variables in Jenkins Workflow plugin”

    This is what I’m doing, based on the example provided in the Jenkins examples repo:

    node {
        git url: 'https://git.com/myproject.git'
    
        sh 'git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
        git_branch = readFile('GIT_BRANCH').trim()
        echo git_branch
    
        sh 'git rev-parse HEAD > GIT_COMMIT'
        git_commit = readFile('GIT_COMMIT').trim()
        echo git_commit
    }
    

    With the 2.4 release of the Pipeline Nodes and Processes Plugin, you can simply do:

    def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    

    This example might get you further:
    https://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/gitcommit

    In this example they are piping the output of git commands to a file, and then reading the file.

    I good way to fix this for now is to use the Multi-branch pipeline, and it might be good to know that Bitbucket and Github have plugins in Jenkins that sets up an organisation and autodiscovers new projects. This works natively then with env.GIT_BRANCH

    For commit ID I would suggest what @mkobit wrote above.

    Git Baby is a git and github fan, let's start git clone.