Search code examples
gitbatch-filepowershellscheduled-tasksposh-git

How to automate a nightly push to a GitHub branch from a live website with powershell and a batch file


I have a live website (served via IIS on a Windows Server 2008 R2) and a github repo on that server as well. I work in a small development team (3 people) and we want to every night, automatically have our live website:

  1. Copy from the IIS hosted folder to our local repo (on the same server)
  2. Checkout the "NightlyPush" branch
  3. Add all files in the freshly updated repo (so it matches Prod at this point)
  4. Commit all the files in the repo
  5. Do a (forced) push to update GitHub.

This way we have a master branch which is only touched by developers manually, however the "NightlyPush" branch will have the most recent version of our site from the night before. This way we can be sure master will always be stable, but NightlyPush is a nice backup that we can always use if we ever run into trouble or need to look at past code.

I wrote a script in Powershell which handles all this and it works well when I run it manually from the Git Shell(posh-git) prompt, however automating it via a batch file isn't working. If I run my script from PowerShell (non-Git one) it doesn't recognize any Git commands... so I figured I could run it via the Git Shell with the same syntax:

C:\Users\Me\AppData\Local\GitHub\GitHub.appref-ms --open-shell -no-exit C:\Users\Me\Documents\Scripts\CopySite_And_PushToGitHub.ps1

This opens Git Shell, however it just sits there flashing, as if I didn't pass it a script to execute. If I manually paste the path into the prompt and hit enter -- everything works.

**Note: This is my first foray into GitHub, Powershell, and batch files, I could be missing something stupid/obvious. If that's the case, I will accept the shaming as long as someone can help me figure this out!


Solution

  • Add PoSH Git to your default powershell profile (and when scheduling it as a task, check the task's user profile as well). This article should get you started. - Roderick Bant