/ docker

博智Git分支模型和Docker自动化部署方案

博智Git分支模型和Docker自动化部署方案

本文为笔者在公司推行的方案,在此分享出来,欢迎留言讨论。

整体方案

整体方案

当开发人员push代码到develop、release和master分支时将自动触发Docker镜像构建并相应部署到内网开发环境、阿里云VPC测试环境和阿里云VPC生产环境。

Git分支模型

整体方案

我们使用Git-flow工具来简化分支操作。图中develop和master为长线分支,feature、release和hotfix为短线分支。develop、release和master分别发布到内网开发环境、测试环境和生产环境。develop分支用来汇总下个版本的提交,在下个版本的release分支创建之前不允许提交非下个版本的功能到develop分支。当下个版本的功能开发完成后,从develop分支创建下个版本的release分支来测试,测试过程中Bug修复提交到release分支。测试完成后,合并release分支到develop分支和master分支,并发布master分支到生产环境,然后删除release分支。

初始化

为了能在Git项目里使用git flow命令,需要先初始化Git-flow。

> git flow init
Initialized empty Git repository in /Users/jagger/tmp/git-flow/.git/
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? [] 

开发

对每个新功能的开发建议创建一个feature分支来进行,也可直接在develop分支上进行。当一个功能开发完成后,合并feature分支到develop分支,然后删除该feature分支。

> git flow feature start rss-feed
Switched to a new branch 'feature/rss-feed'

Summary of actions:
- A new branch 'feature/rss-feed' was created, based on 'develop'
- You are now on branch 'feature/rss-feed'

Now, start committing on your feature. When done, use:

     git flow feature finish rss-feed

> git commit -am 'rss feed'
...

> git flow feature finish rss-feed
Switched to branch 'develop'
Already up-to-date.
Deleted branch feature/rss-feed (was 810a2a4).

Summary of actions:
- The feature branch 'feature/rss-feed' was merged into 'develop'
- Feature branch 'feature/rss-feed' has been removed
- You are now on branch 'develop'

> git push
...

测试

当下个版本的功能都已提交到develop分支,就可以创建下个版本的release分支来测试。测试过程中的Bug修复提交到release分支。

> git flow release start v1.1.0
Switched to a new branch 'release/v1.1.0'

Summary of actions:
- A new branch 'release/v1.1.0' was created, based on 'develop'
- You are now on branch 'release/v1.1.0'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:

     git flow release finish 'v1.1.0'

> git commit -am 'fix bug 100'
...

发布

测试通过后,合并release分支到develop分支和master分支,并发布master分支到生产环境,然后删除release分支。

> git flow release finish v1.1.0
Switched to branch 'master'
Deleted branch release/v1.1.0 (was 810a2a4).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Release branch has been merged into 'master'
- The release was tagged 'v1.1.0'
- Release branch has been back-merged into 'develop'
- Release branch 'release/v1.1.0' has been deleted

> git push origin master
...

Docker自动化部署

我们使用DaoCloud容器云服务来部署应用到我们的Docker容器云。容器云运行在阿里云ECS之上,提供了更高层级的云计算服务。

创建项目

一个DaoCloud项目对应一个Git仓库,项目存在的目的是为了构建镜像。DaoCloud除了支持GitHub和Bitbucket这些公共代码托管服务,还支持私有GitLab服务。

钢铁管家项目

通过配置“流水线”,使得有代码提交到某个分支时自动构建镜像和发布应用。

创建主机集群

主机集群

主机集群由安装了Docker Engine的一台或多台服务器构成。通过在服务器上安装DaoCloud Agent来把它加入到某个主机集群。部署应用时需指定用来运行应用的主机集群,或者集群里的某台服务器。

编排应用

编排应用就是把应用编制排放到主机集群上去运行。很多时候应用都需要依赖其它服务(或者说应用),比如MySQL、Redis等,启动应用时需要把它们一起启动起来,并且相互之间网络互通。这样的一组应用在DaoCloud里称为Stack,它使用兼容于Docker Compose的配置文件语法来描述。

编排应用

Stack配置文件里配置了一组有依赖关系且需要同时启动的应用。

参考资料

  1. Git-flow
  2. DaoCloud文档