Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules)!: enable cloudbuildv2 repository support on tf_cloudbuild_builder and tf_cloudbuild_workspace #299

Merged
merged 52 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
eabc5bf
first commit
caetano-colin Jul 3, 2024
c1e39cb
more info on variables
caetano-colin Jul 4, 2024
5a51af2
update modules
caetano-colin Jul 5, 2024
ceb06be
update documentation
daniel-cit Jul 5, 2024
152dc87
add fixture for cloudbuild workspace for github
daniel-cit Jul 10, 2024
fbb8c3b
update README.md adding information about github connections
caetano-colin Jul 12, 2024
e185109
update github workspace example
daniel-cit Jul 15, 2024
92bdda7
update documentation
daniel-cit Jul 15, 2024
cc0c0f5
Merge branch 'master' into dev
daniel-cit Jul 16, 2024
40506de
fix module source in examples for IM workspace
daniel-cit Jul 16, 2024
5653993
fix shellcheck lint
daniel-cit Jul 16, 2024
8b32895
add integration test for cloudbuild workspace using github
daniel-cit Jul 18, 2024
d46b19f
Merge branch 'master' into dev
daniel-cit Jul 18, 2024
58654b9
revert change in IM examples
daniel-cit Jul 18, 2024
6390a35
add build steps to test tfworkspace for github
daniel-cit Jul 18, 2024
022763b
use different backend prefix
daniel-cit Jul 18, 2024
a227497
use distinct network name for the tf workspace github
daniel-cit Jul 19, 2024
17be087
add case for build failure in testes
daniel-cit Jul 22, 2024
0328f4c
use new prefix in the backend
daniel-cit Jul 22, 2024
f66ed97
Merge branch 'master' into dev
daniel-cit Jul 22, 2024
09931b6
update github-go version
daniel-cit Jul 22, 2024
24dd93a
add example for cloud build with gitlab
daniel-cit Jul 25, 2024
bdac16a
make enabling sourcerepo.googleapis.com conditional
daniel-cit Jul 31, 2024
3062dff
use Source Repositories v2 on tf_cloudbuild_builder module
daniel-cit Jul 31, 2024
9dd944a
fix lint issues
daniel-cit Jul 31, 2024
fb07686
fix lint issue
daniel-cit Jul 31, 2024
8a2cbf5
use distinct names for resources in the examples
daniel-cit Aug 5, 2024
c11095f
use distinct names for trigger name in the examples
daniel-cit Aug 5, 2024
77bfd2f
use distinct names for resources in the workspace examples
daniel-cit Aug 6, 2024
4d5ae9e
use distinct repository names in the workspace examples
daniel-cit Aug 6, 2024
9e66641
fix tests
daniel-cit Aug 6, 2024
25bc7fa
log build logs in case of failure
daniel-cit Aug 6, 2024
c706c73
fix builds log command in tests
daniel-cit Aug 6, 2024
0f341f6
serialize execution of workspace integration tests
daniel-cit Aug 6, 2024
aa3ed41
make subnetworks distinct
daniel-cit Aug 6, 2024
cbbb589
create resources in a single region in the builder example
daniel-cit Aug 7, 2024
0cf9a63
make location required
daniel-cit Aug 7, 2024
08688b8
run tests simultaneously
daniel-cit Aug 7, 2024
ae539ac
change test order to prevent collision
daniel-cit Aug 7, 2024
77845b3
wait build to reinstate ci-integration account as project creator
daniel-cit Aug 7, 2024
e668ca0
move location to a variable in the tests
daniel-cit Aug 7, 2024
4c627ee
merge fixture and example for the usage of Cloud Build repositories 2…
daniel-cit Aug 8, 2024
e41d2b2
Merge branch 'master' into dev
daniel-cit Aug 8, 2024
a2de7b5
add missing output
daniel-cit Aug 8, 2024
cfb6fa8
fix lint issue
daniel-cit Aug 8, 2024
c07457c
add upgrading to v9.0 documentation
daniel-cit Aug 14, 2024
a4094d4
fix variable usage for Cloud Build repositories (2nd Gen)
daniel-cit Aug 14, 2024
a462303
Merge branch 'terraform-google-modules:master' into dev
daniel-cit Aug 14, 2024
bc3a7bc
Merge remote-tracking branch 'origin/dev' into dev
daniel-cit Aug 14, 2024
695345c
fix lint issue
daniel-cit Aug 14, 2024
51c2c84
set trigger_location on tf_cloudbuild_workspace_simple example
daniel-cit Aug 14, 2024
01f78c5
remove default value for trigger_location and gar_repo_location
daniel-cit Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ steps:

- id: apply-tfsource
waitFor:
- create-all
- destroy-simple-folder
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildSourceSimple --stage apply --verbose']
- id: verify-tfsource
Expand Down Expand Up @@ -119,6 +119,44 @@ steps:
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilder --stage teardown --verbose']

