Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top