Windows .bat file to schedule git add, commit and push to Github
Asked Answered
B

3

5

I need to schedule a task in Windows Server 2008 R2 to automatically push files from a folder to Github Enterprise (I don't think the "Enterprise" part will make it different than regular Github, except that ssh authentication is required).

Here's what I've done:

  1. Set up a public/private key pair and added them to Github and .ssh, respectively
  2. Created a Github repo and cloned it to a local folder
  3. Manually ran through the add, commit, and push steps to make sure it works that way
  4. Tried to write a .bat file for automatically doing step #3
  5. Scheduled the task to run as my user, with highest privileges, in the target folder

Unfortunately, the task fails because I am not writing the .bat file correctly.

This was my attempt at the .bat file:

echo git add . | "C:\Program Files\Git\bin\git.exe" 
echo git commit -m 'scheduled commit' | "C:\Program Files\Git\bin\git.exe" 
echo git push | "C:\Program Files\Git\bin\git.exe" 

I found a good resource on how to do this, but it is unfortunately very Linux-specific.

Boxhaul answered 13/12, 2016 at 18:10 Comment(2)
Just as a note, you might want to do this to something a bit less public, there's generally no reason anyone else needs to see your work before you've done an editing pass on the history. Why publish muddy writing and coding mistakes you did yesterday and caught this morning?Etruscan
@Etruscan Do you mean the file being pushed to Github? It's just a file that's automatically updated daily by someone else's SAS script. We're using the company Github as a way to share the daily-updated file with other people in the company, whilst avoiding having to update the permissions of a network folder for new users. We were going to use Sharepoint or Onedrive instead of Github (since it's data, not code), but there were permissions issues.Boxhaul
C
10

Just do:

git add . 
git commit -m 'scheduled commit'
git push

Or, if git is not in your path:

"C:\Program Files\Git\bin\git.exe" add .
"C:\Program Files\Git\bin\git.exe" commit -m 'scheduled commit'
"C:\Program Files\Git\bin\git.exe" push

And depending on what you want to achieve,

git commit -a -m 'scheduled commit'

to commit only tracked file that changed, or :

git add -A
git commit -m 'scheduled commit'

to commit new file or delete is even better...

Cynara answered 13/12, 2016 at 18:37 Comment(2)
It's kind of weird. When i do this it says that it completed successfully, but when I look on Github I don't actually see an update.Boxhaul
so how to put few conditions here. like, let's say your push fails. and you want to give a try again.Slope
U
1

The above answer from Philippe works. However, it does not work if it is run somewhere the errorlevel is checked after the execution of commit (like in Jenkins). A small modification solves that:

git add -A
git diff-index --quiet HEAD || git commit -m "scheduled commit"

This is explained further in the original answer on https://devops.stackexchange.com/a/5443

Unmoor answered 17/12, 2018 at 19:49 Comment(0)
U
0

Maybe you should call git directly. Because git does not read from the standard input, the output of echo means nothing to git through the pipe | .

The solution has been posted by Philippe while I'm editing this answer :)

Upstream answered 13/12, 2016 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.