Cloud Foundation Toolkit 基础知识

1. CFT 基础知识简介

b1d2ab0f35bb62a8.png

上次更新日期:2022 年 2 月 11 日

Cloud Foundation Toolkit 是什么?

实质上,CFT 提供了最佳做法模板,可帮助您快速开始使用 Google Cloud Platform。在本教程中,您将学习如何为 Cloud Foundation Toolkit 贡献力量。

所需条件

  • GitHub 账号。
  • 在您的机器上安装 Docker 或使用 Cloud Shell(Mac 安装Windows 安装
  • 用于修改代码的代码编辑器(例如:Visual Studio Code
  • 基本熟悉 Git 和 GitHub
  • 有一定的 Terraform 和基础架构即代码使用经验
  • 向服务账号授予 Project Creator 角色的权限
  • Google Cloud 组织、测试文件夹和结算账号

构建内容

在此 Codelab 中,您将学习如何为 Cloud Foundation Toolkit (CFT) 做贡献。

您将学习以下内容:

  • 设置开发环境为 CFT 做贡献
  • 向 CFT 模块添加特征
  • 为已添加的功能添加测试
  • 在 CFT 中执行集成测试
  • 执行 lint 测试
  • 将代码提交到 GitHub 并提交拉取请求 (PR)

您将通过向 Google Cloud Storage CFT 模块添加新功能来执行上述所有步骤。您将添加一个名为 "silly_label" 的标签,该标签会自动添加到通过 GCS CFT 模块创建的所有存储分区中。您还需要编写测试来验证您的功能并确保端到端集成。

2. 设置开发环境

如果需要,您可以使用 Cloud Shell 进行开发。如果您不想使用 Cloud Shell 为 CFT 做贡献,可以在计算机上设置开发环境。

设置 Git

GitHub 基于一个名为 Git 的开源版本控制系统 (VCS)。Git 负责在您的机器或 Cloud Shell 本地发生的所有与 GitHub 相关的任务。

  1. 使用 Cloud Shell 时,您无需安装 git,因为它已预安装。
$ git --version
# This will display the git version on the Cloud Shell.

如果您要在计算机上设置开发环境,则需要安装 Git。

在 Git 中设置您的用户名和电子邮件地址

Git 使用用户名将提交与身份相关联。Git 用户名与您的 GitHub 用户名不同。

您可以使用 git config 命令更改与您的 Git 提交内容关联的名称。使用 git config 更改与 Git 提交相关联的名称只会影响将来的提交,而不会更改用于过去提交的名称。

您已成功设置 Git,应该能够克隆、创建和克隆分支。在此 Codelab 中,我们将广泛使用 Git。

3. 复刻 CFT 的 GCS 代码库

克隆 CFT 代码库

在上一步中,您可以在本地机器或 Cloud Shell 上设置 Git。现在,您需要复刻 Google Cloud Storage CFT 代码库,才能开始贡献代码。

分支是代码库的副本。通过创建代码库分支,您可以随意尝试更改,而不会影响原始项目。

分支最常见的用途是提出对他人项目的更改建议,或以他人的项目作为起点来构思自己的创意。

例如,您可以使用分支提出与修复 bug 相关的更改。如需修复 bug,您可以:

  • 创建代码库分支。
  • 解决问题。
  • 向项目所有者提交拉取请求。

创建 CFT 代码库分支的步骤:

  1. 打开网络浏览器并导航到 terraform-google-modules/terraform-google-cloud-storage 代码库。我们将将此代码库用于整个 Codelab。
  2. 在页面的右上角,点击创建分支

9dc18f15ca662b56

  1. 系统会显示一个选项,供您选择想要创建分支的位置、选择您的个人资料,以及为代码库创建分支。

在本地克隆分支

您创建的分支是 GCS 模块代码库的副本。现在,您需要将此代码库克隆到本地环境,以添加新功能。

克隆分支的步骤:

  1. 打开网络浏览器并导航到 terraform-google-modules/terraform-google-cloud-storage 上的分支。
  2. 在右上角,您会看到“代码”请点击该按钮。

98f8be8df319dcd8.png

  1. 点击“代码”按钮后按钮,点击“复制”图标复制分支的网址。您将使用此网址将分支克隆到本地环境。

e61e1da6371f2a1d.png

  1. 转到 VSCode 或机器中的终端,然后克隆分支。
$ git clone <url>
# This command will clone your fork locally.
# Paste the copied URL from the previous step.
  1. 在本地克隆分支后,您应该进入仓库,基于分支创建一个新分支,并对临时分支进行代码更改。

按照惯例,您可以按如下方式命名分支:

  • 对于功能请求:feature/feature-name
  • 对于内部更新,internal/change-name
  • bug 修复:bugfix/issue-name

由于您要添加新功能,因此可以调用临时分支 feature/silly_label

$ cd terraform-google-cloud-storage
# This command takes you into the cloned directory on your local machine.

$ git branch
# This command tells your current branch
# When you run this for the first time after you have cloned, your 
# output should say "master", that is your fork.

$ git checkout -b feature/silly_label
# This command creates a new branch on your fork and switches your 
# branch to the newly created branch.

$ git branch
# This command will confirm your current branch to be "feature/silly_label"

现在,您已完成所有设置,可以开始处理 Cloud Foundation Toolkit 了!

4. 创建测试环境

标准 CFT 开发流程基于使用隔离的测试项目进行测试。此步骤将指导您通过服务账号创建测试项目(基于标准配置)。

0. 安装 Docker Engine

如果您将机器用于开发目的,则需要安装 Docker Engine

1. 安装 Google Cloud SDK

如果您使用的是 GCP Cloud Shell,则无需安装 Google Cloud SDK

前往 Google Cloud SDK,下载适用于您的平台的交互式安装程序。

2. 设置配置

如需创建测试环境,您需要一个 Google Cloud 组织、一个测试文件夹和一个结算账号。这些值需要通过环境变量设置:

export TF_VAR_org_id="your_org_id"
export TF_VAR_folder_id="your_folder_id"
export TF_VAR_billing_account="your_billing_account_id"

3. 设置您的服务账号

在创建测试环境之前,您需要将服务账号密钥下载到测试环境。此服务账号需要 Project Creator、Billing Account UserOrganization Viewer 角色。这些步骤可帮助您创建新的服务账号,但您也可以重复使用现有账号。

3.1 创建或选择种子 GCP 项目

在创建服务账号之前,您需要选择一个项目来托管它。您也可以创建新项目。

gcloud config set core/project YOUR_PROJECT_ID

3.2 启用 Google Cloud API

在您的种子项目上启用以下 Google Cloud API:

gcloud services enable cloudresourcemanager.googleapis.com
gcloud services enable iam.googleapis.com
gcloud services enable cloudbilling.googleapis.com

3.3 创建服务账号

创建新的服务账号来管理测试环境:

# Creating a service account for CFT.
gcloud iam service-accounts create cft-onboarding \
  --description="CFT Onboarding Terraform Service Account" \
  --display-name="CFT Onboarding"

# Assign SERVICE_ACCOUNT environment variable for later steps
export SERVICE_ACCOUNT=cft-onboarding@$(gcloud config get-value core/project).iam.gserviceaccount.com

验证您的服务账号是否已创建:

gcloud iam service-accounts list --filter="EMAIL=${SERVICE_ACCOUNT}"

3.4 向服务账号授予 Project Creator、Billing Account User 和 Organization Viewer 角色

gcloud resource-manager folders add-iam-policy-binding ${TF_VAR_folder_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/resourcemanager.projectCreator"
gcloud organizations add-iam-policy-binding ${TF_VAR_org_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/billing.user"
gcloud beta billing accounts add-iam-policy-binding ${TF_VAR_billing_account} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/billing.user"
gcloud organizations add-iam-policy-binding ${TF_VAR_org_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/resourcemanager.organizationViewer"

现在,您已经拥有了一个可用于管理测试环境的服务账号。

4. 准备 Terraform 凭据

为了创建测试环境,您需要将服务账号密钥下载到 shell 中。

4.1 服务账号密钥

为 Terraform 创建和下载服务账号密钥

gcloud iam service-accounts keys create cft.json --iam-account=${SERVICE_ACCOUNT}

4.2 设置 Terraform 凭据

使用环境变量 SERVICE_ACCOUNT_JSON 向 Terraform 提供密钥,将值设置为服务账号密钥的内容

export SERVICE_ACCOUNT_JSON=$(< cft.json)

将凭据信息存储在环境变量中后,移除密钥文件。稍后您可以根据需要使用相同的命令重新创建密钥。

rm -rf cft.json

5. 为 Terraform 部署创建测试项目

现在,一切都已准备就绪,您可以使用单个命令创建测试项目。从 terraform-google-cloud-storage 目录根目录运行以下命令:

make docker_test_prepare

运行 make docker_test_prepare 时,您将看到以下输出。最后,您会收到已创建的测试 project_id,您将使用新功能部署和测试 Cloud Storage 模块。如果您在关联结算账号时遇到问题,请参阅问题排查步骤

macbookpro3:terraform-google-cloud-storage user$ make docker_test_prepare
docker run --rm -it \
                -e SERVICE_ACCOUNT_JSON \
                -e TF_VAR_org_id \
                -e TF_VAR_folder_id \
                -e TF_VAR_billing_account \
                -v /Users/cft/terraform-google-cloud-storage:/workspace \
                gcr.io/cloud-foundation-cicd/cft/developer-tools:0.8.0 \
                /usr/local/bin/execute_with_credentials.sh prepare_environment
Activated service account credentials for: [cft-onboarding@<project_id>.iam.gserviceaccount.com]
Activated service account credentials for: [cft-onboarding@<project_id>.iam.gserviceaccount.com]
Initializing modules...

Initializing the backend...

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.google-beta: version = "~> 3.9"
* provider.null: version = "~> 2.1"
* provider.random: version = "~> 2.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
module.project.module.project-factory.null_resource.preconditions: Refreshing state... [id=8723188031607443970]
module.project.module.project-factory.null_resource.shared_vpc_subnet_invalid_name[0]: Refreshing state... [id=5109975723938185892]
module.project.module.gsuite_group.data.google_organization.org[0]: Refreshing state...
module.project.module.project-factory.random_id.random_project_id_suffix: Refreshing state... [id=rnk]
module.project.module.project-factory.google_project.main: Refreshing state... [id=<project-id>]
module.project.module.project-factory.google_project_service.project_services[0]: Refreshing state... [id=<project-id>/storage-api.googleapis.com]
module.project.module.project-factory.google_project_service.project_services[1]: Refreshing state... [id=<project-id>/cloudresourcemanager.googleapis.com]
module.project.module.project-factory.google_project_service.project_services[2]: Refreshing state... [id=<project-id>/compute.googleapis.com]
module.project.module.project-factory.data.null_data_source.default_service_account: Refreshing state...
module.project.module.project-factory.google_service_account.default_service_account: Refreshing state... [id=projects/ci-cloud-storage-ae79/serviceAccounts/project-service-account@<project-id>.iam.gserv
iceaccount.com]
module.project.module.project-factory.google_project_service.project_services[3]: Refreshing state... [id=<project-id>/serviceusage.googleapis.com]
module.project.module.project-factory.null_resource.delete_default_compute_service_account[0]: Refreshing state... [id=3576396874950891283]
google_service_account.int_test: Refreshing state... [id=projects/<project-id>/serviceAccounts/cft-onboarding@<project-id>.iam.gserviceaccount.com]
google_service_account_key.int_test: Refreshing state... [id=projects/<project-id>/serviceAccounts/cft-onboarding@<project-id>.iam.gserviceaccount.com/keys/351009a1e011e88049ab2097994d1c627a61
6961]
google_project_iam_member.int_test[1]: Refreshing state... [id=<project-id>/roles/iam.serviceAccountUser/serviceaccount:cft-onboarding@<project-id>.iam.gserviceaccount.com]
google_project_iam_member.int_test[0]: Refreshing state... [id=<project-id>/roles/storage.admin/serviceaccount:cft-onboarding@<project-id>.iam.gserviceaccount.com]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

project_id = <test-project-id>
sa_key = <sensitive>
Found test/setup/make_source.sh. Using it for additional explicit environment configuration.

现在,您已经创建了一个由 project_id 引用的测试项目,如控制台输出所示。您的开发和测试环境已设置完毕。

5. 向 CFT 模块添加新特征

现在您的开发和测试环境已设置完毕,我们开始添加“silly_label”添加到 google-cloud-storage CFT 模块中

确保您位于 terraform-google-cloud-storage 中,并打开 main.tf 文件,如下面的文件夹结构所示。

ac1dba25408abd09.png

由于“silly_label”是一个标签,您需要在第 27 行的变量“labels”中添加该地图项。位于 main.tf 中,如下所示:

terraform-google-cloud-storage/main.tf

resource "google_storage_bucket" "buckets" {
 <...>
 storage_class = var.storage_class
 // CODELAB:Add silly label in labels variable
 labels        = merge(var.labels, { name = replace("${local.prefix}${lower(each.value)}", ".", "-") }, { "silly" = var.silly_label })
 force_destroy = lookup(
 <...>
}

现在,您将在上述文件夹结构中看到的 variables.tf 中添加 silly_label 变量。

请复制以下代码,并将其添加到 variables.tf 中的第 31 行,并确保您添加的变量块的上方和下方都有新的行字符。

terraform-google-cloud-storage/variables.tf

variable "names" {
 description = "Bucket name suffixes."
 type        = list(string)
}

// CODELAB: Add "silly_label" variable to variables.tf between "names" and "location"
variable "silly_label" {
 description = "Sample label for bucket."
 type        = string
}

variable "location" {
 description = "Bucket location."
 default     = "EU"
}

6. 向存储分区示例添加新功能

您已经将功能添加到模块的 main.tf 中,现在您将通过示例测试添加的功能。

“silly_label”需要添加到 examples/multiple-buckets/main.tf

此示例将在下一步执行集成测试。

将下面的变量 silly_label 行粘贴到 main.tf 中的第 27 行,位于 terraform-google-cloud-storage/examples/multiple-buckets/ 中,如文件夹结构所示:

5224fefbbcc61d89

terraform-google-cloud-storage/examples/multiple-buckets/main.tf

module "cloud_storage" {
 <...>
 // CODELAB: Add "silly_label" as an example to main.tf.
 silly_label        = "awesome"

 <..>
}

7. 更新蓝图测试以检查功能

您已将功能添加到模块的 main.tf 中,然后又将功能添加到了 multiple_buckets 示例中。现在,您需要通过使用 Golang 编写的蓝图集成测试来测试您的功能。

您将在以下文件夹结构中的 multiple_buckets_test.go 文件中添加新测试:

72ea272d4792405

你添加了“silly_label”。

在以下代码中,您将通过 gcloud alpha storage 命令获取每个存储分区的标签,然后检查该命令返回的输出。

test/integration/multiple_buckets/multiple_buckets_test.go

func TestMultipleBuckets(t *testing.T) {
 <..>
op := gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s", bucketName), gcloudArgs).Array()[0]

// verify silly label on each bucket
assert.Equal("awesome", op.Get("metadata.labels.silly").String(), "should have silly label set to awesome")

// verify lifecycle rules
...
}

8. 在 CFT 中执行集成测试

集成测试

集成测试用于验证根模块、子模块和示例的行为。添加、更改和修复时应伴随测试。

集成测试使用蓝图测试框架编写,并使用 CFT CLI 运行。为方便起见,这些工具封装在 Docker 映像中。

这些测试的一般策略是验证示例模块的行为,从而确保根模块、子模块和示例模块在功能上均正确无误。

在交互式执行中,您要通过多个命令执行每个步骤。

  1. 运行 make docker_run,以交互模式启动测试 Docker 容器。

Make 是一种构建自动化工具,通过读取名为 Makefile 的文件(指定了如何派生目标程序),自动从源代码构建可执行程序和库。当您更改文件时,Docker 容器必须自动更新。

运行 make docker_run 时,您将在 Docker 容器中创建一个工作区,并激活您的服务账号的凭据。在接下来的步骤中,将使用该工作区来运行测试。

您将在终端中看到以下输出:

Activated service account credentials for: [cft@<PROJECT_ID>.iam.gserviceaccount.com]
  1. 运行 cft test list 以列出工作区中的所有蓝图测试。

您将在终端中看到以下输出:

[root@CONTAINER_ID workspace]# cft test list
 NAME                           | CONFIG                    | LOCATION                                                   
--------------------------------+---------------------------+------------------------------------------------------------
 TestAll/examples/simple_bucket | examples/simple_bucket    | test/integration/discover_test.go                          
 TestMultipleBuckets            | examples/multiple_buckets | test/integration/multiple_buckets/multiple_buckets_test.go 

  1. 运行 cft test run <EXAMPLE_NAME> --stage init 以初始化示例。在本例中,用于初始化 TestMultipleBuckets 测试运行 cft test run TestMultipleBuckets --stage init。在运行测试时,您还可以额外使用 --verbose 标志来获取其他信息。

此初始化阶段会初始化 Terraform 示例。

您将在终端中看到以下输出。

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage init --verbose
INFO[02-09|08:24:31] using test-dir: test/integration 
...
TestMultipleBuckets 2022-02-09T08:24:35Z command.go:179: Terraform has been successfully initialized!
...
TestMultipleBuckets 2022-02-09T08:24:35Z command.go:100: Running command terraform with args [validate]
TestMultipleBuckets 2022-02-09T08:24:36Z command.go:179: Success! The configuration is valid.
...
--- PASS: TestMultipleBuckets (4.05s)
  1. 运行 cft test run <EXAMPLE_NAME> --stage apply 以应用示例模块。

此步骤会将在上一阶段中初始化的示例应用于之前在此 Codelab 中创建的 GCP 项目。

您将在终端中看到以下输出。

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage apply --verbose
INFO[02-09|08:28:11] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: 
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: Outputs:
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: 
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: names = {
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179:   "one" = "multiple-buckets-erp1-eu-one"
...
--- PASS: TestMultipleBuckets (6.51s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    6.548s
  1. 运行 cft test run <EXAMPLE_NAME> --stage verify 以验证该示例是否创建了预期的基础架构。

此步骤将在 TestMultipleBuckets 中运行验证函数。验证通常是通过执行 gcloud 命令来检索资源当前状态的 JSON 输出,并断言当前状态与示例中所声明的状态相同。

如果您收到任何错误,将会看到预期内容以及测试命令收到的内容。

您将在终端中看到以下输出。

cft test run TestMultipleBuckets --stage verify --verbose
INFO[02-09|08:30:19] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:100: Running command terraform with args [output -no-color -json names_list]
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:179: ["multiple-buckets-erp1-eu-one","multiple-buckets-erp1-eu-two"]
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:100: Running command gcloud with args [alpha storage ls --buckets gs://multiple-buckets-erp1-eu-one --project ci-cloud-storage-8ce9 --json]
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179: [
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179: {
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179:   "url": "gs://multiple-buckets-erp1-eu-one/",
...
TestMultipleBuckets 2022-02-09T08:30:33Z command.go:179: ]
2022/02/09 08:30:33 RUN_STAGE env var set to verify
2022/02/09 08:30:33 Skipping stage teardown
--- PASS: TestMultipleBuckets (12.32s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    12.359s
  1. 运行 cft test run <EXAMPLE_NAME> --stage teardown 以拆解该示例。

此步骤将销毁您在上述步骤中创建的基础架构。此步骤还将销毁在项目中创建的 GCS 存储分区以及您添加到 GCS 模块的标签。

您可以在终端中查看以下输出。

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage teardown --verbose
INFO[02-09|08:36:02] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:36:06Z command.go:100: Running command terraform with args [destroy -auto-approve -input=false -lock=false]
TestMultipleBuckets 2022-02-09T08:36:07Z command.go:179: module.cloud_storage.random_id.bucket_suffix: Refreshing state... [id=mNA]
TestMultipleBuckets 2022-02-09T08:36:07Z command.go:179: random_string.prefix: Refreshing state... [id=erp1]
TestMultipleBuckets 2022-02-09T08:36:08Z command.go:179: module.cloud_storage.google_storage_bucket.buckets["two"]: Refreshing state... [id=multiple-buckets-erp1-eu-two]
...
TestMultipleBuckets 2022-02-09T08:36:10Z command.go:179: Destroy complete! Resources: 6 destroyed.
TestMultipleBuckets 2022-02-09T08:36:10Z command.go:179: 
--- PASS: TestMultipleBuckets (6.62s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    6.654s
  1. 运行 exit 以退出测试容器。

9. 为输入和输出生成文档

根模块、子模块和示例模块的 README 文件中的“Inputs”(输入)和“Outputs”(输出)表是根据相应模块的 variablesoutputs 自动生成的。如果模块接口发生更改,则必须刷新这些表。

运行以下命令:

make generate_docs
# This will generate new Inputs and Outputs tables

10. 在 CFT 中执行 lint 测试

linter 是一种工具,用于分析源代码,以标记编程错误、bug、样式错误和可疑结构。

代码库中的许多文件可以执行 lint 请求或设置格式,以保持质量标准。为确保 CFT 的质量,您将使用 lint 测试。

运行以下命令:

make docker_test_lint
# This will run all lint tests on your repo

11. 在 GitHub 上提交 PR

现在您已在本地更改了代码并通过集成测试对其进行了测试,接下来便可以将此代码发布到主代码库。

如需使代码在主代码库上可用,您需要将代码更改提交到分支,并将其推送到主代码库。为了将您的代码添加到您在 Codelab 开始时派生的主代码库,您需要在将代码提交到代码库后针对主代码库引发拉取请求 (PR)。

当您提交 PR 时,代码库管理员会收到通知,审核建议的代码更改。此外,您还可以将其他用户添加为审核者,以获取有关代码更改的反馈。PR 将触发 Cloud Build,后者将对代码库运行测试。

代码审核者会根据您的代码更改对代码进行注释,如果需要根据最佳实践和文档更改某些内容,还会要求进行修改。管理员将审核您的代码更改,确保您的代码符合代码库的要求,并且可能会再次要求您进行一些更改,然后再将代码合并到主代码库。

执行以下步骤,将代码提交到分支分支,并将代码推送到分支分支

  1. 第一步是将更改的文件添加到本地代码库中。
$ git add main.tf
$ git add README.md
$ git add variables.tf
$ git add examples/multiple-buckets/main.tf
$ git add test/integration/multiple_buckets/multiple_buckets_test.go
# The ‘git add' command adds the file in the local repository and 
# stages the file for commit. To unstage a file, use git reset HEAD YOUR-FILE
  1. 您的文件现已暂存,接下来您需要提交更改。
$ git commit -m "First CFT commit"
# This will commit the staged changes and prepares them to be pushed 
# to a remote repository. To remove this commit and modify the file, 
# use 'git reset --soft HEAD~1' and commit and add the file again.
  1. 将本地代码库中已提交的更改推送到 GitHub,以创建拉取请求 (PR)。
$ git push -u origin feature/silly_label
# Pushes the changes in your local repository up to the remote
# repository you specified as the origin

您的代码更改现在已准备好处理拉取请求!

执行以下步骤,为 terraform-google-modules/terraform-google-cloud-storage 代码库创建 PR

  1. 在网络浏览器中,导航到代码库的主页。
  2. 您会在横幅中看到一条建议,让您从分支中打开 PR。点击“比较和拉取请求”。

60e7ae0cbc11588e

  1. 输入拉取请求的标题和说明,以描述代码更改。尽可能具体但简明扼要。

329342f7e9d64410

  1. 如需创建可供审核的拉取请求,请点击“创建拉取请求”。
  2. 您会看到由于 PR 而触发的 Cloud Build 触发器正在运行。
  3. 如果您遇到任何问题,请参阅有关从分支打开拉取请求的官方 GitHub 文档。

您已成功将第一项代码更改推送到分支分支,并针对主分支发起了您的第一个 CFT PR!

12. 恭喜

恭喜!您已成功向 CFT 模块添加了一项功能,并已提交 PR 以供审核!

您向 CFT 模块添加了一项功能,通过示例在本地对其进行了测试,并在将代码提交到 GitHub 之前执行了测试。最后,您提交了 PR 以供审核,并最终将其合并到 CFT。

现在,您已了解开始使用 Cloud Foundation Toolkit 的重要步骤。