

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为多 DevOps 账户环境实施 GitHub Flow 分支策略
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments"></a>

*Mike Stephens 和 Abhilash Vinod，Amazon Web Services*

## Summary
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-summary"></a>

管理源代码存储库时，不同的分支策略会影响开发团队使用的软件开发和发布流程。常见分支策略的示例包括 Trunk、 GitHub Flow 和 Gitflow。这些策略使用不同的分支，并且在每种环境中执行的活动也各不相同。正在实施 DevOps 流程的组织将受益于可视化指南，以帮助他们了解这些分支策略之间的区别。在组织中使用此视觉对象可以帮助开发团队协调工作并遵循组织标准。此模式提供了这种视觉效果，并描述了在您的组织中实施 GitHub Flow 分支策略的过程。

这种模式是关于为具有多个 AWS 账户分支机构的组织选择和实施 DevOps 分支策略的文档系列的一部分。本系列旨在帮助您从一开始就应用正确的策略和最佳实践，以简化您在云中的体验。 GitHub Flow 只是您的组织可以使用的一种可能的分支策略。本文档系列还涵盖了[中继](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/implement-a-trunk-branching-strategy-for-multi-account-devops-environments.html)和 [Gitflow](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/implement-a-gitflow-branching-strategy-for-multi-account-devops-environments.html) 分支模型。如果你还没有这样做，我们建议你先查看[为多账户 DevOps 环境选择 Git 分支策略](https://docs.aws.amazon.com/prescriptive-guidance/latest/choosing-git-branch-approach/)，然后再按此模式实施指南。请务必谨慎选择适合组织的分支策略。

本指南提供了一个示意图，显示了组织如何实施 GitHub Flow 策略。建议您查看《Well-Ar [ DevOps chitec AWS ted 指南》，以查看最佳实](https://docs.aws.amazon.com/wellarchitected/latest/devops-guidance/devops-guidance.html)践。此模式包括 DevOps 流程中每个步骤的推荐任务、步骤和限制。

## 先决条件和限制
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-prereqs"></a>

**先决条件**
+ Git，[已安装](https://git-scm.com/downloads)。这可用作源代码存储库工具。
+ Draw.io，[已安装](https://github.com/jgraph/drawio-desktop/releases)。此应用程序可用于查看和编辑图表。

## 架构
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-architecture"></a>

**目标架构**

下图的使用方法与[旁氏表](https://en.wikipedia.org/wiki/Punnett_square)类似（维基百科）。您可以将垂直轴上的分支与水平轴上的 AWS 环境对齐，以确定在每个场景中要执行的操作。这些数字表示工作流中各项操作的顺序。此示例可引导您了解从 `feature` 分支到在生产环境中部署的整个过程。

![每个分支和环境中的 GitHub Flow 活动的 Punnett 方块。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/780a5bce-3cd2-4092-8537-b7a77c3d6b8d/images/8a2a774a-cd85-466e-838e-a9a1f3b58a63.png)


有关 GitHub Flow 方法中的 AWS 账户、环境和分支的更多信息，请参阅[为多账户 DevOps 环境选择 Git 分支策略](https://docs.aws.amazon.com/prescriptive-guidance/latest/choosing-git-branch-approach)。

**自动化和扩展**

持续集成和持续交付（CI/CD) is the process of automating the software release lifecycle. It automates much or all of the manual processes traditionally required to get new code from an initial commit into production. A CI/CD pipeline encompasses the sandbox, development, testing, staging, and production environments. In each environment, the CI/CD pipeline provisions any infrastructure that is needed to deploy or test the code. By using CI/CD, development teams can make changes to code that are then automatically tested and deployed. CI/CD管道）还通过强制执行一致性、标准、最佳实践以及功能接受和部署的最低接受程度，为开发团队提供管理和护栏。 有关更多信息，请参阅[上的 “练习持续集成和持续交付” AWS](https://docs.aws.amazon.com/whitepapers/latest/practicing-continuous-integration-continuous-delivery/welcome.html)。

AWS 提供了一套旨在帮助您构建 CI/CD 管道的开发人员服务。例如，[AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)是一项完全托管的持续交付服务，可帮助您实现发布管道的自动化，从而实现快速可靠的应用程序和基础设施更新。 [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html)编译源代码、运行测试和生成 ready-to-deploy软件包。有关更多信息，请参阅[上的开发者工具 AWS](https://aws.amazon.com/products/developer-tools/)。

## 工具
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-tools"></a>

**AWS 服务和工具**

AWS 提供了一套开发者服务，你可以用它们来实现这种模式：
+ [AWS CodeArtifact](https://docs.aws.amazon.com/codeartifact/latest/ug/welcome.html) 是一项高度可扩展的托管式构件存储库服务，有助于您存储和共享用于应用程序开发的软件包。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一项完全托管式构建服务，可编译源代码、运行单元测试和生成部署就绪的构件。
+ [AWS CodeDeploy](https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html)自动部署到亚马逊弹性计算云 (Amazon EC2)、本地实例、 AWS Lambda 函数或亚马逊弹性容器服务 (Amazon ECS) 服务。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可帮助您快速对软件发布过程的不同阶段进行建模和配置，并自动执行持续发布软件变更所需步骤。

**其他工具**
+ [Draw.io 桌面](https://github.com/jgraph/drawio-desktop/releases)是用于制作流程图和图表的应用程序。代码存储库包含 Draw.io 的 .drawio 格式模板。
+ [Figma](https://www.figma.com/design-overview/) 是一款专为协作而打造的在线设计工具。代码存储库包含 Figma 的 .fig 格式模板。

**代码存储库**

此模式下图表的源文件可在 GitHub [Git GitHub Flow 分支策略存储库中找](https://github.com/awslabs/git-branching-strategies-for-multiaccount-devops/tree/main/github-flow)到。其包括采用 PNG、draw.io 和 Figma 格式的文件。您可以修改这些图表，以为组织的流程提供支持。

## 最佳实践
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-best-practices"></a>

遵循 Well-Architect [AWS e DevOps d 指南和为多账户环境](https://docs.aws.amazon.com/wellarchitected/latest/devops-guidance/devops-guidance.html)[选择 Git 分支](https://docs.aws.amazon.com/prescriptive-guidance/latest/choosing-git-branch-approach/)策略中的最佳实践和建议。 DevOps 它们可以帮助您有效地实施 GitHub 基于 Flow 的开发、促进协作、提高代码质量和简化开发流程。

## 操作说明
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-epics"></a>

### 查看 GitHub Flow 工作流程
<a name="reviewing-the-github-flow-workflows"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 查看标准 GitHub 流程流程。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-a-github-flow-branching-strategy-for-multi-account-devops-environments.html) | DevOps 工程师 | 
| 查看错误修复 GitHub 流程流程。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-a-github-flow-branching-strategy-for-multi-account-devops-environments.html) | DevOps 工程师 | 
| 查看修补程序 GitHub 流程流程。 | GitHub Flow 旨在实现持续交付，在这种交付中，可以频繁可靠地将代码更改部署到更高的环境中。关键在于，每个 `feature` 分支都可以随时部署。<br />类似于 `feature` 或 `bugfix` 分支的 `Hotfix` 分支可以遵循与其他分支相同的过程。但是，考虑到其紧迫性，修补程序通常具有更高的优先级。根据团队的政策和情况的即时性，可以加快流程中的某些步骤。例如，修补程序的代码审查可能加速进行。因此，尽管修补程序过程与功能或错误修复过程相似，但由于围绕修补程序的紧迫性，可能需要修改程序遵守情况。务必制定有关管理修补程序的指导原则，以确保高效、安全地处理修补程序。 | DevOps 工程师 | 

## 问题排查
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 分支冲突 |  GitHub Flow 模型可能出现的一个常见问题是，需要在生产环境中进行修补程序，但需要在修改相同资源的`feature``bugfix`、或`hotfix`分支中进行相应的更改。我们建议您经常将来自 `main` 更改合并到较低的分支中，以避免在合并到 `main` 时发生重大冲突。 | 
| 团队成熟度 | GitHub Flow 鼓励每天部署到更高的环境，采用真正的持续集成和持续交付 (CI/CD)。团队必须具备工程成熟度，才能构建功能并为其创建自动化测试。在审批更改之前，团队必须对合并请求进行详尽的审查。这就打造了一种强大的工程文化，促进了开发过程的质量、问责制和效率。 | 

## 相关资源
<a name="implement-a-github-flow-branching-strategy-for-multi-account-devops-environments-resources"></a>

本指南不包括针对 Git 的培训；但是，如果您需要这种培训，则可以在互联网上找到许多可用的高质量的资源。我们建议从 [Git 文档](https://git-scm.com/doc)网站开始。

以下资源可以帮助您完成 GitHub Flow 分支之 AWS Cloud旅。

**AWS DevOps 指导**
+ [AWS DevOps 指南](https://docs.aws.amazon.com/wellarchitected/latest/devops-guidance/devops-guidance.html)
+ [AWS 部署管道参考架构](https://pipelines.devops.aws.dev/)
+ [什么是 DevOps？](https://aws.amazon.com/devops/what-is-devops/)
+ [DevOps resources](https://aws.amazon.com/devops/resources/)

**GitHub 流量指导**
+ [GitHub Flow 快速入门教程](https://docs.github.com/en/get-started/using-github/github-flow) () GitHub
+ [为什么 GitHub Flow？](https://githubflow.github.io/)

**其他资源**
+ [十二要素应用程序方法论](https://12factor.net/)（12factor.net）