Use Git Commands in Jenkins: git not found

1/18/2018

I'm running Jenkins on Kubernetes with the git plugin installed. Now I want to use git commands in my script, which fails with the log:

script.sh: line 1: git: not found

My script:

stage('Package Helm Chart'){
            sh """ 
              #!/bin/bash

              echo "Pushing to remote Repository.."

              git checkout master

              git add <myfilehere>

              git commit -m "[Jenkins] Adding Artifact ${env.BUILD_NUMBER} to repository"

              git push

              echo "Successfully pushed artifact to repository"
            """

Any idea on how to fix this?

Cheers Jst

-- JSt
git
jenkins
kubernetes

2 Answers

1/18/2018

I'm not sure but I think you mix Jenkins git plugin, that allow you to choose git as source code management for project workspace Inside project configuration, and git in command line Inside a script that which requires that git is install and present in $PATH when script is execute.

-- romaric crailox
Source: StackOverflow

1/18/2018

The sh command in question should run on a jenkins node inside a node block. This command will then run in a shell on that node. To use git in the sh tag of a pipeline script you need to have git installed and on the PATH on the node that you want to use.

If you are using Kubernetes, then I assume you are running the Jenkins master or the node from a docker image, thus this image will need git installed and on the PATH.

Once this is done the shell will be able to find git.

-- JamesD
Source: StackOverflow