Skip to content

Commit

Permalink
Merge pull request #4 from Suke-H/#3-google-cloudへのデプロイ
Browse files Browse the repository at this point in the history
#3 google cloudへのデプロイ
  • Loading branch information
Suke-H authored Dec 17, 2024
2 parents f527437 + 6cf2d59 commit a4fe59f
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Deploy to Cloud Run

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

permissions:
contents: 'read'
id-token: 'write'

env:
SERVICE_NAME: 'kiro-stage-editor'
REGION: 'asia-northeast1'

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.SA_EMAIL }}

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.PROJECT_ID }}
install_components: 'beta'

- name: Build Docker image
run: |
docker build --no-cache -t ${{ env.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ secrets.ARTIFACT_REGISTRY_REPO_NAME }}/${{ env.SERVICE_NAME }}:$GITHUB_SHA .
- name: Push Docker image
run: |
gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev
docker push ${{ env.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ secrets.ARTIFACT_REGISTRY_REPO_NAME }}/${{ env.SERVICE_NAME }}:$GITHUB_SHA
- name: Deploy to Cloud Run
run: |
gcloud run deploy ${{ env.SERVICE_NAME }} \
--image ${{ env.REGION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ secrets.ARTIFACT_REGISTRY_REPO_NAME }}/${{ env.SERVICE_NAME }}:$GITHUB_SHA \
--platform managed \
--region ${{ env.REGION }} \
--allow-unauthenticated
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# Terraform
.terraform
*.tfvars
*.tfstate
*.tfstate.*
9 changes: 9 additions & 0 deletions default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 8080;
root /usr/share/nginx/html;
index index.html;

location / {
try_files $uri $uri/ /index.html;
}
}
18 changes: 18 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:18-alpine AS builder

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

FROM nginx:alpine

COPY default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist /usr/share/nginx/html

EXPOSE 8080

CMD ["nginx", "-g", "daemon off;"]
22 changes: 22 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
}
}

provider "google" {
project = var.project_id
region = var.region
}

# Terraformを実行するサービスアカウントを作成
resource "google_service_account" "terraform_sa" {
account_id = "${var.terraform_sa_name}"
display_name = "Terraform Service Account"
project = var.project_id
}

# Terraformサービスアカウントに必要な権限を付与
module "terraform_sa_roles" {
source = "./modules/iam_binding"
project = var.project_id
roles = var.terraform_sa_roles
service_account_email = google_service_account.terraform_sa.email
}

# 必要なAPIの有効化
resource "google_project_service" "services" {
for_each = toset(var.enabled_apis)

project = var.project_id
service = each.key
disable_on_destroy = false

lifecycle {
create_before_destroy = true
}
}

# Artifact Registryリポジトリの作成
resource "google_artifact_registry_repository" "docker_repo" {
depends_on = [google_project_service.services]

location = var.region
repository_id = var.artifact_registry_repo_name
description = "Docker repository for GitHub Actions"
format = "DOCKER"
project = var.project_id
}

# GitHub Actions用のサービスアカウントの作成
resource "google_service_account" "github_actions" {
depends_on = [google_project_service.services]

account_id = var.github_actions.service_account_name
display_name = "Service Account for GitHub Actions"
description = "Used for deploying to Cloud Run from GitHub Actions"
project = var.project_id
}

# GitHub Actions用サービスアカウントに必要な権限の付与
module "github_actions_roles" {
source = "./modules/iam_binding"
project = var.project_id
roles = var.github_actions_roles
service_account_email = google_service_account.github_actions.email
}

# Workload Identity Poolの作成
resource "google_iam_workload_identity_pool" "github_pool" {
depends_on = [google_project_service.services]

workload_identity_pool_id = var.github_actions.workload_identity_pool_name
project = var.project_id
display_name = "GitHub Actions Pool"
description = "Workload Identity Pool for GitHub Actions"
}

# Workload Identity Providerの作成
resource "google_iam_workload_identity_pool_provider" "github_provider" {
workload_identity_pool_id = google_iam_workload_identity_pool.github_pool.workload_identity_pool_id
workload_identity_pool_provider_id = var.github_actions.workload_identity_provider_name
project = var.project_id

attribute_mapping = {
"google.subject" = "assertion.sub"
"attribute.actor" = "assertion.actor"
"attribute.repository" = "assertion.repository"
"attribute.ref" = "assertion.ref"
}

oidc {
issuer_uri = "https://token.actions.githubusercontent.com"
}

attribute_condition = "attribute.repository == \"${var.github_repo}\""
}

# Workload Identity Poolとサービスアカウントの紐付け
resource "google_service_account_iam_member" "workload_identity_user" {
service_account_id = google_service_account.github_actions.name
role = "roles/iam.workloadIdentityUser"
member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.github_pool.name}/attribute.repository/${var.github_repo}"
}

# Secretsに必要な情報を出力
output "github_actions" {
value = {
PROJECT_ID = var.project_id
WIF_PROVIDER = "projects/${data.google_project.project.number}/locations/global/workloadIdentityPools/${google_iam_workload_identity_pool.github_pool.workload_identity_pool_id}/providers/${google_iam_workload_identity_pool_provider.github_provider.workload_identity_pool_provider_id}"
SA_EMAIL = google_service_account.github_actions.email
ARTIFACT_REGISTRY_REPO_NAME = var.artifact_registry_repo_name
}
description = "GitHub Actions用の設定情報"
}

data "google_project" "project" {
project_id = var.project_id
}
19 changes: 19 additions & 0 deletions terraform/modules/iam_binding/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variable "project" {
type = string
}

variable "roles" {
type = list(string)
}

variable "service_account_email" {
type = string
}

resource "google_project_iam_member" "binding" {
for_each = toset(var.roles)

project = var.project
role = each.key
member = "serviceAccount:${var.service_account_email}"
}
67 changes: 67 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
variable "project_id" {
type = string
description = "GCP Project ID"
}

variable "region" {
type = string
description = "GCP Region"
default = "asia-northeast1"
}

variable "github_repo" {
type = string
description = "GitHubリポジトリ (例: user/repo)"
}

variable "artifact_registry_repo_name" {
type = string
description = "Artifact Registryのリポジトリ名"
}

variable "terraform_sa_name" {
type = string
description = "Terraform用サービスアカウントの名前"
default = "terraform-sa"
}

variable "github_actions" {
type = object({
service_account_name = string
workload_identity_pool_name = string
workload_identity_provider_name = string
})
description = "GitHub Actions関連の設定"
}

variable "enabled_apis" {
type = list(string)
description = "プロジェクトに必要なAPI"
default = [
"iam.googleapis.com",
"iamcredentials.googleapis.com",
"cloudresourcemanager.googleapis.com",
"run.googleapis.com",
"artifactregistry.googleapis.com"
]
}

variable "terraform_sa_roles" {
type = list(string)
description = "Terraform用サービスアカウントに付与するロール"
default = [
"roles/iam.serviceAccountAdmin",
"roles/iam.workloadIdentityPoolAdmin",
"roles/serviceusage.serviceUsageAdmin"
]
}

variable "github_actions_roles" {
type = list(string)
description = "GitHub Actions用サービスアカウントに付与するロール"
default = [
"roles/run.admin",
"roles/iam.serviceAccountUser",
"roles/artifactregistry.writer"
]
}

0 comments on commit a4fe59f

Please sign in to comment.