- id: apply-tfbuilder-github
waitFor:
- create-all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage apply --verbose']
secretEnv: ['IM_GITHUB_PAT']
- id: verify-tfbuilder-github
waitFor:
- apply-tfbuilder-github
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage verify --verbose']
secretEnv: ['IM_GITHUB_PAT']
- id: teardown-tfbuilder-github
waitFor:
- verify-tfbuilder-github
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitHub --stage teardown --verbose']
secretEnv: ['IM_GITHUB_PAT']

- id: apply-tfbuilder-gitlab
waitFor:
- create-all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage apply --verbose']
secretEnv: ['IM_GITLAB_PAT']
- id: verify-tfbuilder-gitlab
waitFor:
- apply-tfbuilder-gitlab
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage verify --verbose']
secretEnv: ['IM_GITLAB_PAT']
- id: teardown-tfbuilder-gitlab
waitFor:
- verify-tfbuilder-gitlab
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestTFCloudBuildBuilderGitLab --stage teardown --verbose']
secretEnv: ['IM_GITLAB_PAT']

- id: apply-tfworkspace
waitFor:
- create-all
Expand Down Expand Up @@ -173,6 +211,44 @@ steps:
args: ['/bin/bash', '-c', 'cft test run TestIMCloudBuildWorkspaceGitLab --stage teardown --verbose']
secretEnv: ['IM_GITLAB_PAT']

- id: apply-tfworkspace-github
waitFor:
- create-all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage apply --verbose']
secretEnv: ['IM_GITHUB_PAT']
- id: verify-tfworkspace-github
waitFor:
- apply-tfworkspace-github
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage verify --verbose']
secretEnv: ['IM_GITHUB_PAT']
- id: teardown-tfworkspace-github
waitFor:
- verify-tfworkspace-github
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitHub --stage teardown --verbose']
secretEnv: ['IM_GITHUB_PAT']

- id: apply-tfworkspace-gitlab
waitFor:
- create-all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage apply --verbose']
secretEnv: ['IM_GITLAB_PAT']
- id: verify-tfworkspace-gitlab
waitFor:
- apply-tfworkspace-gitlab
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage verify --verbose']
secretEnv: ['IM_GITLAB_PAT']
- id: teardown-tfworkspace-gitlab
waitFor:
- verify-tfworkspace-gitlab
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestCloudBuildWorkspaceSimpleGitLab --stage teardown --verbose']
secretEnv: ['IM_GITLAB_PAT']

availableSecrets:
secretManager:
- versionName: $_IM_GITHUB_PAT_SECRET_ID/versions/latest
Expand Down
42 changes: 42 additions & 0 deletions docs/upgrading_to_v9.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Upgrading to v9.0

The v9.0 release of *bootstrap* is a backwards incompatible release.

Some variables default values were replaced to align with the restriction that Cloud Build Repositories (2nd Gen) cannot be created in multi-regions or in the `global` region.

You need to update your configurations if you used the default values to prevent resources to be recreated.

## Default value for variable `trigger_location` in module `tf_cloudbuild_workspace` has changed

To preserve the resources created before. include the input `trigger_location` with the previous default value in the module call

```diff
module "tf_workspace" {
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_workspace"
- version = "~> 8.0"
+ version = "~> 9.0"

+ trigger_location = "global"
```

## Default value for variables `trigger_location` and `gar_repo_location` in module `tf_cloudbuild_builde` have changed

To preserve the resources created before, include the inputs `trigger_location` and `gar_repo_location` with the previous default values in the module call

```diff
module "cloudbuilder" {
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
- version = "~> 8.0"
+ version = "~> 9.0"

+ trigger_location = "global"
+ gar_repo_location = "us"
```

An apply after adding the two inputs will still have an *in-place update* in the `google_workflows_workflow` created by the module.

The endpoint that is used to trigger a build was replaced with a new one that allows a location to be provided.

```
# module.cloudbuilder.google_workflows_workflow.builder will be updated in-place
```
2 changes: 2 additions & 0 deletions examples/tf_cloudbuild_builder_simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module "cloudbuilder" {

project_id = module.enabled_google_apis.project_id
dockerfile_repo_uri = google_sourcerepo_repository.builder_dockerfile_repo.url
trigger_location = "us-central1"
gar_repo_location = "us-central1"
# allow logs bucket to be destroyed
cb_logs_bucket_force_destroy = true
}
Expand Down
39 changes: 39 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/cloud-builders/gcloud-slim

# Use ARG so that values can be overriden by user/cloudbuild
ARG TERRAFORM_VERSION=1.1.0

ENV ENV_TERRAFORM_VERSION=$TERRAFORM_VERSION

RUN apt-get update && \
/builder/google-cloud-sdk/bin/gcloud -q components install alpha beta terraform-tools && \
apt-get -y install curl jq unzip git ca-certificates gnupg && \
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip --output terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip && \
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_SHA256SUMS.sig --output terraform_SHA256SUMS.sig && \
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_SHA256SUMS --output terraform_SHA256SUMS && \
curl https://keybase.io/hashicorp/pgp_keys.asc --output pgp_keys.asc && \
gpg --import pgp_keys.asc && \
gpg --verify terraform_SHA256SUMS.sig terraform_SHA256SUMS && \
grep terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip terraform_SHA256SUMS | shasum --algorithm 256 --check && \
unzip terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip -d /builder/terraform && \
rm -f terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip terraform_SHA256SUMS && \
apt-get --purge -y autoremove && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

