Cron – keeping a remote server up to date with git repo

crongitgithub

So, I'm aware of git hooks, and will be looking into those next, but want to write a simple bash script, run via cron, to keep a repo on a remote server up to date. I imagine it working like this:

  • cron executes myupdatescript.sh
  • myupdatescript.sh will:
    1. cd into /my/sites/staging
    2. git pull -q origin master
    3. if something was pulled, then:
      • run grunt to build the source
      • yadda yadda yadda

I currently have this working as a cron task that looks like:

*/1 * * * * cd /my/sites/staging && git pull -q origin master && grunt production

I'm trying to avoid having to run my grunt build if nothing changed. I guess it all boils down to how can I tell if git pull actually pulled anything, and use that as a condition to running my grunt build. Ubuntu server, key is installed on github, all that good stuff. Thanks for any help!

Best Answer

I think you can solve this issue simply doing it:

*/1 * * * * cd /my/sites/staging && git pull -q origin master|grep -v "up-to-date" && grunt production