Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using git hook after commit

I have just started writing a web application.

I am using GIT for version control and I have git and web server in the same computer.

Application has 3 environments: dev, test and production

I want to use git hook after every commit to update dev, test or production application.

what is the best practice for this?

I need something like this:

  1. when I commit, dev must automatically be updated
  2. when commit message contains "test: " in front of message - dev and test must be updated.
  3. when commit message contains "production: " in front of message - production, dev and test must be updated.

Thanks!

like image 610
Irakli Avatar asked Oct 09 '22 19:10

Irakli


2 Answers

I've just written a hook / mini bash script to solve this problem

#!/bin/bash

if git log --pretty=format:%s -1 | grep -q "^test: "
then
    #action / update dev/test
elif git log --pretty=format:%s -1 | grep -q "^production: "
then
    #action / update dev/test/production
else
    #action / update dev
fi

It is my first bash script so.. please help to improve this :)

like image 171
Irakli Avatar answered Oct 13 '22 11:10

Irakli


Based on Irakli idea, here is what I have working as a post-receive in my repo...

#!/bin/bash

MESSAGE=$(git log -1 HEAD --pretty=format:%s)

if [[ "$MESSAGE" == *\[staging\]* ]];
then
    #action / update staging
    # another method not being used...
    # GIT_WORK_TREE=/path/to/working/site/ git checkout -q -f staging
    echo "NOTE: Beginning Auto-Push to Staging Server... "
    `git push staging`
    echo "========================================================
======== Done! Pushed to STAGING.com  ============= 
======== Thanks Captain. Keep up the good work! ========
========================================================"
elif [[ "$MESSAGE" == *\[production\]* ]];
then
    #action / update production
    echo "NOTE: Beginning Auto-Push to Production Server... "
    # `git push production`
    echo "========================================================
======== Done!!! Pushed to Production.com  ======= 
======== Test immediately for any errors! =========
========================================================"
fi

Note:

to make the 'git push staging' work, you need to have a .git/hooks/post-reveive hook on that working tree. I used this code except I added 'umask 002 && git reset --hard' at the bottom.

I also had to add a denyrecive to that working tree's .git/config file:

[receive]
    denycurrentbranch = ignore

Note 2:

Please note this setup IS NOT for everyone... only for small(ish) sites where quick & dirty updates are fine.

like image 21
Chuck Norton Avatar answered Oct 13 '22 12:10

Chuck Norton