Build a git server and use git hook to automatically deploy code

Source: Internet
Author: User

I like GitHub. My personal code is all hosted on it, but some companies or projects are not suitable for GitHub. You hope to have a completely private repository, if you have a server, this is obviously easy to do.

The following is a simple description of a git service that I deployed in a project. After local updates are submitted, the server automatically updates the code to the repository of online code.

Create a user and allow the client to log on to the server using the key:
  • Root User SSH log on to the server and create a new user to log on to git and perform operations. For example, here I create a user named "Git:

    $ sudo add user git
  • Go to the/home/git folder and check whether the. Ssh/directory exists. If not:

    $ mkdir .ssh
  • Go to the/home/git/. Ssh folder and create the authorized_keys file.

  • Wait

Operations in the client:
  • Go ~ /. Ssh/directory to check whether the id_rsa.pub file exists. If not:

    $ ssh-keygen
  • Press enter all the way, and then you can find ~ The/. Ssh/directory contains id_rsa.pub. Copy the content and paste it to the authorized_keys file on the operating server.

  • Test whether the key logon is successful

    $ ssh git@yourserverip
Create a git Server Remote Repository
  • Go to the/opt/git directory. If not, create
  • Create a repository folder, for example:

    # path: /opt/git$ mkdir gitdemo
  • Initialize to a remote Repository

    $ git init --bare

The above steps have completed the creation of the GIT remote repository. At this time, you only need to add git remote git @ yourserverip:/opt/git/gitdemo to your local git code repository, you can submit and update the code.

For example, the Development Directory on my machine is :~ /Www/gitdemodev:

$ Cd ~ /Www/gitdemodev $ git init # create a file and write something into it $ touch readme | echo 'Hello git '> reademe $ git add readme $ git remote add origin git @ yourserverip: /opt/git/gitdemo $ git commit-m'first commit '$ git push origin master

Now that your local code has been successfully submitted to the remote repository, you only need to perform the same operation on the computers of your team members, you can use git for collaborative development, copy the id_rsa.pub of your computer to the authorized_keys file on the server.

Git hook automatically deploy code

Assuming that your project runs on this server, it is easy to automatically deploy the Code. For example, your online service code is in the/var/www/demo folder.

Initialize the code library first:

    $ git clone /opt/git/gitdemo /var/www/demo

Then you can use git pull to update the code.

Of course, this is manual. What I want is that after the local commit is updated, the server can automatically update the GIT pull code, so we need to use git hook.

Go to the/opt/git/gitdemo folder and you will find that. the GIT/hook folder is in it and enters the hook. There are many sample scripts in it. Here we only need to use post-update.

    $ mv post-update.sample post-update    $ vim post-update

As you can see, there are actually some shell scripts. All you need to do is write git pull. After the user submits the request, the post-update script is called.

These scripts can obviously do a lot of things, as long as you want to get it, you need to know when each script will be called, Google.

[Note] for folders and files related to git users on the server, please:

    $ chown -Rh git:git /your/git/dirs
(From: http://xydudu.calepin.co/git-server-and-hook.html)

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.