git hook realizes automatic code update

Git can customize some hooks, and these hooks can be executed in specific situations, which are divided into client-side hooks and server-side hooks. Client-side hooks are triggered by operations, such as commit, merge, etc., and server-side hooks are triggered by network actions, such as pushed commits.

 

So where are the hooks?
Under the .git/hooks/ folder. When you init a repository, there will be some hook examples below, ending with .sample.

 

Then when the hook is executed, Git pre-defines the trigger timing:
ClientSide hooks:
1 pre-commit, this hook is executed first when the commit action is executed, you can use this hook to do some checks, such as code style checks, or run tests first.
2 prepare-commit-msg, this hook will be triggered before entering the message when committing. You can use this hook to customize your own default message information.
3 commit-msg, which is triggered when the user enters the commit message, you can use this hook to verify the message information, such as whether it complies with the regulations, whether there is a cr, etc.
4 post-commit, triggered when the commit is completed, you can use this hook to send notifications, etc.
5 pre-rebase, rebase will be triggered before, you can use this hook to reject all the commits that have been pushed for rebase operation.
6 post-merge, when the merge is successful, this hook will be triggered.
7 pre-push, when push, remote refs are updated, but triggered before all objects are transmitted.
8 pre-auto-gc, triggered before git gc --auto is executed. It's nice to do some validation or backup before garbage collection.
ServerSide hooks:
1 pre-receive, will be executed before receiving the push action.
2 update, which is also executed before receiving the push action, but may be executed multiple times, once for each branch.
3 post-receive, it will be triggered when the push action has been completed. You can use this hook to push notification, such as sending emails, notifying the continuous build server, etc.

#!/bin/sh
# Modify the .git/hooks/post-receive.sample file in the server-side directory of the git project. The hook will run after the push is completed. Remove .sample to enable it, and add an execution script. The variables need to be modified into two items. The path of the machine, take my test script on the 81 server as an example:

#zh-jssdk Project storage path, need to be modified
zh_jssdk_pro_path="/home/zhaojianping/weizhan/zh-jssdk/"
#Folder relative path, generally do not need to be modified
zh_jssdk_dir_path="dist/"
#wmp project storage path, need to be modified
weizhan_wmp_pro_path="/home/zhaojianping/weizhan/wmp/"
#Folder relative path, generally do not need to be modified
weizhan_wmp_dir_path="wmp-webapp/src/main/resources/static/wmp/common/zh-sdk"

cd $ {zh_jssdk_pro_path}
unset GIT_DIR
git pull
cd ${weizhan_wmp_pro_path}
unset GIT_DIR
git checkout master
for i in $(git branch -a)
do
notMaster=$(echo $i | grep "master")
remoteBranch=$(echo $i | grep "remotes/origin/")
if [[ "$notMaster"x == ""x ]]
then
        if [[ "$remoteBranch" != "" ]]
        then
        branchName=${i##*/}
        git branch -D ${branchName}
        git checkout "$i" -b ${branchName}
        git pull
        git rm -rf ${weizhan_wmp_dir_path}
        mkdir "$weizhan_wmp_dir_path"
        git commit -m 'Delete old files commit!!--by shell [zh-jssdk push!]'
        cp -rf ${zh_jssdk_pro_path}${zh_jssdk_dir_path}* ${weizhan_wmp_pro_path}${weizhan_wmp_dir_path}
        git add .
        git commit -m 'Update new files commit!!--by shell [zh-jssdk push!]'
        git push
        git checkout master
        git branch -D ${branchName}
        be
be
done

Remember that all hooks should be executable. chmod u+x your_hook

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326253289&siteId=291194637