Automatically syncing your GitHub repo to a windows box

Building a windows jump box to execute code remotely and sync with GitHub

Recently I was building out my automation framework to start executing Windows Powershell via Ansible. What I had found was that there was not an easy direct way to run powershell inside of a Linux Ansible host so I built a windows jump box that Ansible could remotely call to execute my powershell scripts. I had a number of scripts on this jump host and I began to think more about the operational aspect of other people in my team editing code. The obvious answer is using GitHub or GitLab, but I wasnt quite sure how to sync my repo onto this jump box. Thus began my journey of finding out how to natively use Git commands on windows and schedule them to run in intervals through the day.

The problem:
Downloading Git Repo code to the jump host on a regular interval to ensure code is being updated and in sync with the repo.

Solution Breakdown:

  1. Install Git on windows.
  2. Setup Authentication to GitHub
  3. Create script to run the git operation to refresh the folder
  4.  
  5. Create a Scheduled Job with the task scheduler to run every couple of minutes.

Now that you guys understand the problem and the solution break down. Let’s begin.

(1) Install Git
— Google or find the “Git” for windows package install.
https://git-scm.com/download/win

Run the setup as Administrator.
a) Choose a Path like C:\Git or a path with no spaces (this makes your life easier when referenceing the path doing the script calls for git).
b) Keep defaults for the rest of the items selected

 

(2) Setup your authentication
a) Run “Git Bash”
b) Generate your ssh key
ssh-keygen -t rsa
leave the pass phrase blank
(output should like like below minus the blurs)

c) Login to your github account. Go to your repo and Insert your public key into your github account as a “Deploy Key”

d) you can do a quick test to see if auth works by doing a test of cloning your repo with a simple command git clone <git@github.com:your_repo>

(3) Lets setup the bat script to do the call to Git Hub.

a) Once you have setup authentication you can basically call anything you would normally do with the git command on the CMD CLI. Just substitute git with “C:\your_path_to_git.exe”
In my setup I am going to put my stuff in a folder called git.

 

Create a batch (.bat) file with notepad to do your refresh. Below is what it should look like. Make sure you change directories to the repo (you should have a hidden .git folder in that directory). Warning: the commands below will do a hard reset and sync it to the remote repo origin. Don’t do code changes in this directory

cd C:\<your_project_directory>\
C:\Git\bin\git.exe fetch --all
C:\Git\bin\git.exe reset --hard origin/master

(4) Setup your scheduled task

a) Open Windows task scheduler
b) Create a new task and configure it.

I run mine every 5 minutes, but you can schedule it as needed.

Ta da. That’s it. You now have a syncing code base to a windows server. Execute those power shell and windows scripts at will 😀