Skip to content

Commit 62f5f7d

Browse files
feat(modules)!: enable cloudbuildv2 repository support on tf_cloudbuild_builder and tf_cloudbuild_workspace (#299)
Co-authored-by: Daniel Andrade <dandrade@ciandt.com>
1 parent e2b589e commit 62f5f7d

File tree

56 files changed

+2792
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2792
-41
lines changed

build/int.cloudbuild.yaml

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ steps:
8989

9090
- id: apply-tfsource
9191
waitFor:
92-
- create-all
92+
- destroy-simple-folder
9393
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
9494
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildSourceSimple --stage apply --verbose']
9595
- id: verify-tfsource
@@ -119,6 +119,44 @@ steps:
119119
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
120120
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilder --stage teardown --verbose']
121121

122+
- id: apply-tfbuilder-github
123+
waitFor:
124+
- create-all
125+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
126+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage apply --verbose']
127+
secretEnv: ['IM_GITHUB_PAT']
128+
- id: verify-tfbuilder-github
129+
waitFor:
130+
- apply-tfbuilder-github
131+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
132+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage verify --verbose']
133+
secretEnv: ['IM_GITHUB_PAT']
134+
- id: teardown-tfbuilder-github
135+
waitFor:
136+
- verify-tfbuilder-github
137+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
138+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage teardown --verbose']
139+
secretEnv: ['IM_GITHUB_PAT']
140+
141+
- id: apply-tfbuilder-gitlab
142+
waitFor:
143+
- create-all
144+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
145+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage apply --verbose']
146+
secretEnv: ['IM_GITLAB_PAT']
147+
- id: verify-tfbuilder-gitlab
148+
waitFor:
149+
- apply-tfbuilder-gitlab
150+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
151+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage verify --verbose']
152+
secretEnv: ['IM_GITLAB_PAT']
153+
- id: teardown-tfbuilder-gitlab
154+
waitFor:
155+
- verify-tfbuilder-gitlab
156+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
157+
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage teardown --verbose']
158+
secretEnv: ['IM_GITLAB_PAT']
159+
122160
- id: apply-tfworkspace
123161
waitFor:
124162
- create-all
@@ -173,6 +211,44 @@ steps:
173211
args: ['/bin/bash', '-c', 'cft test run TestIMCloudBuildWorkspaceGitLab --stage teardown --verbose']
174212
secretEnv: ['IM_GITLAB_PAT']
175213

214+
- id: apply-tfworkspace-github
215+
waitFor:
216+
- create-all
217+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
218+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage apply --verbose']
219+
secretEnv: ['IM_GITHUB_PAT']
220+
- id: verify-tfworkspace-github
221+
waitFor:
222+
- apply-tfworkspace-github
223+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
224+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage verify --verbose']
225+
secretEnv: ['IM_GITHUB_PAT']
226+
- id: teardown-tfworkspace-github
227+
waitFor:
228+
- verify-tfworkspace-github
229+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
230+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage teardown --verbose']
231+
secretEnv: ['IM_GITHUB_PAT']
232+
233+
- id: apply-tfworkspace-gitlab
234+
waitFor:
235+
- create-all
236+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
237+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage apply --verbose']
238+
secretEnv: ['IM_GITLAB_PAT']
239+
- id: verify-tfworkspace-gitlab
240+
waitFor:
241+
- apply-tfworkspace-gitlab
242+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
243+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage verify --verbose']
244+
secretEnv: ['IM_GITLAB_PAT']
245+
- id: teardown-tfworkspace-gitlab
246+
waitFor:
247+
- verify-tfworkspace-gitlab
248+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
249+
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage teardown --verbose']
250+
secretEnv: ['IM_GITLAB_PAT']
251+
176252
availableSecrets:
177253
secretManager:
178254
- versionName: $_IM_GITHUB_PAT_SECRET_ID/versions/latest

docs/upgrading_to_v9.0.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Upgrading to v9.0
2+
3+
The v9.0 release of *bootstrap* is a backwards incompatible release.
4+
5+
Some variables default values were removed to align with the restriction that Cloud Build Repositories (2nd Gen) cannot be created in multi-regions or in the `global` region.
6+
7+
You need to update your configurations if you used the default values to prevent resources to be recreated.
8+
9+
## Default value for variable `trigger_location` in module `tf_cloudbuild_workspace` was removed
10+
11+
To preserve the resources created before. include the input `trigger_location` with the previous default value in the module call
12+
13+
```diff
14+
module "tf_workspace" {
15+
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_workspace"
16+
- version = "~> 8.0"
17+
+ version = "~> 9.0"
18+
19+
+ trigger_location = "global"
20+
```
21+
22+
## Default value for variables `trigger_location` and `gar_repo_location` in module `tf_cloudbuild_builde` were removed
23+
24+
To preserve the resources created before, include the inputs `trigger_location` and `gar_repo_location` with the previous default values in the module call
25+
26+
```diff
27+
module "cloudbuilder" {
28+
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
29+
- version = "~> 8.0"
30+
+ version = "~> 9.0"
31+
32+
+ trigger_location = "global"
33+
+ gar_repo_location = "us"
34+
```
35+
36+
An apply after adding the two inputs will still have an *in-place update* in the `google_workflows_workflow` created by the module.
37+
38+
The endpoint that is used to trigger a build was replaced with a new one that allows a location to be provided.
39+
40+
```
41+
# module.cloudbuilder.google_workflows_workflow.builder will be updated in-place
42+
```

examples/tf_cloudbuild_builder_simple/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module "cloudbuilder" {
2020

2121
project_id = module.enabled_google_apis.project_id
2222
dockerfile_repo_uri = google_sourcerepo_repository.builder_dockerfile_repo.url
23+
trigger_location = "us-central1"
24+
gar_repo_location = "us-central1"
2325
# allow logs bucket to be destroyed
2426
cb_logs_bucket_force_destroy = true
2527
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM gcr.io/cloud-builders/gcloud-slim
16+
17+
# Use ARG so that values can be overriden by user/cloudbuild
18+
ARG TERRAFORM_VERSION=1.1.0
19+
20+
ENV ENV_TERRAFORM_VERSION=$TERRAFORM_VERSION
21+
22+
RUN apt-get update && \
23+
/builder/google-cloud-sdk/bin/gcloud -q components install alpha beta terraform-tools && \
24+
apt-get -y install curl jq unzip git ca-certificates gnupg && \
25+
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip --output terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip && \
26+
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_SHA256SUMS.sig --output terraform_SHA256SUMS.sig && \
27+
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_SHA256SUMS --output terraform_SHA256SUMS && \
28+
curl https://keybase.io/hashicorp/pgp_keys.asc --output pgp_keys.asc && \
29+
gpg --import pgp_keys.asc && \
30+
gpg --verify terraform_SHA256SUMS.sig terraform_SHA256SUMS && \
31+
grep terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip terraform_SHA256SUMS | shasum --algorithm 256 --check && \
32+
unzip terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip -d /builder/terraform && \
33+
rm -f terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip terraform_SHA256SUMS && \
34+
apt-get --purge -y autoremove && \
35+
apt-get clean && \
36+
rm -rf /var/lib/apt/lists/*
37+
38+
ENV PATH=/builder/terraform/:$PATH
39+
ENTRYPOINT ["terraform"]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Overview
2+
3+
This example demonstrates the simplest usage of the [tf_cloudbuild_builder](../../modules/tf_cloudbuild_builder/) module with a Cloud Build repositories (2nd gen) GitHub repository.
4+
5+
For GitHub connections you will need:
6+
7+
- Install the [Cloud Build App](https://github.com/apps/google-cloud-build) on Github.
8+
- Create a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) on Github with [scopes](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps#available-scopes) `repo` and `read:user` (or if app is installed in a organization use `read:org`).
9+
10+
For more information on this topic refer to the Cloud Build repositories (2nd gen) documentation for
11+
[Connect to a GitHub repository](https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen).
12+
13+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
14+
## Inputs
15+
16+
| Name | Description | Type | Default | Required |
17+
|------|-------------|------|---------|:--------:|
18+
| github\_pat | GitHub personal access token. | `string` | n/a | yes |
19+
| project\_id | The ID of the project in which to provision resources. | `string` | n/a | yes |
20+
| repository\_uri | The URI of the GitHub repository where the Terraform configs are stored. | `string` | n/a | yes |
21+
22+
## Outputs
23+
24+
| Name | Description |
25+
|------|-------------|
26+
| artifact\_repo | GAR Repo created to store TF Cloud Builder images |
27+
| cloudbuild\_trigger\_id | Trigger used for building new TF Builder |
28+
| location | The location in which the resources were provisioned |
29+
| project\_id | The ID of the project in which the resources were provisioned |
30+
| repository\_id | ID of the Cloud Build repositories (2nd gen) repository |
31+
| scheduler\_id | Scheduler ID for periodically triggering TF Builder build Workflow |
32+
| workflow\_id | Workflow ID for triggering new TF Builder build |
33+
34+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "enabled_google_apis" {
18+
source = "terraform-google-modules/project-factory/google//modules/project_services"
19+
version = "~> 15.0"
20+
21+
project_id = var.project_id
22+
disable_services_on_destroy = false
23+
24+
activate_apis = [
25+
"iam.googleapis.com",
26+
"compute.googleapis.com",
27+
"workflows.googleapis.com",
28+
"artifactregistry.googleapis.com",
29+
"cloudbuild.googleapis.com",
30+
"cloudscheduler.googleapis.com"
31+
]
32+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
// Found in the URL of your Cloud Build GitHub app configuration settings
19+
// https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#connecting_a_github_host_programmatically
20+
github_app_installation_id = "47590865"
21+
22+
# GitHub repo url of form "github.com/owner/name"
23+
repoURL = endswith(var.repository_uri, ".git") ? var.repository_uri : "${var.repository_uri}.git"
24+
repoURLWithoutSuffix = trimsuffix(local.repoURL, ".git")
25+
gh_repo_url_split = split("/", local.repoURLWithoutSuffix)
26+
gh_name = local.gh_repo_url_split[length(local.gh_repo_url_split) - 1]
27+
28+
location = "us-central1"
29+
}
30+
31+
data "google_project" "project" {
32+
project_id = var.project_id
33+
}
34+
35+
// Added to various IDs to prevent potential conflicts for deployments targeting the same repository.
36+
resource "random_id" "resources_random_id" {
37+
byte_length = 4
38+
}
39+
40+
module "cloudbuilder" {
41+
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
42+
version = "~> 8.0"
43+
44+
project_id = module.enabled_google_apis.project_id
45+
dockerfile_repo_uri = google_cloudbuildv2_repository.repository_connection.id
46+
dockerfile_repo_type = "GITHUB"
47+
use_cloudbuildv2_repository = true
48+
trigger_location = local.location
49+
gar_repo_location = local.location
50+
bucket_name = "tf-cloudbuilder-build-logs-${var.project_id}-gh"
51+
gar_repo_name = "tf-runners-gh"
52+
workflow_name = "terraform-runner-workflow-gh"
53+
trigger_name = "tf-cloud-builder-build-gh"
54+
55+
# allow logs bucket to be destroyed
56+
cb_logs_bucket_force_destroy = true
57+
}
58+
59+
// Create a secret containing the personal access token and grant permissions to the Service Agent.
60+
resource "google_secret_manager_secret" "github_token_secret" {
61+
project = var.project_id
62+
secret_id = "builder-gh-${random_id.resources_random_id.dec}-${local.gh_name}"
63+
64+
labels = {
65+
label = "builder-gh-${random_id.resources_random_id.dec}"
66+
}
67+
68+
replication {
69+
auto {}
70+
}
71+
}
72+
73+
// Personal access token from VCS.
74+
resource "google_secret_manager_secret_version" "github_token_secret_version" {
75+
secret = google_secret_manager_secret.github_token_secret.id
76+
secret_data = var.github_pat
77+
}
78+
79+
resource "google_secret_manager_secret_iam_member" "github_token_iam_member" {
80+
project = var.project_id
81+
secret_id = google_secret_manager_secret.github_token_secret.id
82+
role = "roles/secretmanager.secretAccessor"
83+
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
84+
}
85+
86+
// See https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen
87+
resource "google_cloudbuildv2_connection" "vcs_connection" {
88+
project = var.project_id
89+
name = "builder-gh-${random_id.resources_random_id.dec}-${var.project_id}"
90+
location = local.location
91+
92+
github_config {
93+
app_installation_id = local.github_app_installation_id
94+
authorizer_credential {
95+
oauth_token_secret_version = google_secret_manager_secret_version.github_token_secret_version.name
96+
}
97+
}
98+
}
99+
100+
// Create the repository connection.
101+
resource "google_cloudbuildv2_repository" "repository_connection" {
102+
project = var.project_id
103+
name = local.gh_name
104+
location = local.location
105+
106+
parent_connection = google_cloudbuildv2_connection.vcs_connection.name
107+
remote_uri = local.repoURL
108+
}
109+
110+
# Bootstrap GitHub with Dockerfile
111+
module "bootstrap_github_repo" {
112+
source = "terraform-google-modules/gcloud/google"
113+
version = "~> 3.1"
114+
upgrade = false
115+
116+
create_cmd_entrypoint = "${path.module}/scripts/push-to-repo.sh"
117+
create_cmd_body = "${var.github_pat} ${var.repository_uri} ${path.module}/Dockerfile"
118+
}

0 commit comments

Comments
 (0)