1

I'm trying to set up a Jenkins project which call a gradle task.

Without jenkins (i.e. from command line), the project compile fine.

However, when I ran it from jenkins, it stop at a particular function that I build to get the total commit number from Git:

def getBuildNumber = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-list', 'HEAD', '--count'
        standardOutput = stdout
    } 
    return stdout.toString().trim()
}

It produces this error:

12:19:44.920 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
12:19:44.924 [ERROR] [org.gradle.BuildExceptionReporter] 
12:19:44.927 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
12:19:44.928 [ERROR] [org.gradle.BuildExceptionReporter] A problem occurred evaluating root project 'MyProject Bitbucket'.
12:19:44.931 [ERROR] [org.gradle.BuildExceptionReporter] > A problem occurred starting process 'command 'git''

I thought this is caused by jenkins not recognising the command, so I added an export path statement before the execution of the gradle script. However the error still happen.

I'm not sure where I did wrong, If there's an alternative to do this I would be more than happy to try it out.

evanp
  • 21
  • 6
  • possibly related: [how-can-the-git-command-be-executed-on-windows-through-gradle](https://stackoverflow.com/questions/34361298/how-can-the-git-command-be-executed-on-windows-through-gradle) – TT-- Nov 19 '18 at 03:57

2 Answers2

0

Since I haven't received any reply, I decided to play around and found a plugin to use this: Gradle Git Plugin

I found a question in the raised issue regarding calling rev-list from grgit , and decided to implement the same thing in my build file.

I added the plugin into my gradle build file:

plugins {
  id 'org.ajoberstar.grgit' version '1.3.2'
}

I converted the getBuildNumber into this:

def getBuildNumber = { ->
  return grgit.log(includes:['HEAD']).size()
}

Afterwards, it runs fine in both non-jenkins and jenkins environment.

I'm still looking for non-plugin answer though for this issue.

evanp
  • 21
  • 6
0

May need to specify the executable explicitly, such as cmd or sh for example.

Refer to Gradle tasks Exec.

Here is a more generic way that will account for the system it could be executed on:

task myExecTypeTask(type: Exec) {

    workingDir "$projectDir/mySubDir"

    if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {

        commandLine 'cmd', '/c', 'git', 'rev-list', 'HEAD', '--count'

    } else {

        commandLine 'sh', '-c', 'git', 'rev-list', 'HEAD', '--count'

    }
}
TT--
  • 2,956
  • 1
  • 27
  • 46