Git server implements Web code auto-deployment

Source: Internet
Author: User
Tags using git vps server

Personal original, copyright, reproduced Please indicate the source, and keep the original link:

http://www.embbnux.com/2014/09/05/git_server_let_code_auto_deploy/

Subscribe Blog:blog of embbnux articles PageView: 4,209 views

Prior to a VPS server built a git server, to do code management, easy to team development. But the problem is the corresponding, using Git can easily upload code, and because of the web development, every time also get the code on the server manually pull or copied to the folder where the page is located, it is more troublesome, not suitable for my lazy people. GIT provides a hook mechanism that makes it easy to automate the deployment of your code.

Personal original, copyright, reproduced please specify the source:

http://www.embbnux.com/2014/09/05/git_server_let_code_auto_deploy/

A brief introduction to the GIT mechanism

Git uses the code repository, git server has a warehouse, called the remote repository, we clone down, there is a local warehouse called the local warehouse. When we commit, we commit the code to the local repository, and when we push, we commit the code to the remote repository. Pull is the time to download the code from the remote repository to the local repository.

The Code warehouse uses version pointers for the storage of the code, and each submitted version corresponds to a head pointer, and the current version pointer changes as the code commits.

Two automatic deployment principles

Tell us about the distribution of the code now, the local repository on the developer's computer, the remote repository on the GIT server, the other local repository on the Web server, and the code that our browser accesses.

To implement automatic deployment, when developers submit code from the local repository to the remote repository, the code is automatically deployed to the Web server's local repository, which synchronizes the local repository of the developer's local repository with the Web server.

Three implementation of automatic deployment

According to the above, is to be submitted by the developer, automatically trigger scripts, scripts to implement the Web-side code deployment.

Here's the GIT hook mechanism that triggers when a git server gets a variety of events, and the hooks used here are

Post-receive

This hook is triggered when the GIT server receives a push request and accepts the code submission.

The specific code reflects:

Create a new post-receive file in the hooks directory of the Git remote repository:

123456789101112131415161718192021222324252627 #!/bin/sh #author: embbnux#Blog of Embbnux: http://www.embbnux.com#判断是不是远端仓库IS_BARE=$(git rev-parse --is-bare-repository)if [ -z "$IS_BARE" ]; thenecho >&2 "fatal: post-receive: IS_NOT_BARE"exit 1fiunset GIT_DIRDeployPath="/var/web"echo "==============================================="cd $DeployPathecho "deploying the test web"#git stash#git pull origin mastergit fetch --allgit reset --hard origin/mastertime=`date`echo "web server pull at webserver at time: $time."echo "================================================"

Grant executable permissions after saving:

1 chmod+x hooks/post-receive

This will automatically be deployed when the developer submits the code.

When I deploy it automatically, I use Git fetch, or git pull, where the two differences are mainly the pull-fetch and then merge, merging the local and remote code. But there is a problem, if the developer in the submission process error, after using the git reset reset, the remote version of the code is now lower than the version of the web-side code, and then use pull can not be implemented with the developer's local code synchronization. So after using fetch here, it is mandatory to use reset to implement the Web-side code version pointer and GIT server-side consistency. Because the merge is not used, the code in the future will not be changed directly on the server, the changes to the web-side code should use the developer's computer for code submission, or error.

Reference:

Http://argcv.com/articles/2078.c

http://blog.csdn.net/a06062125/article/details/11727273

Git server implements Web code auto-deployment

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.