简体   繁体   中英

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!

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.

You can write a post-commit hook which will parse the commit message by using something like git log -1 --format=%B and do the appropriate action like git push dev etc.

If you are talking about pushing your commits to a remote central repo and that repo has to do this, then you have to use a post-receive hook in a similar way. Note that commit hooks run on the client repo where you commit.

With that said, pushing to environments using what you say in the message is not a proper workflow. You can have different branches, where you can cherry pick your commits etc. You can setup hooks such that when you push to test branch, the test environment is updated and so on.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM