Reference
70825225?utm_source=itdadao&utm_medium=referral
52794581
Principles and processes
- Git users to perform
git push
actions
- A remote repository discovers that a user has performed a push operation and executes a script
post-receive
(HOOK)
- In the
post-receive
script, copy the code from the GIT repository to the Web site Directory
Create a git repository
We can create a git repository on our own server in two ways:
git --bare init
(Bare warehouse)
git init
The difference between the two:
- The directory structure of the common Git repository is consistent with your code directory structure, with only a few
.git
directories, and the .git
directory contains some git configuration data.
- Bare warehouses only save some configuration information, etc., the naked eye can not find the code we uploaded
Using bare warehouses is recommended
Execution hooks
The GIT repository has a different location than the hooks in the Git bare repository.
- Git common repository hooks in the
.git/hooks/
- git bare repository hooks in the
hooks/
The hook to do is to copy the code from the repository to the Web directory in two ways:
- In the Web directory, clone the Git repository code:
git clone xxxxx
When you need to deploy the code, you can synchronize the code by executing a git pull.
- Package the code in the Git repository and extract it to the web directory
Realize:
The first way to achieve:
In the hooks directory above, create a post-receive
file that reads as follows
#!/bin/shDEPLOY_PATH=/home/wwwroot/default/myproject/unset GIT_DIR #这条命令很重要cd $DEPLOY_PATHgit reset --hardgit pullchown www:www -R $DEPLOY_PATH
The second way to achieve:
#!/bin/shDEPLOY_PATH=/home/wwwroot/default/myproject/git archive --format zip --output /path/to/file.zip master # 将 master 以zip格式打包到指定文件(裸仓库中执行)mv /path/to/file.zip $DEPLOY_PATH #将打包好的剪切到web目录unset GIT_DIRcd $DEPLOY_PATHunzip -o file.zip #解压覆盖rm -rf file.zip #删除chown www:www -R $DEPLOY_PATH
Finally, add executable permissions for Post-receive
chmod +x post-receive
Add a remote source to a local warehouse
This time the local warehouse is really your development machine above the local. Add a new remote source to your existing Git project, and the push code in the remote source will automatically trigger the bash script above.
$ git remote add deploy user@server.ip:/home/user/testRepo$ git push deploy master
Automate site deployment with Git hooks