Automating git commands on Windows

Context: I create an automatic build script that will run on a Windows server (required, because our development software package is only windows). My client box is also Windows. He has to follow several steps, one of which is to log into github, discard all local changes (just in case, there really shouldn't be any local changes), fetch and merge.

Limitations: 1) I must be able to run one batch file (or another script file if it works in the window window) 2) I can not go to the box and enter the password for SSH each time. It should work automatically.

Problem: I cannot get Git and SSH to work 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% 2 parameter is the location of the Windows file,% 3 is the github https URL,% 4 is the branch name.

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

So I tried to do this in SSH (via bash) using the commands:

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

for example, from a 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 a passphrase every time I use this command.

How can I either A) save real-time SSH login information between batch / bash scripts or B) programmatically enter a program phrase?

+4
source share
1 answer

Since you use Windows on the client and server, I recommend using plink from PuTTY Suite to automate the work.

The kit also includes the SSH agent ( pageant ). However, you need an agent only if the private key is password protected. For automation purposes, I would create a special key pair without a passphrase and use this key:

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

The command C:\serverpath\batch.cmd indicates the location of the batch file on the server. You can also save the commands you want to run in a file on the client and use them with plink :

 plink -ssh -batch -i "C:\path\to\private.ppk" -m C:\localpath\batch.cmd user@host 
+3
source

Source: https://habr.com/ru/post/1488266/


All Articles