GIT server for automatic web code deployment

Source: Internet
Author: User

GIT server for automatic web code deployment

Here, we will briefly record how the GIT server can automatically deploy web code.

1. git mechanism Overview

Git uses a code repository. The git server has a repository called a remote repository. The local repository is also called a local repository. When we commit code to the local repository, when we push the code to the remote repository, and when pull, we download the code from the remote repository to the local repository.

The code repository uses version pointers for code storage. Each submitted version corresponds to a HEAD pointer, and the current version pointer changes with the code submission.

2. Principles of automatic deployment

Let's talk about the distribution of code, the local repository on the developer's computer, the remote repository on the git server, and another local repository on the web server. The code here is accessed by our browser.

To achieve automatic deployment, the code is automatically deployed to the local repository of the web server when the developer submits code from the local repository to the remote repository, synchronize local warehouses of developers and web servers.

3. Automatic deployment

As mentioned above, the script is automatically triggered when the developer submits the script to deploy the web code.

Here we need to talk about the git hook mechanism. When the git server is triggered when it receives various events, the hook used here is post-receive, which receives push requests on the git server, triggered when the code is submitted.

Code details:

Create a post-receive file in the hooks directory of the git remote Repository:

#! /Bin/sh # author: phpernote # Blog of phpernote: http://www.phpernote.com # determine whether the remote repository IS_BARE =$ (git rev-parse -- is-bare-repository) if [-z "$ IS_BARE"]; then echo> & 2 "fatal: post-receive: IS_NOT_BARE "exit 1 fi unset GIT_DIR DeployPath ="/var/web "echo" ================ ============== "cd $ DeployPath echo" deploying the test web "# git stash # git pull origin master git fetch -- all git reset -- hard origin/master time = 'date' echo "web server pull at webserver at time: $ time. "echo" ============================================ ============"

Save and grant the executable permission:

Chmod + x hooks/post-receive

In this way, the code is automatically deployed when the developer submits the code.

Here, I will add a description: because I used git users to create a git repository, but the site server used nginx servers during Automatic deployment. The git users belong to the git group nginx and belong to the nginx group, so here we put post- if the receive is set to an nginx group, there is no way to update the file in the site directory if it is not set (no operation permission will be prompted)

During automatic deployment, I use git fetch or git pull. The difference between the two is that pull fetch in advance and then use merge, to merge local and remote code. However, there is a problem. If a developer makes a mistake in the submission process, after the git reset is reset, the remote code version is earlier than the web code version, when pull is used, it cannot be synchronized with the developer's local code. Therefore, after fetch is used, the version pointer of the web code is consistent with that of the git server by force using reset. Because merge is not used, the web Code cannot be changed directly on the server in the future. All changes to the web code should be submitted on the developer's computer. Otherwise, an error will be reported.

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.