Automate site deployment with Git hooks

Source: Internet
Author: User
Tags shebang git hooks

Reference

70825225?utm_source=itdadao&utm_medium=referral

52794581

Principles and processes
    1. Git users to perform git push actions
    2. A remote repository discovers that a user has performed a push operation and executes a script post-receive (HOOK)
    3. 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:

    1. git --bare init(Bare warehouse)
    2. git init

The difference between the two:

    1. 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.
    2. 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.

    1. Git common repository hooks in the .git/hooks/
    2. 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:

    1. 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.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.