Maja Piechotka
Maja Piechotka

Reputation: 7216

How to use git directly on Windows with ssh-agent in Jenkins

I'm trying to update bare git repo as part of build. On Linux it was relatively simple:

dir("/my/git/repo.git") {
    sshagent(['git']) {
        sh "git fetch origin module:module"
    }
}

However I cannot figure out how to do it on Windows. I tried:

dir("c:\\my\\git\\repo.git") {
    withEnv(["PATH=${env.PATH};C:\\Program Files\\Git\\usr\\bin"]) {
        sshagent(['git']) {
            sh "git fetch origin module:module"
        }
    }
}

But it fails:

Could not find ssh-agent: IOException: Cannot run program "ssh-agent": CreateProcess error=2, The system cannot find the file specified
Check if ssh-agent is installed and in PATH

How can I do it?

Upvotes: 4

Views: 3781

Answers (1)

VonC
VonC

Reputation: 1323095

I see ssh-agent in:

C:\>where ssh-agent
C:\tools\gits\latest\usr\bin\ssh-agent.exe

You need to make sure your %PATH% (as seen by the user executing the Jenkins job) includes Git\bin, Git\usr\bin, Git\mingw64\bin.
(Replace Git by the Git installation path folder)

After executing a job (even failed), look for the link "Environment Variables": you will see exactly who was running the job (USERNAME) and the PATH used.

Upvotes: 3

Related Questions