简体   繁体   中英

How to deploy a project using Git

I figured out a solution, but I feel there must be a simpler way of deployment, using Git. The solution is that

  1. develop app with Git locally
  2. set up Git server
    • clone the local repository locally as a bare repository
    • copy the bare repository to the remote server
    • set the bare repository as a remote repository
  3. clone a repository from the bare repository to the server (so as to have a repo with working directory)
  4. update the bare repository by pushing the locally committed changes.
  5. update working directory by pulling changes from the bare repo

It is not a simple solution, there may be better ways to use Git for deployment. I tried to use the bare repository alone on the server, and set the work-tree variable in the git config file to an other directory, but it didn't work: after the first checkout, when I updated the bare repo and tried to check out the changes, it seemed that everything ok but nothing happened in the working directory. Then I decided to clone the bare repository, so the cloned repo has a working directory, and it can be updated by a git pull (this pull the changes from the bare repo as its origin remote repository).

The big advantage of this solution is that you can fix the code on the server too, commit these changes, update the bare repo, and then you can simply update the local repository. Another advantage is that you can clone the project from the bare repo, and more participants can take part in the development if they have access to the server.

Do you know a simpler way that has the above advantages?

I'm going to describe to you what I do to accomplish that, so it may be usefull to you. I'm also going to give some details so the new commers can be helped in the future.

I have a server where I like to run some personal experiments and I used git to deploy in the simplest way I found. It's done using the hooks that cames with git and very little shell script.

There we go:

First: initialize a bare repository in the server. The diference from a normal git repository is the lack of a working tree (the project files and common stuff). You just have the repository tree.

git init --bare 

If you look at the bare structure you are going to see something like this:

› git init --bare
Initialized empty Git repository in /home/git/mysite/
› ls
branches  config  description  HEAD  hooks  info  objects  refs

if you look closely at the hooks folder, you can find some interesting stuff:

daniel at heavens-empire into /home/git/mysite/hooks  
› ls
applypatch-msg.sample  post-update.sample     pre-commit.sample      
pre-push.sample        update.sample          commit-msg.sample      
pre-applypatch.sample  prepare-commit-msg.sample  pre-rebase.sample

Each one of that files is an example of a script that can be triggered after some events. Today we are looking at the post-receive event (it doesn't came in this version.. I don't know why). It happens when we push to that bare repository. You can create it and put a little of shell beauty in it:

#shows file content
daniel at heavens-empire into /home/git/mysite/hooks$ cat post-receive  

#!/bin/sh

GIT_WORK_TREE=/home/www/site git checkout -f

The GIT_WORK_TREE is going to determine where the files of the working tree that I've pushed are going to be. It's just like you have the repo in a folder and says that the tree is in another.

I've copied those scripts from my toy server, so I might say they should work with you, changing the apropriate work tree variable.

In my local repository I just add that bare folder as a remote repo. I'll use ssh to make things simple. In that case, make sure that the user you use to access has the proper permissions on the work tree folder:

git remote add mygitserver daniel@heavensempire.com:/home/git/mysite

Every time you make a push, your local git will send the commits. When the server git repo receives and accepts them, it will fire that post-receive script. With the work tree setted and the permissions given, it will make a simple checkout on the folder where the app is supposed to run.

I hope this is usefull for you and may be for others. Personaly I avoid editting things on the server. If you find it usefull to have a complete repo reflected in the server, you can also use the post-receive to make a clone/push and them a checkout for you in another repo. Something like:

cd /home/mysite
git pull repo/folder masterOrOther

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM