git repo代码部署策略及工具

一般在项目或者产品开发流程中,先是开发人员在本地做好开发及测试,其中可能包含很多用于测试用的目录以及源代码文件,在部署前往往会有一个build过程。web项目最终build产生出优化生产环境下减少http请求的bundle js,已经有了sprite image外加css代码的适合生产部署的系统。在部署的时候,很多文件可能并不想部署到服务器上去。如何处理?

一个可行的策略及步骤如下:

1. 使用.gitattributes文件中的export-ignore来指定哪些文件将不被打包到部署包;

2. 使用git archive命令将master或者integration等branch的内容打包: git archive intergation -o release.zip

3. 将上述release.zip文件解压后即可在生产系统中部署。

以上3个步骤已经是可以工作了。但是可能还是有待改进。比如我们releaes的package也希望在一个repo中做好版本控制,也就是希望放到repo中。同时,我们可能还有一个本地最后测试production release的需求。你当然可以另外建一个repo和目录来专门存放这个包,并且搭建对应的本地生产测试环境。但笔者建议更进一步,也就是使用同一个repo,但是又不希望看到开发repo中太多的历史信息。那么可以继续下面几个步骤:

4. 创建一个deliverable的branch,专门用于保存git archive产生的发布包,并用于本地生产测试。 

git checkout --orphan deliverable // 创建deliverable的orphan branch,该分支上将保存所有release包
git rm -rf . // 由于orphan分支创建后所有index的内容都将自动包内含integration分支的内容,我们需要全部删除

5. 将archive integration生成的发布包解压后放到deliverable分支的根目录中。这时deliverable就仅仅包含了干净的发布包文件目录。

6. 使用该发布包继续测试是否work,确认ok后,直接git a . git commit即可。

7. 以后有新的版本发布的话,重复第2.步到第6步即可。

使用git archive命令可以很好地拉取git repo中的一个snapshot,同时在.gitattributes文件中指定归档策略,将一些不必要的文件不放在部署服务器上。

# used to remove files from deployment using `git archive`
# git files
.gitattributes export-ignore
.gitignore export-ignore
# drush files
build.make export-ignore
patches.txt export-ignore
# zen 5.x files
sites/all/themes/*/sass export-ignore
sites/all/themes/*/sass-extensions export-ignore
sites/all/themes/*/images-source export-ignore
sites/all/themes/*/fonts export-ignore
sites/all/themes/*/config.rb export-ignore
sites/all/themes/*/STARTERKIT export-ignore

最后执行以下命令生成对应的包

git archive master | bzip2 -9 > latest.tar.bz2

 类似gh-pages方法部署

https://coderwall.com/p/-bcoua/how-to-create-gh-pages-branch

https://stackoverflow.com/questions/19980631/what-is-git-checkout-orphan-used-for

https://stackoverflow.com/questions/4750520/git-branch-gh-pages

https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/

https://gist.github.com/chrisjacob/833223

原文地址:https://www.cnblogs.com/kidsitcn/p/8596971.html