0

When I do a Git SCM checkout, I don't have the environment variable GIT_COMMIT which contains the latest git commit hash.

I can still access the commit hash by command line with git rev-parse HEAD but the environment variable normally already has it.

pipeline{
    agent {label 'master'}
    stages{
        stage('checkout from SCM'){
            steps{
            checkout([$class: 'GitSCM', branches: [[name: '*/${GIT_BRANCH}']], 
                doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: '9595efef-aa11-3366-5e5e-12a11b21da6a', 
                url: 'https://host:port/xxx/yyy']]])
                bat """
                    echo %GIT_COMMIT%
                    echo ${env.COMMIT}
                    git rev-parse HEAD
                """
            }         
        }
    }
}

Results are:

  • %GIT_COMMIT% => empty
  • ${env.COMMIT} => null
  • git rev-parse HEAD => prints hash code to Jenkins job output

EDIT:

Also tried to implement solution from this answer : https://stackoverflow.com/a/45845221/8315843

def scmVars = checkout([$class: 'GitSCM', branches: [[name: '*/master']], 
    userRemoteConfigs: [[credentialsId: '2b74a351-67d5-4d00-abd3-
    49842a984201', url: 'ssh://git@corporate.com:repo.git']]])
env.GIT_COMMIT = scmVars.GIT_COMMIT

but as soon as I have def someVariable in my script, Groovy throws the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 13.
               def scmVars = checkout([$class: 'GitSCM', branches: [[name: '*/${GIT_BRANCH}']], 
               ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1107)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:624)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:602)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:579)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:677)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:689)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:553)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:505)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:335)
    at hudson.model.ResourceController.execute(ResourceController.java:101)
    at hudson.model.Executor.run(Executor.java:442)
Finished: FAILURE

I'm using Jenkins 2.332.2

Sybuser
  • 735
  • 10
  • 27
  • 4
    Git env variables are not setup automatically when using `checkout` step. If you wrap the `def scmVars` and checkout around a `script {}` you should be able to access the `scmVars.GIT_COMMIT`. F.e. `steps{ script{ def scmVars = checkout([...]) echo scmVars.GIT_COMMIT...}..}` – Unforgettable631 Nov 02 '22 at 16:22

0 Answers0