diff --git a/.github/workflows/deployment.yaml b/.github/workflows/deployment.yaml index ce71136..5e6bb34 100644 --- a/.github/workflows/deployment.yaml +++ b/.github/workflows/deployment.yaml @@ -19,9 +19,7 @@ jobs: deploy: runs-on: ubuntu-latest needs: [test] - defaults: - run: - working-directory: ${{ github.ref == 'refs/heads/main' && './terraform/prod' || './terraform/dev' }} + steps: - uses: actions/checkout@v6 - name: Google Cloud Auth @@ -32,19 +30,15 @@ jobs: - uses: hashicorp/setup-terraform@v4 - - name: Terraform fmt - id: fmt - run: terraform fmt -check - - - name: Terraform Init - id: init - run: terraform init - - - name: Terraform Validate - id: validate - run: terraform validate -no-color - - name: Terraform Apply id: apply run: | - terraform apply -auto-approve + if [ "${GITHUB_REF}" = "refs/heads/main" ]; then + ENV=prod + elif [ "${GITHUB_REF}" = "refs/heads/development" ]; then + ENV=dev + else + echo "Unsupported branch for deployment: ${GITHUB_REF}" + exit 1 + fi + make tf_apply ENV="$ENV" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8596808 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: * +ENV ?= dev + +tf_plan: + cd terraform/ && terraform init -reconfigure -backend-config=backend-$(ENV).hcl -upgrade && terraform fmt -check && terraform validate && terraform plan --var="environment=$(ENV)" + +tf_apply: + cd terraform/ && terraform init -reconfigure -backend-config=backend-$(ENV).hcl && terraform apply -auto-approve --var="environment=$(ENV)" diff --git a/src/Dockerfile b/src/Dockerfile index 3edf02f..2bfbc4d 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -14,4 +14,4 @@ COPY --chown=node:node . . ENV PORT=8080 -CMD ["npx", "functions-framework", "--target=app"] \ No newline at end of file +CMD ["npx", "functions-framework", "--target=app"] diff --git a/terraform/backend-dev.hcl b/terraform/backend-dev.hcl new file mode 100644 index 0000000..ffdab14 --- /dev/null +++ b/terraform/backend-dev.hcl @@ -0,0 +1,2 @@ +bucket = "tfstate-httparchive" +prefix = "tech-report-apis/dev" diff --git a/terraform/backend-prod.hcl b/terraform/backend-prod.hcl new file mode 100644 index 0000000..0f243ae --- /dev/null +++ b/terraform/backend-prod.hcl @@ -0,0 +1,2 @@ +bucket = "tfstate-httparchive" +prefix = "tech-report-apis/prod" diff --git a/terraform/modules/cdn-glb/cloud_armor.tf b/terraform/cdn-glb/cloud_armor.tf similarity index 100% rename from terraform/modules/cdn-glb/cloud_armor.tf rename to terraform/cdn-glb/cloud_armor.tf diff --git a/terraform/modules/cdn-glb/main.tf b/terraform/cdn-glb/main.tf similarity index 100% rename from terraform/modules/cdn-glb/main.tf rename to terraform/cdn-glb/main.tf diff --git a/terraform/modules/cdn-glb/outputs.tf b/terraform/cdn-glb/outputs.tf similarity index 100% rename from terraform/modules/cdn-glb/outputs.tf rename to terraform/cdn-glb/outputs.tf diff --git a/terraform/modules/cdn-glb/variables.tf b/terraform/cdn-glb/variables.tf similarity index 100% rename from terraform/modules/cdn-glb/variables.tf rename to terraform/cdn-glb/variables.tf diff --git a/terraform/dev/main.tf b/terraform/dev/main.tf deleted file mode 100644 index 73c7ab0..0000000 --- a/terraform/dev/main.tf +++ /dev/null @@ -1,36 +0,0 @@ -terraform { - required_version = ">=1.11.0" - - backend "gcs" { - bucket = "tfstate-httparchive" - prefix = "tech-report-apis/dev" - } - required_providers { - docker = { - source = "kreuzwerker/docker" - version = ">=3.6.2" - } - google = { - source = "hashicorp/google" - version = ">=7.13.0" - } - } -} - -provider "google" { - project = var.project - region = var.region -} - -module "endpoints" { - source = "./../modules/run-service" - project = var.project - environment = var.environment - source_directory = "../../src" - service_name = "report-api" - region = var.region - environment_variables = { - "PROJECT" = var.project - "DATABASE" = var.project_database - } -} diff --git a/terraform/dev/variables.tf b/terraform/dev/variables.tf deleted file mode 100644 index e864e10..0000000 --- a/terraform/dev/variables.tf +++ /dev/null @@ -1,19 +0,0 @@ -variable "project" { - description = "The project name" - type = string - default = "httparchive" -} -variable "region" { - type = string - default = "us-central1" -} -variable "environment" { - description = "The environment name" - type = string - default = "dev" -} -variable "project_database" { - type = string - description = "The database name" - default = "tech-report-api-prod" // TODO: Update this to the DEV database name -} diff --git a/terraform/prod/main.tf b/terraform/main.tf similarity index 63% rename from terraform/prod/main.tf rename to terraform/main.tf index 05b8f55..81afba9 100644 --- a/terraform/prod/main.tf +++ b/terraform/main.tf @@ -1,15 +1,16 @@ terraform { - backend "gcs" { - bucket = "tfstate-httparchive" - prefix = "tech-report-apis/prod" - } + required_version = ">=1.11.0" + + backend "gcs" {} + required_providers { docker = { source = "kreuzwerker/docker" - version = "3.6.2" + version = ">=3.6.2" } google = { - source = "hashicorp/google" + source = "hashicorp/google" + version = ">=7.13.0" } } } @@ -20,21 +21,23 @@ provider "google" { } module "endpoints" { - source = "./../modules/run-service" + source = "./run-service" project = var.project environment = var.environment - source_directory = "../../src" + source_directory = "../src" service_name = "report-api" region = var.region - min_instances = 1 + min_instances = var.environment == "prod" ? 1 : 0 environment_variables = { "PROJECT" = var.project - "DATABASE" = var.project_database + "DATABASE" = "${var.project_database}prod" // TODO: Update this to use ${var.environment} } } module "cdn_glb" { - source = "./../modules/cdn-glb" + count = var.environment == "prod" ? 1 : 0 + + source = "./cdn-glb" project = var.project region = var.region @@ -45,9 +48,14 @@ module "cdn_glb" { load_balancer_name = "httparchive-load-balancer" name_prefix = var.name_prefix - neg_name = "report-api-prod" + neg_name = "report-api-${var.environment}" backend_service_name = "report-api" ssl_cert_name = var.ssl_cert_name https_proxy_name = "httparchive-load-balancer-target-proxy-2" https_forwarding_rule_name = "httparchive-load-balancer-forwarding-rule-2" } + +moved { + from = module.cdn_glb + to = module.cdn_glb[0] +} diff --git a/terraform/modules/run-service/main.tf b/terraform/run-service/main.tf similarity index 100% rename from terraform/modules/run-service/main.tf rename to terraform/run-service/main.tf diff --git a/terraform/modules/run-service/outputs.tf b/terraform/run-service/outputs.tf similarity index 100% rename from terraform/modules/run-service/outputs.tf rename to terraform/run-service/outputs.tf diff --git a/terraform/modules/run-service/variables.tf b/terraform/run-service/variables.tf similarity index 100% rename from terraform/modules/run-service/variables.tf rename to terraform/run-service/variables.tf diff --git a/terraform/prod/variables.tf b/terraform/variables.tf similarity index 90% rename from terraform/prod/variables.tf rename to terraform/variables.tf index f54a5bc..1d08f74 100644 --- a/terraform/prod/variables.tf +++ b/terraform/variables.tf @@ -10,12 +10,11 @@ variable "region" { variable "environment" { description = "The environment name" type = string - default = "prod" } variable "project_database" { type = string description = "The database name" - default = "tech-report-api-prod" + default = "tech-report-api-" } variable "name_prefix" {