ENV PATH=/builder/terraform/:$PATH
ENTRYPOINT ["terraform"]
34 changes: 34 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Overview

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.

For GitHub connections you will need:

- Install the [Cloud Build App](https://github.com/apps/google-cloud-build) on Github.
- 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`).

For more information on this topic refer to the Cloud Build repositories (2nd gen) documentation for
[Connect to a GitHub repository](https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen).

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| github\_pat | GitHub personal access token. | `string` | n/a | yes |
| project\_id | The ID of the project in which to provision resources. | `string` | n/a | yes |
| repository\_uri | The URI of the GitHub repository where the Terraform configs are stored. | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| artifact\_repo | GAR Repo created to store TF Cloud Builder images |
| cloudbuild\_trigger\_id | Trigger used for building new TF Builder |
| location | The location in which the resources were provisioned |
| project\_id | The ID of the project in which the resources were provisioned |
| repository\_id | ID of the Cloud Build repositories (2nd gen) repository |
| scheduler\_id | Scheduler ID for periodically triggering TF Builder build Workflow |
| workflow\_id | Workflow ID for triggering new TF Builder build |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
32 changes: 32 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/apis.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "enabled_google_apis" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 15.0"

project_id = var.project_id
disable_services_on_destroy = false

activate_apis = [
"iam.googleapis.com",
"compute.googleapis.com",
"workflows.googleapis.com",
"artifactregistry.googleapis.com",
"cloudbuild.googleapis.com",
"cloudscheduler.googleapis.com"
]
}
118 changes: 118 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

locals {
// Found in the URL of your Cloud Build GitHub app configuration settings
// https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#connecting_a_github_host_programmatically
github_app_installation_id = "47590865"

# GitHub repo url of form "github.com/owner/name"
repoURL = endswith(var.repository_uri, ".git") ? var.repository_uri : "${var.repository_uri}.git"
repoURLWithoutSuffix = trimsuffix(local.repoURL, ".git")
gh_repo_url_split = split("/", local.repoURLWithoutSuffix)
gh_name = local.gh_repo_url_split[length(local.gh_repo_url_split) - 1]

location = "us-central1"
}

data "google_project" "project" {
project_id = var.project_id
}

// Added to various IDs to prevent potential conflicts for deployments targeting the same repository.
resource "random_id" "resources_random_id" {
byte_length = 4
}

module "cloudbuilder" {
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
version = "~> 8.0"

project_id = module.enabled_google_apis.project_id
dockerfile_repo_uri = google_cloudbuildv2_repository.repository_connection.id
dockerfile_repo_type = "GITHUB"
use_cloudbuildv2_repository = true
trigger_location = local.location
gar_repo_location = local.location
bucket_name = "tf-cloudbuilder-build-logs-${var.project_id}-gh"
gar_repo_name = "tf-runners-gh"
workflow_name = "terraform-runner-workflow-gh"
trigger_name = "tf-cloud-builder-build-gh"

# allow logs bucket to be destroyed
cb_logs_bucket_force_destroy = true
}

// Create a secret containing the personal access token and grant permissions to the Service Agent.
resource "google_secret_manager_secret" "github_token_secret" {
project = var.project_id
secret_id = "builder-gh-${random_id.resources_random_id.dec}-${local.gh_name}"

labels = {
label = "builder-gh-${random_id.resources_random_id.dec}"
}

replication {
auto {}
}
}

// Personal access token from VCS.
resource "google_secret_manager_secret_version" "github_token_secret_version" {
secret = google_secret_manager_secret.github_token_secret.id
secret_data = var.github_pat
}

resource "google_secret_manager_secret_iam_member" "github_token_iam_member" {
project = var.project_id
secret_id = google_secret_manager_secret.github_token_secret.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
}

// See https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen
resource "google_cloudbuildv2_connection" "vcs_connection" {
project = var.project_id
name = "builder-gh-${random_id.resources_random_id.dec}-${var.project_id}"
location = local.location

github_config {
app_installation_id = local.github_app_installation_id
authorizer_credential {
oauth_token_secret_version = google_secret_manager_secret_version.github_token_secret_version.name
}
}
}

// Create the repository connection.
resource "google_cloudbuildv2_repository" "repository_connection" {
project = var.project_id
name = local.gh_name
location = local.location

parent_connection = google_cloudbuildv2_connection.vcs_connection.name
remote_uri = local.repoURL
}

# Bootstrap GitHub with Dockerfile
module "bootstrap_github_repo" {
source = "terraform-google-modules/gcloud/google"
version = "~> 3.1"
upgrade = false

create_cmd_entrypoint = "${path.module}/scripts/push-to-repo.sh"
create_cmd_body = "${var.github_pat} ${var.repository_uri} ${path.module}/Dockerfile"
}
Loading