简体   繁体   English

如何使用Git部署项目

[英]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. 我找到了一个解决方案,但我觉得必须有一个更简单的部署方式,使用Git。 The solution is that 解决方案就是这样

  1. develop app with Git locally 在本地使用Git开发应用程序
  2. set up Git server 设置Git服务器
    • 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. 这不是一个简单的解决方案,可能有更好的方法来使用Git进行部署。 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. 我试图在服务器上单独使用裸存储库,并将git配置文件中的工作树变量设置为其他目录,但它不起作用:在第一次检出后,当我更新裸仓库并尝试查看更改,似乎一切正常,但工作目录中没有任何反应。 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). 然后我决定克隆裸存储库,因此克隆的repo有一个工作目录,它可以通过git pull更新(这将来自裸存储库的更改作为其原始远程存储库)。

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. 我有一个服务器,我喜欢运行一些个人实验,我用git以我发现的最简单的方式进行部署。 It's done using the hooks that cames with git and very little shell script. 它是使用git附带的钩子和很少的shell脚本完成的。

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). 与普通git存储库的不同之处在于缺少工作树(项目文件和常见的东西)。 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: 如果仔细观察hooks文件夹,你会发现一些有趣的东西:

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. GIT_WORK_TREE将确定我推送的工作树的文件的位置。 It's just like you have the repo in a folder and says that the tree is in another. 这就像你在一个文件夹中有repo并说树在另一个文件夹中。

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. 我会用ssh来简单化。 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. 每次推送时,您的本地git都会发送提交。 When the server git repo receives and accepts them, it will fire that post-receive script. 当服务器git repo接收并接受它们时,它将触发该接收后脚本。 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. Personaly我避免在服务器上编辑东西。 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. 如果您发现在服务器中反映完整的repo很有用,您还可以使用post-receive进行克隆/推送,并在另一个repo中为您结账。 Something like: 就像是:

cd /home/mysite
git pull repo/folder masterOrOther

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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