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 40 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
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"]
25 changes: 25 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Overview

This example demonstrates the simplest usage of the [tf_cloudbuild_builder](../../modules/tf_cloudbuild_builder/) module with a Repositories V2 GitHub repo.

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

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| cloudbuildv2\_repository\_id | Cloudbuild 2nd gen repository ID. Format: 'projects/{{project}}/locations/{{location}}/connections/{{parent\_connection}}/repositories/{{name}}'. Must be defined if repository type is `CLOUDBUILD_V2_REPOSITORY`. | `string` | n/a | yes |
| github\_pat | GitHub personal access token. | `string` | n/a | yes |
| project\_id | n/a | `string` | `"test-builder-workflow-4"` | no |
| repository\_uri | The URI of the repo 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 |
| project\_id | n/a |
| 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"
]
}
44 changes: 44 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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 "cloudbuilder" {
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
version = "~> 8.0"

project_id = module.enabled_google_apis.project_id
dockerfile_repo_id = var.cloudbuildv2_repository_id
dockerfile_repo_type = "GITHUB"
use_cloudbuildv2_repository = true
trigger_location = "us-central1"
gar_repo_location = "us-central1"
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
}

# 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"
}
39 changes: 39 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/outputs.tf
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
*
* 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.
*/

output "artifact_repo" {
description = "GAR Repo created to store TF Cloud Builder images"
value = module.cloudbuilder.artifact_repo
}

output "workflow_id" {
description = "Workflow ID for triggering new TF Builder build"
value = module.cloudbuilder.workflow_id
}

output "scheduler_id" {
description = "Scheduler ID for periodically triggering TF Builder build Workflow"
value = module.cloudbuilder.scheduler_id
}

output "cloudbuild_trigger_id" {
description = "Trigger used for building new TF Builder"
value = module.cloudbuilder.cloudbuild_trigger_id
}

output "project_id" {
value = var.project_id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# 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.


set -ex

if [ "$#" -lt 3 ]; then
>&2 echo "Not all expected arguments set."
exit 1
fi

GITHUB_TOKEN=$1
REPO_URL=$2
DOCKERFILE_PATH=$3

# extract portion after https:// from URL
IFS="/"; mapfile -t -d / URL_PARTS < <(printf "%s" "$REPO_URL")
# construct the new authenticated URL
AUTH_REPO_URL="https://${GITHUB_TOKEN}:@${URL_PARTS[2]}/${URL_PARTS[3]}/${URL_PARTS[4]}"

# create temp dir, cleanup at exit
tmp_dir=$(mktemp -d)
# # shellcheck disable=SC2064
# trap "rm -rf $tmp_dir" EXIT
git clone "${AUTH_REPO_URL}" "${tmp_dir}"
cp "${DOCKERFILE_PATH}" "${tmp_dir}"
pushd "${tmp_dir}"
git config credential.helper gcloud.sh
git config init.defaultBranch main
git config user.email "[email protected]"
git config user.name "TF Robot"
git checkout main || git checkout -b main
git add Dockerfile
git commit -m "init tf dockerfile"
git push origin main -f
35 changes: 35 additions & 0 deletions examples/tf_cloudbuild_builder_simple_github/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.
*/

variable "project_id" {
default = "test-builder-workflow-4"
}

variable "cloudbuildv2_repository_id" {
description = "Cloudbuild 2nd gen repository ID. Format: 'projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories/{{name}}'. Must be defined if repository type is `CLOUDBUILD_V2_REPOSITORY`."
type = string
}

variable "github_pat" {
description = "GitHub personal access token."
type = string
sensitive = true
}

variable "repository_uri" {
description = "The URI of the repo where the Terraform configs are stored."
type = string
}
Loading