|
| 1 | +terraform { |
| 2 | + backend "gcs" {} |
| 3 | + |
| 4 | + required_providers { |
| 5 | + google = { |
| 6 | + source = "registry.terraform.io/hashicorp/google" |
| 7 | + version = "6.1.0" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +# Artifact Registry |
| 13 | + |
| 14 | +resource "google_project_service" "artifact_registry_api" { |
| 15 | + service = "artifactregistry.googleapis.com" |
| 16 | + disable_on_destroy = false |
| 17 | +} |
| 18 | + |
| 19 | +resource "google_artifact_registry_repository" "docker" { |
| 20 | + repository_id = "docker-${var.docker_env}" |
| 21 | + location = var.location |
| 22 | + description = "Static CT docker images" |
| 23 | + format = "DOCKER" |
| 24 | + depends_on = [ |
| 25 | + google_project_service.artifact_registry_api, |
| 26 | + ] |
| 27 | +} |
| 28 | + |
| 29 | +# Cloud Build |
| 30 | + |
| 31 | +locals { |
| 32 | + artifact_repo = "${var.location}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.docker.name}" |
| 33 | + conformance_gcp_docker_image = "${local.artifact_repo}/conformance-gcp" |
| 34 | +} |
| 35 | + |
| 36 | +resource "google_project_service" "cloudbuild_api" { |
| 37 | + service = "cloudbuild.googleapis.com" |
| 38 | + disable_on_destroy = false |
| 39 | +} |
| 40 | + |
| 41 | +resource "google_service_account" "cloudbuild_service_account" { |
| 42 | + account_id = "cloudbuild-${var.env}-sa" |
| 43 | + display_name = "Service Account for Cloud Build (${var.env})" |
| 44 | +} |
| 45 | + |
| 46 | +resource "google_project_iam_member" "logging_log_writer" { |
| 47 | + project = var.project_id |
| 48 | + role = "roles/logging.logWriter" |
| 49 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 50 | +} |
| 51 | + |
| 52 | +resource "google_artifact_registry_repository_iam_member" "artifactregistry_writer" { |
| 53 | + project = google_artifact_registry_repository.docker.project |
| 54 | + location = google_artifact_registry_repository.docker.location |
| 55 | + repository = google_artifact_registry_repository.docker.name |
| 56 | + role = "roles/artifactregistry.writer" |
| 57 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 58 | +} |
| 59 | + |
| 60 | +# TODO: Use google_cloud_run_service_iam_member to limit the service scope. |
| 61 | +resource "google_project_iam_member" "run_developer" { |
| 62 | + project = var.project_id |
| 63 | + role = "roles/run.developer" |
| 64 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 65 | +} |
| 66 | + |
| 67 | +resource "google_project_iam_member" "iam_service_account_user" { |
| 68 | + project = var.project_id |
| 69 | + role = "roles/iam.serviceAccountUser" |
| 70 | + member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}" |
| 71 | +} |
| 72 | + |
| 73 | +resource "google_cloudbuild_trigger" "build_trigger" { |
| 74 | + name = "build-docker-${var.docker_env}" |
| 75 | + service_account = google_service_account.cloudbuild_service_account.id |
| 76 | + location = var.location |
| 77 | + |
| 78 | + github { |
| 79 | + owner = var.github_owner |
| 80 | + name = "static-ct" |
| 81 | + push { |
| 82 | + branch = "^main$" |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + build { |
| 87 | + ## TODO: Destroy any pre-existing deployment/live/gcp/ci environment. |
| 88 | + ## This might happen if a previous cloud build failed for some reason. |
| 89 | + |
| 90 | + ## Build the SCTFE GCP Docker image. |
| 91 | + ## This will be used by the building the conformance Docker image which includes |
| 92 | + ## the test data. |
| 93 | + step { |
| 94 | + id = "docker_build_sctfe_gcp" |
| 95 | + name = "gcr.io/cloud-builders/docker" |
| 96 | + args = [ |
| 97 | + "build", |
| 98 | + "-t", "sctfe-gcp:$SHORT_SHA", |
| 99 | + "-t", "sctfe-gcp:latest", |
| 100 | + "-f", "./cmd/gcp/Dockerfile", |
| 101 | + "." |
| 102 | + ] |
| 103 | + } |
| 104 | + |
| 105 | + ## Build the SCTFE GCP Conformance Docker container image. |
| 106 | + step { |
| 107 | + id = "docker_build_conformance_gcp" |
| 108 | + name = "gcr.io/cloud-builders/docker" |
| 109 | + args = [ |
| 110 | + "build", |
| 111 | + "-t", "${local.conformance_gcp_docker_image}:$SHORT_SHA", |
| 112 | + "-t", "${local.conformance_gcp_docker_image}:latest", |
| 113 | + "-f", "./cmd/gcp/ci/Dockerfile", |
| 114 | + "." |
| 115 | + ] |
| 116 | + } |
| 117 | + |
| 118 | + ## Push the conformance Docker container image to Artifact Registry. |
| 119 | + step { |
| 120 | + id = "docker_push_conformance_gcp" |
| 121 | + name = "gcr.io/cloud-builders/docker" |
| 122 | + args = [ |
| 123 | + "push", |
| 124 | + "--all-tags", |
| 125 | + local.conformance_gcp_docker_image |
| 126 | + ] |
| 127 | + wait_for = ["docker_build_conformance_gcp"] |
| 128 | + } |
| 129 | + |
| 130 | + ## Deploy container image to Cloud Run. |
| 131 | + ## TODO: Remove this as the `terragrunt apply` will bring up the Cloud Run. |
| 132 | + step { |
| 133 | + id = "cloud_run_deploy" |
| 134 | + name = "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 135 | + entrypoint = "gcloud" |
| 136 | + args = [ |
| 137 | + "run", |
| 138 | + "deploy", |
| 139 | + "${var.docker_env}-static-ct", |
| 140 | + "--image", |
| 141 | + "${local.conformance_gcp_docker_image}:$SHORT_SHA", |
| 142 | + "--region", |
| 143 | + var.location |
| 144 | + ] |
| 145 | + wait_for = ["docker_push_conformance_gcp"] |
| 146 | + } |
| 147 | + |
| 148 | + ## TODO: Apply the terragrunt configuration to create the CI environment. |
| 149 | + |
| 150 | + options { |
| 151 | + logging = "CLOUD_LOGGING_ONLY" |
| 152 | + machine_type = "E2_HIGHCPU_8" |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + depends_on = [ |
| 157 | + google_artifact_registry_repository.docker |
| 158 | + ] |
| 159 | +} |
0 commit comments