diff --git a/readme/deploy.md b/readme/deploy.md new file mode 100644 index 0000000..0fc0931 --- /dev/null +++ b/readme/deploy.md @@ -0,0 +1,11 @@ +# デプロイの方法について + +## 環境について + +* terraform +* google cloud + * Cloud Run Job + + + + diff --git a/terraform/af.tf b/terraform/af.tf new file mode 100644 index 0000000..fd4fcf8 --- /dev/null +++ b/terraform/af.tf @@ -0,0 +1,5 @@ +resource "google_artifact_registry_repository" "repo" { + location = var.region + repository_id = "cicd-repo-${var.env_name}" + format = "DOCKER" +} \ No newline at end of file diff --git a/terraform/platform.tf b/terraform/platform.tf new file mode 100644 index 0000000..17fcd21 --- /dev/null +++ b/terraform/platform.tf @@ -0,0 +1,10 @@ +# Google CloudのAPIを有効化 + +resource "google_project_service" "services" { + for_each = toset([ + "run.googleapis.com", + "artifactregistry.googleapis.com", + "cloudbuild.googleapis.com", + ]) + service = each.key +} \ No newline at end of file diff --git a/terraform/provider.tf b/terraform/provider.tf new file mode 100644 index 0000000..fdeb7be --- /dev/null +++ b/terraform/provider.tf @@ -0,0 +1,5 @@ +# Google Providerの設定 +provider "google" { + project = var.project_id + region = var.region +} \ No newline at end of file diff --git a/terraform/run_job.tf b/terraform/run_job.tf new file mode 100644 index 0000000..9d5b1d1 --- /dev/null +++ b/terraform/run_job.tf @@ -0,0 +1,30 @@ +# Cloud Run Jobのリソース +resource "google_cloud_run_job" "job" { + name = "${var.job_name}-${var.env_name}-job" + location = var.region + + # サービスアカウントを指定 + service_account = google_service_account.job_sa.email + + template { + template { + containers { + image = var.container_image + + resources { + limits = { + cpu = var.cpu_limit + memory = var.memory_limit + } + } + + # env { + # 必要に応じ環境変数を設定 + # name = "ENV_VAR_NAME" + # } + } + timeout_seconds = var.timeout_seconds + } + } +} + diff --git a/terraform/sa.tf b/terraform/sa.tf new file mode 100644 index 0000000..798cc12 --- /dev/null +++ b/terraform/sa.tf @@ -0,0 +1,14 @@ +resource "google_service_account" "job_sa" { + account_id = "sa-${var.job_name}-${var.env_name}" + display_name = "Cloud Run Job Service Account for ${var.job_name} in ${var.env_name} environment" + description = "Cloud Run Job Service Account for ${var.job_name} in ${var.env_name} environment" + project = var.project_id +} + +# IAM role assignment +# Cloud Run Job実行に必要な権限を付与 +resource "google_project_iam_member" "run_job_invoker" { + project = var.project_id + role = "roles/run.invoker" + member = "serviceAccount:${google_service_account.job_sa.email}" +} \ No newline at end of file diff --git a/terraform/sample.tfvars b/terraform/sample.tfvars new file mode 100644 index 0000000..dd838f6 --- /dev/null +++ b/terraform/sample.tfvars @@ -0,0 +1,10 @@ +project_id = "プロジェクトIDを指定してください" +region = "asia-northeast1" +env_name = "dev" + +job_name = "ジョブ名を指定してください" +# コンテナイメージ(CI/CDから渡される想定) + +cpu_limit = "1" +memory_limit = "512Mi" +timeout_seconds = 900 diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..26937d7 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,53 @@ +# GCPプロジェクトIDとリージョン、環境名、ジョブ名の変数定義 +variable "project_id" { + description = "The ID of the GCP project to deploy resources into." + type = string +} +variable "region" { + description = "The GCP region to deploy resources into." + type = string + default = "asia-northeast1" # 東京 +} + +variable "env_name" { + description = "The environment name for the deployment." + type = string + default = "dev" + validation { + condition = contains(["dev", "staging", "prd"], var.env_name) + error_message = "env_name must be one of: dev, staging, prd." + } +} + +variable "job_name" { + description = "The name of the Cloud Run Job." + type = string + default = "get-news-ai" +} + + +# コンテナイメージの変数定義(CI/CDから渡される想定) +variable "container_image" { + description = "The container image for the Cloud Run Job." + type = string +} + + +# Cloud Run Jobの設定変数 +variable "cpu_limit" { + description = "The CPU limit for the Cloud Run Job container." + type = string + default = "1" +} + +variable "memory_limit" { + description = "The memory limit for the Cloud Run Job container." + type = string + default = "512Mi" +} + +variable "timeout_seconds" { + description = "The timeout for the Cloud Run Job." + type = number + default = 300 +} \ No newline at end of file