Git manages and automatically deploys Projects

Source: Internet
Author: User
Tags using git

When a project needs to be included in version control, many tools are selected. The most common tools are CVs, SVN, and git. In normal development, the best version control tools are never available, and only the most suitable tools are available. Here I am used to using git to manage my own projects. Of course I used SVN to manage my projects previously, but I am reluctant to use other tools to manage my projects when I use git. In addition to the habit, many features of git are not available in SVN. The simplest thing is to submit it offline. For projects managed by git, you will find that the size of the entire project has not changed much, unlike SVN, each directory has another one. SVN directory, and the project will become very large. For details about the differences between git and Svn, refer to (differences between git and SVN ).

Git Project Management

  1. Download git Tool
    • On Windows, you can download and install it directly on git for Windows. Alternatively, you can search for a project on GitHub and clone it directly, you have installed git on Windows as per the pilot process, provided that you have a GitHub account.
    • On the Linux platform, If You Want To RedHat, Ubuntu, and centos, you can search for them online. For Ubuntu, you can use apt-Get install git to install git.
  2. Git operation (taking Ubuntu as an example)
    • For git operations or git learning, you can directly download the progit book, which contains a detailed explanation of git, allowing you to have a comprehensive understanding of git. Here I will summarize several important parts.
      • The basic operations of git include initializing a version, incorporating it into local management, and pushing local projects to the GIT remote repository. Tracking submitted versions and viewing logs. When referring to this book, you can also search for any questions on the Internet.
      • For git on the server, including Git-supported protocols, the most common is the SSH protocol. For websites hosted by source code such as GitHub, the HTTPS protocol is used, the common tool for managing multiple developers on the GIT server is gitosis. You can refer to the Ubuntu server to set up git
      • Understand git's branches (local branches and remote branches), and how git combines code with merge and rebase. In this part, you need to perform experiments locally and thoroughly understand the operations, advantages, and disadvantages of the two. There are also many articles about this. You can refer to the basic articles about git rebase.
      • Finally, if you are interested, you can find out how git works. In fact, you can understand git as a content addressing file system and how git implements branches, if you record the information about each submitted snapshot, how to store the version information, how many objects git has, and how to use commands provided by git to manage a project.
  3. With the above foundation, you can perform version control on the project as needed by yourself or the team.

 Git project automatic deployment

For automatic deployment, we first Virtualize an application scenario, that is, your code repository is on server a, and your project runs on server B, which is dominated by web projects. Assume that after all developers submit the development code to server A, they want the code to be automatically updated to server B. This eliminates the need to manually deploy the project, and the server will always be the latest code. It should be emphasized that there are two ways to initialize the repository on server a: git init and git init -- bare, for the two differences, refer to git init & git init -- bare difference for a brief introduction here, that is, using git init to initialize a work tree with a working directory ), that is, you can see a complete project. With -- bare, you can only use Git-related files without the work-tree. The two initialization methods depend on your application scenario. It is worth noting that if you use git init for initialization, You need to modify it ,. the GIT/config file allows it to push code to this repository.

 [receive]denyCurrentBranch = ignore

There are two solutions for automatic deployment, taking the Linux platform as an example:

  1. You can use shell scripts and cron jobs to compile a script. The function of this script is to download it from the GIT repository and deploy it. Then, write a scheduled task to download the code on the server once every hour. The Code is as follows:
    #!/bin/bashif [ -f "deploy.lock" ] ; thengit pull origin masterelsegit initgit remote add origin [email protected]_ip:demo.gitgit pull origin masterBASE_DIR="/var/www/html/demo"chown -R demo:www-data $BASE_DIRfind -type d -exec chmod 750 {} \;find -not -type d -exec chmod 640 {} \;chmod u+x deploy.shtouch deploy.lockfi

    Here, the demo user is used as an example to download the latest code from a remote server and complete a series of deployments, in ubuntu, Apache uses the WWW-data user and user group as WWW-data to manage the project by default. Here, we change the project owner to the demo user, the user group is www-data. After the script is completed, you can add the script to the scheduled task. The scheduled task is not described here. You can refer to PhP scheduled task implementation. The disadvantage of doing so is that it is not real-time, and the advantage is that it is easy to control.

  2. Another method is to use the hook function hooks funciton provided by git itself. The script is written in the Post-receive stage of git, which is located in the hooks directory of the project, by default, all scripts are suffixed. end with simple. this script is enabled after simple is removed. Here, we need to explain the issues of user permissions. Because the Default User and owner of the GIT repository are git users, the hook script is also called by the GIT user, so first ensure that the GIT user can download the code of his/her repository. When downloading the project, you must configure the. Git/config file to make git ignore permissions:
    [core]        filemode = false
    The owner and user group of the Apache project are both www-data. You need to change the project owner to the GIT user and add the GIT user to the www-data user group, in this case, the hook script will be correctly called by the GIT user. The content of the Post-receive script is as follows:
    #!/bin/sh#GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)unset $(git rev-parse --local-env-vars)cd /var/www/html/demo || exitecho "git status info:"git statusecho "***************************"git pull origin masterecho "--- Done! ---"

    After the script is complete, as long as the code is submitted to server a, server a immediately executes the hook function, the code is automatically synchronized from the master branch of the project on server a to the new server B (in the running environment), so that the automatic deployment is completed. The most important thing here is the user permissions.

Done!

Git manages and automatically deploys Projects

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.