4

I'm new in Jenkins pipline and I'm trying to get the git SHA after checking out the Master branch of my github repo but I can't find the correct way to do it...

My code:

pipeline {
    agent { label 'SonarPHP56' }
    stages {
        stage('vehicle-prices.com') {
            agent { label 'SonarPHP70' }
            steps {
                //vehicle-prices.com
                checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout'], [$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'st-jenkins-bot', url: 'https://github.com/****/vehicle-prices.com']]]
                gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                sh 'echo ${gitCommit}'
            }
        }
    }
}

I'm using this line to try to do it:

gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
sh 'echo ${gitCommit}'

But I get this from Jenkins console:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 24: Expected a step @ line 24, column 17.
               gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
               ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1073)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:129)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:516)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:479)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:252)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:405)
Finished: FAILURE

Can someone help me please?

XorX
  • 238
  • 2
  • 5
  • 19
  • try `def gitCommit = sh(....` – tim_yates Jul 21 '17 at 11:46
  • WorkflowScript: 24: Expected a step @ line 24, column 17. def gitCommit = sh(returnStdout: true, script: 'git rev-pars... – XorX Jul 21 '17 at 11:48
  • 2
    @tim_yates Wondering how this question is duplicate of the question you referred. I checked both. They are different. – brightDot Sep 28 '18 at 21:59
  • The error and solutions are the same – tim_yates Sep 28 '18 at 22:19
  • 3
    Yeah they're not. One is asking for a commitHash back from an API request (this one), the other was asking about variable declaration in general. To answer this question you would define something like this: def commitHash = checkout(scm).GIT_COMMIT That would return your commitHash on the branch that is running as a var commitHash. Then use that and pass it around. If you re-ask this question, tag me and I'll give you a more details example. – isaac weathers May 01 '19 at 20:04
  • 3
    1. This question is different, it is not about how to define a variable, but how to get the SHA of the build. 2. This question was never actually answered, how does one get the SHA of the last commit in a jenkins pipeline. – LaFayette Jan 18 '20 at 20:55

0 Answers0