4

Context: I am building an automated build script which will run on a Windows server box (necessary because our development software package is windows only). My client box is also Windows. It should execute a number of steps, one of which is to login to github, discard all local changes (just in case; there really shouldn't be any local changes), fetch and merge.

Restrictions: 1) I should be able to run one batch file (or other script file, as long as it runs on a windows box) 2) I cannot walk over to the box and input my password for SSH everytime. It should work automatically.

Problem: I cannot get Git and SSH to work nicely with my Windows batch file.

My First Attempt:

:: set repo folder
CD %2

:: check status
CALL git status
ECHO.

:: discard all changes
ECHO ~  discard all changes
ECHO.
CALL git reset --hard
CALL git clean -f -d
ECHO.

:: switch branch
ECHO ~  checkout branch %4
ECHO.
CALL git checkout %4
ECHO.

:: get any changes from server
ECHO ~  fetch %3
ECHO.
CALL git fetch %3
ECHO.

:: merge changes into current branch
ECHO ~  merge %3/%4
ECHO.
CALL git merge %3/%4
ECHO.

The parameter %2 is the windows file location, %3 is the github HTTPS url, %4 is the branch name.

This works, but uses HTTPS, which means entering my username and password manually.

So I tried doing it in SSH (via bash), using the commands:

eval `ssh-agent`
ssh-add /z/id_rsa

like so from batch:

CD C:\Program Files (x86)\Git\bin\
sh.exe --login -i -c "eval `ssh-agent` && ssh-add /z/id_rsa && exit"

However, SSH also requires me to enter the key passphrase every time I use this command.

How can I, either A) keep the SSH login information live between batch/bash scripts or B) enter the passphrase programmatically?

2
  • Is your client running Windows or Linux? Commented Jun 26, 2013 at 12:03
  • Ansgar Wiechers: Windows, both on client and (not yet existing) server. Commented Jun 26, 2013 at 12:07

1 Answer 1

3

Since you're using Windows on client and server I'd recommend using plink from the PuTTY Suite for automating stuff.

The suite also includes an SSH agent (pageant). However, you only need an agent if the private key is password protected. For automation purposes I'd create a dedicated keypair without a passphrase and use that key:

plink -ssh -batch -i "C:\path\to\private.ppk" user@host C:\serverpath\batch.cmd

In the above command C:\serverpath\batch.cmd is the location of the batch file on the server. You can also keep the commands you want to run in a file on the client and use that with plink:

plink -ssh -batch -i "C:\path\to\private.ppk" -m C:\localpath\batch.cmd user@host
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, thanks Ansgar. Once I regenerated my key without a passphrase, the original script I wrote worked almost verbatim, the only difference being I passed all the git commands to sh.exe. I will however take a look at using Putty's plink, maybe I can clean up the chain of commands into something readable for others on my team.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.