@@ -21,50 +21,24 @@ module "artifactregistry" {
21
21
# Cloud Build
22
22
23
23
locals {
24
- artifact_repo = " ${ var . location } -docker.pkg.dev/${ var . project_id } /${ google_artifact_registry_repository . docker . name } "
25
- conformance_gcp_docker_image = " ${ local . artifact_repo } /conformance-gcp"
24
+ cloudbuild_service_account = " cloudbuild-${ var . env } -sa@${ var . project_id } .iam.gserviceaccount.com"
25
+ artifact_repo = " ${ var . location } -docker.pkg.dev/${ var . project_id } /${ module . artifactregistry . docker . name } "
26
+ conformance_gcp_docker_image = " ${ local . artifact_repo } /conformance-gcp"
26
27
}
27
28
28
29
resource "google_project_service" "cloudbuild_api" {
29
30
service = " cloudbuild.googleapis.com"
30
31
disable_on_destroy = false
31
32
}
32
33
33
- resource "google_service_account" "cloudbuild_service_account" {
34
- account_id = " cloudbuild-${ var . env } -sa"
35
- display_name = " Service Account for Cloud Build (${ var . env } )"
36
- }
37
-
38
- resource "google_project_iam_member" "logging_log_writer" {
39
- project = var. project_id
40
- role = " roles/logging.logWriter"
41
- member = " serviceAccount:${ google_service_account . cloudbuild_service_account . email } "
42
- }
43
-
44
- resource "google_artifact_registry_repository_iam_member" "artifactregistry_writer" {
45
- project = google_artifact_registry_repository. docker . project
46
- location = google_artifact_registry_repository. docker . location
47
- repository = google_artifact_registry_repository. docker . name
48
- role = " roles/artifactregistry.writer"
49
- member = " serviceAccount:${ google_service_account . cloudbuild_service_account . email } "
50
- }
51
-
52
- # TODO: Use google_cloud_run_service_iam_member to limit the service scope.
53
- resource "google_project_iam_member" "run_developer" {
54
- project = var. project_id
55
- role = " roles/run.developer"
56
- member = " serviceAccount:${ google_service_account . cloudbuild_service_account . email } "
57
- }
58
-
59
- resource "google_project_iam_member" "iam_service_account_user" {
60
- project = var. project_id
61
- role = " roles/iam.serviceAccountUser"
62
- member = " serviceAccount:${ google_service_account . cloudbuild_service_account . email } "
34
+ resource "google_project_service" "serviceusage_api" {
35
+ service = " serviceusage.googleapis.com"
36
+ disable_on_destroy = false
63
37
}
64
38
65
39
resource "google_cloudbuild_trigger" "build_trigger" {
66
40
name = " build-docker-${ var . docker_env } "
67
- service_account = google_service_account . cloudbuild_service_account . id
41
+ service_account = " projects/ ${ var . project_id } /serviceAccounts/ ${ local . cloudbuild_service_account } "
68
42
location = var. location
69
43
70
44
github {
@@ -76,8 +50,22 @@ resource "google_cloudbuild_trigger" "build_trigger" {
76
50
}
77
51
78
52
build {
79
- # # TODO: Destroy any pre-existing deployment/live/gcp/ci environment.
53
+ # # Destroy any pre-existing deployment/live/gcp/ci environment.
80
54
# # This might happen if a previous cloud build failed for some reason.
55
+ step {
56
+ id = " preclean_env"
57
+ name = " alpine/terragrunt"
58
+ script = << EOT
59
+ terragrunt --terragrunt-non-interactive --terragrunt-no-color destroy -auto-approve -no-color 2>&1
60
+ EOT
61
+ dir = " deployment/live/gcp/ci"
62
+ env = [
63
+ " GOOGLE_PROJECT=${ var . project_id } " ,
64
+ " TF_IN_AUTOMATION=1" ,
65
+ " TF_INPUT=false" ,
66
+ " TF_VAR_project_id=${ var . project_id } "
67
+ ]
68
+ }
81
69
82
70
# # Build the SCTFE GCP Docker image.
83
71
# # This will be used by the building the conformance Docker image which includes
@@ -119,25 +107,67 @@ resource "google_cloudbuild_trigger" "build_trigger" {
119
107
wait_for = [" docker_build_conformance_gcp" ]
120
108
}
121
109
122
- # # Deploy container image to Cloud Run.
123
- # # TODO: Remove this as the `terragrunt apply` will bring up the Cloud Run.
110
+ # # Apply the deployment/live/gcp/ci terragrunt config.
111
+ # # This will bring up the conformance infrastructure, including a service
112
+ # # running the conformance server docker image built above.
124
113
step {
125
- id = " cloud_run_deploy"
126
- name = " gcr.io/google.com/cloudsdktool/cloud-sdk"
127
- entrypoint = " gcloud"
128
- args = [
129
- " run" ,
130
- " deploy" ,
131
- " ${ var . docker_env } -static-ct" ,
132
- " --image" ,
133
- " ${ local . conformance_gcp_docker_image } :$SHORT_SHA" ,
134
- " --region" ,
135
- var . location
114
+ id = " terraform_apply_conformance_ci"
115
+ name = " alpine/terragrunt"
116
+ script = << EOT
117
+ terragrunt --terragrunt-non-interactive --terragrunt-no-color apply -auto-approve -no-color 2>&1
118
+ terragrunt --terragrunt-no-color output --raw conformance_url -no-color > /workspace/conformance_url
119
+ EOT
120
+ dir = " deployment/live/gcp/ci"
121
+ env = [
122
+ " GOOGLE_PROJECT=${ var . project_id } " ,
123
+ " TF_IN_AUTOMATION=1" ,
124
+ " TF_INPUT=false" ,
125
+ " TF_VAR_project_id=${ var . project_id } "
136
126
]
137
127
wait_for = [" docker_push_conformance_gcp" ]
138
128
}
139
129
140
- # # TODO: Apply the terragrunt configuration to create the CI environment.
130
+ # # Since the conformance infrastructure is not publicly accessible, we need to use
131
+ # # bearer tokens for the test to access them.
132
+ # # This step creates those, and stores them for later use.
133
+ step {
134
+ id = " bearer_token"
135
+ name = " gcr.io/cloud-builders/gcloud"
136
+ script = << EOT
137
+ curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/${ local . cloudbuild_service_account } /identity?audience=$(cat /workspace/conformance_url)" > /workspace/cb_identity
138
+ EOT
139
+ wait_for = [" terraform_apply_conformance_ci" ]
140
+ }
141
+
142
+ # # Test against the conformance server.
143
+ # # TODO: Replace this with CT Hammer when it is ready.
144
+ step {
145
+ id = " curl_test"
146
+ name = " curlimages/curl"
147
+ script = << EOT
148
+ curl -iX POST $(cat /workspace/conformance_url)/ci-conformance/ct/v1/add-pre-chain -H "Authorization: Bearer $(cat /workspace/cb_identity)"
149
+ EOT
150
+ wait_for = [" bearer_token" ]
151
+ }
152
+
153
+ # # Destroy the deployment/live/gcp/ci terragrunt config.
154
+ # # This will tear down the conformance infrastructure we brought up
155
+ # # above.
156
+ step {
157
+ id = " terraform_destroy_conformance_ci"
158
+ name = " alpine/terragrunt"
159
+ script = << EOT
160
+ terragrunt --terragrunt-non-interactive --terragrunt-no-color destroy -auto-approve -no-color 2>&1
161
+ EOT
162
+ dir = " deployment/live/gcp/ci"
163
+ env = [
164
+ " GOOGLE_PROJECT=${ var . project_id } " ,
165
+ " TF_IN_AUTOMATION=1" ,
166
+ " TF_INPUT=false" ,
167
+ " TF_VAR_project_id=${ var . project_id } "
168
+ ]
169
+ wait_for = [" curl_test" ]
170
+ }
141
171
142
172
options {
143
173
logging = " CLOUD_LOGGING_ONLY"
@@ -146,6 +176,6 @@ resource "google_cloudbuild_trigger" "build_trigger" {
146
176
}
147
177
148
178
depends_on = [
149
- google_artifact_registry_repository . docker
179
+ module . artifactregistry
150
180
]
151
181
}
0 commit comments