1
votes

I have written following ant target to fetch code from a GitHub repository.

<target name="fetch" description="Fetching the Source Code">
  <echo message="-------------------------------------------------------------"/>
  <echo message="Fetching Latest from ${build.git.organization}/${build.git.repository} ${build.git.branch}"/>  
  <echo message="-------------------------------------------------------------"/>
  <exec executable="git" dir="${build.source.location}\${build.git.repository}" failonerror="true">
      <arg line="fetch ${build.git.organization}/${build.git.repository} ${build.git.branch}"/>
  </exec>
</target>

The target works fine when I run it from the command prompt. However, when I try to run it from Jenkins, it fails with the following error:

BUILD FAILED C:\Users\PJai12\test\build.xml:66: Execute failed: java.io.IOException: Cannot run program "git": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at java.lang.Runtime.exec(Runtime.java:620) at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58) at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:428) at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:442) at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:629) at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:670) at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:496) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:435) at org.apache.tools.ant.Target.performTasks(Target.java:456) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:853) at org.apache.tools.ant.Main.startAnt(Main.java:235) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)

In order to make sure that the Jenkins is properly configured with git path and ssh credentails, I created anothe job. The job just poll scm from the same repository through Jenkins GitHub plugin. It worked fine.

Here is the GIT configuration in Jenkins: https://imgur.com/a/noFJ9M4

1
First, you cannot modify git commands flow much without forking plugin you're using. Second, git-scm.com/book/en/v2/Git-Internals-The-Refspec. You might achieve that with more complicated setup (i.e. using scripted pipelines or external scripts to call git subcommands) to alter basic Jenkins job routineagg3l
Updated my question.miserable

1 Answers

1
votes

The executable should have a complete path of Git executable.

<exec executable="PATH_TO_GIT_EXE" dir="${build.source.location}\${build.git.repository}" failonerror="true">