Get Git Commit ID after Git Checkout - Jenkins (Git Plugin)

1.7k views Asked by At

I'm very new to Jenkins.

enter image description here

I'm using this built-in plugin to do Git Checkout with my GitHub credentials, what I wanna achieve is somehow get the git commit id as a variable so that I can use that commit id on any build step of my pipeline.

I searched but couldn't find any documentation for this plugin.

pipeline {
    agent any

    stages {
        stage('Git Checkout') {
            steps {
                git(
                    branch: 'dev', 
                    credentialsId: 'Aniket-IN-Github', 
                    url: 'https://github.com/ComputerReflex/ComputerReflex-Laravel-React.git'
                )
                 # Want to get the commit id.
                echo "Git Commit # ${GIT_COMMIT}"
            }
        }
    }

}


Thank you so much in advance :)

1

There are 1 answers

0
oTolev On

you can use this after checkout ( kind of workaround)

env.GIT_COMMIT = sh(script: "git rev-parse HEAD ", returnStdout: true).trim() 
echo env.GIT_COMMIT