Skip to content

Commit

Permalink
Merge pull request #1 from to24toro/feature/github_actions
Browse files Browse the repository at this point in the history
prepare for github actions
  • Loading branch information
to24toro committed Nov 5, 2022
2 parents 0157070 + 7b9f5ae commit 9f55244
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @to24toro
58 changes: 58 additions & 0 deletions .github/workflows/terraform_apply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: terraform apply

on:
pull_request:
branches:
- main
types: [closed]

permissions:
id-token: write
contents: read

jobs:
apply:
name: terraform apply for GCP
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Configure GCP credentials
uses: "google-github-actions/auth@v0"
with:
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.2.9

- name: Terraform fmt
id: fmt
working-directory: ./terraform
run: terraform fmt -check
continue-on-error: true

- name: Terraform init
id: init
working-directory: ./terraform
run: terraform init

- name: Terraform Validate
id: validate
working-directory: ./terraform
run: terraform validate -no-color

- name: Terraform Plan
id: plan
working-directory: ./terraform
run: terraform plan -no-color
continue-on-error: true

- name: Terraform Apply
id: apply
working-directory: ./terraform
run: terraform apply -auto-approve

51 changes: 51 additions & 0 deletions .github/workflows/terraform_plan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: terraform plan

on:
pull_request:
branches:
- main

permissions:
id-token: write
contents: read

jobs:
plan:
name: terraform plan for GCP
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Configure GCP credentials
uses: "google-github-actions/auth@v0"
with:
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.2.9

- name: Terraform fmt
id: fmt
working-directory: ./terraform
run: terraform fmt -check
continue-on-error: true

- name: Terraform init
id: init
working-directory: ./terraform
run: terraform init

- name: Terraform Validate
id: validate
working-directory: ./terraform
run: terraform validate -no-color

- name: Terraform Plan
id: plan
working-directory: ./terraform
run: terraform plan -no-color
continue-on-error: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

.secret
.terraform.lock.hcl
18 changes: 18 additions & 0 deletions terraform/gcs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# resource "google_storage_bucket" "terraform-state" {
# name = "terraform-bucket"
# location = var.region
# storage_class = "STANDARD"

# versioning {
# enabled = true
# }

# lifecycle_rule {
# action {
# type = "Delete"
# }
# condition {
# num_newer_versions = 5
# }
# }
# }
27 changes: 27 additions & 0 deletions terraform/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# IAM policy

data "google_iam_policy" "workload_identity_user_github_actions" {
binding {
role = "roles/iam.workloadIdentityUser"
members = ["principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.github_actions.name}/attribute.repository/to24toro/terraform-gcp"]
}
}

resource "google_project_iam_member" "owner_github_actions" {
project = var.project_id
role = "roles/owner"
member = "serviceAccount:${google_service_account.github_actions.email}"
}

resource "google_project_iam_member" "github_actions_cloud_storage_admin" {
project = var.project_id
role = "roles/storage.objectAdmin"
member = "serviceAccount:${google_service_account.github_actions.email}"
}

# IAM binding

resource "google_service_account_iam_policy" "binding_sa_and_wi_github_actions" {
service_account_id = google_service_account.github_actions.name
policy_data = data.google_iam_policy.workload_identity_user_github_actions.policy_data
}
5 changes: 5 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
terraform {
backend "gcs" {
bucket = "terraform-bucket"
}
}
7 changes: 7 additions & 0 deletions terraform/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "workload_identity_provider" {
value = "${google_iam_workload_identity_pool.github_actions.name}/providers/${google_iam_workload_identity_pool_provider.github_actions.workload_identity_pool_provider_id}"
}

output "service_account" {
value = google_service_account.github_actions.email
}
5 changes: 5 additions & 0 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
5 changes: 5 additions & 0 deletions terraform/service_account.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "google_service_account" "github_actions" {
account_id = "terraform-github-actions"
display_name = "terraform_github_actions"
description = "service account for github actions"
}
14 changes: 14 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "project_id" {
type = string
default = "terraform-367614"
}

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

variable "zone" {
type = string
default = "asia-northeast1-a"
}
29 changes: 29 additions & 0 deletions terraform/workload_identity.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# github actions用プール
resource "google_iam_workload_identity_pool" "github_actions" {
provider = google-beta
project = var.project_id
workload_identity_pool_id = "github-actions"
description = "workload identity pool for github actions"
disabled = false
}

# github actions用プロバイダ
resource "google_iam_workload_identity_pool_provider" "github_actions" {
provider = google-beta
project = var.project_id
workload_identity_pool_id = google_iam_workload_identity_pool.github_actions.workload_identity_pool_id
workload_identity_pool_provider_id = "github-actions"
description = "workload identity pool provider for github actions"

# https://cloud.google.com/iam/docs/configuring-workload-identity-federation
# ID プロバイダの認証情報を外部 ID にマッピングする属性マッピングを定義
# google.subject : ユーザーの一意の識別子。ロールバインディングで使用され、Cloud Logging のログに表示される
# attribute. : 特定の属性を持つすべての ID にアクセス権を付与
attribute_mapping = {
"google.subject" = "assertion.sub" # リポジトリ名と Git リファレンス
"attribute.actor" = "assertion.actor" # Github Actions を実行したユーザーアカウント
"attribute.repository" = "assertion.repository" # オーナーとリポジトリ名
}
oidc { issuer_uri = "https://token.actions.githubusercontent.com" }
}

0 comments on commit 9f55244

Please sign in to comment.