Skip to content

Commit

Permalink
feat(infra): rafactor re-server Terraform and Github Actions Files
Browse files Browse the repository at this point in the history
  • Loading branch information
bocklag committed Dec 28, 2024
1 parent 49b1513 commit beb62e1
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 0 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/rc-build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: RC - CD - Build Images

on:
workflow_dispatch:

env:
AWS_REGION: ap-northeast-2
ECS_CLUSTER: Codedang-Api

permissions: # permissions to the job (for the OpenID Connection)
id-token: write
contents: read

jobs:
build-client-api:
name: Build client-api image
runs-on: ubuntu-latest
steps:
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_ECR_PUSH_RC }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build and push image
uses: docker/build-push-action@v6
with:
file: ./apps/backend/Dockerfile
push: true
build-args: |
target=client
app_env=production
tags: ${{ steps.login-ecr.outputs.registry }}/codedang-client-api:latest

build-admin-api:
name: Build admin-api image
runs-on: ubuntu-latest
steps:
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_ECR_PUSH_RC }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build and push image
uses: docker/build-push-action@v6
with:
file: ./apps/backend/Dockerfile
push: true
build-args: |
target=admin
app_env=production
tags: ${{ steps.login-ecr.outputs.registry }}/codedang-admin-api:latest

build-iris:
name: Build iris Docker image
runs-on: ubuntu-latest
steps:
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_ECR_PUSH_RC }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build and push image (iris)
uses: docker/build-push-action@v6
with:
push: true
context: '{{defaultContext}}:apps/iris'
build-args: |
app_env=production
tags: ${{ steps.login-ecr.outputs.registry }}/codedang-iris:latest
66 changes: 66 additions & 0 deletions .github/workflows/rc-deploy-target.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: RC - Deploy - Target

on:
workflow_dispatch:
inputs:
terraform_project:
description: 'Select Terraform Project to Deploy'
required: true
type: choice
options:
- 'network'
- 'storage'
- 'codedang'

env:
AWS_REGION: ap-northeast-2
ECS_CLUSTER: Codedang-Api

permissions: # permissions to the job (for the OpenID Connection)
id-token: write
contents: read

jobs:
rc-deploy-target-project:
name: RC - Deploy Terraform targeted Project
runs-on: ubuntu-latest
environment: production
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_DEPLOY_RC }}
aws-region: ${{ env.AWS_REGION }}

- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.2

- name: Create Terraform variable file
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: |
echo "$TFVARS_RC" >> terraform.tfvars
echo "$OAUTH_GITHUB" >> terraform.tfvars
echo "$OAUTH_KAKAO" >> terraform.tfvars
echo 'env = "rc"' >> terraform.tfvars
env:
TFVARS_RC: ${{ secrets.TFVARS_RC }}
OAUTH_GITHUB: ${{ secrets.OAUTH_GITHUB }}
OAUTH_KAKAO: ${{ secrets.OAUTH_KAKAO }}

- name: Terraform Init
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: terraform init -backend-config="bucket=codedang-tf-state-rc"

- name: Terraform Plan
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: terraform plan -input=false -out=plan.out

- name: Terraform Apply
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: terraform apply -input=false plan.out
133 changes: 133 additions & 0 deletions .github/workflows/rc-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: RC - Deploy

on:
workflow_dispatch:

env:
AWS_REGION: ap-northeast-2
ECS_CLUSTER: Codedang-Api

permissions: # permissions to the job (for the OpenID Connection)
id-token: write
contents: read

jobs:
rc-deploy-network:
name: RC - Deploy Network
runs-on: ubuntu-latest
environment: production
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_DEPLOY_RC }}
aws-region: ${{ env.AWS_REGION }}

- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.2

- name: Create Terraform variable file
working-directory: ./apps/infra/rc/network
run: |
echo 'env = "rc"' >> terraform.tfvars
- name: Terraform Init
working-directory: ./apps/infra/rc/network
run: terraform init -backend-config="bucket=codedang-tf-state-rc"

- name: Terraform Plan
working-directory: ./apps/infra/rc/network
run: terraform plan -input=false -out=plan.out

- name: Terraform Apply
working-directory: ./apps/infra/rc/network
run: terraform apply -input=false plan.out

rc-deploy-storage:
name: RC - Deploy Storage
runs-on: ubuntu-latest
needs: [rc-deploy-network]
environment: production
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_DEPLOY_RC }}
aws-region: ${{ env.AWS_REGION }}

- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.2

- name: Create Terraform variable file
working-directory: ./apps/infra/rc/storage
run: |
echo 'env = "rc"' >> terraform.tfvars
- name: Terraform Init
working-directory: ./apps/infra/rc/storage
run: terraform init -backend-config="bucket=codedang-tf-state-rc"

- name: Terraform Plan
working-directory: ./apps/infra/rc/storage
run: terraform plan -input=false -out=plan.out

- name: Terraform Apply
working-directory: ./apps/infra/rc/storage
run: terraform apply -input=false plan.out

rc-deploy-codedang:
name: RC - Deploy Codedang
runs-on: ubuntu-latest
needs: [rc-deploy-network, rc-deploy-storage]
environment: production
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_DEPLOY_RC }}
aws-region: ${{ env.AWS_REGION }}

- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.2

- name: Create Terraform variable file
working-directory: ./apps/infra/rc/codedang
run: |
echo "$TFVARS_RC" >> terraform.tfvars
echo "$OAUTH_GITHUB" >> terraform.tfvars
echo "$OAUTH_KAKAO" >> terraform.tfvars
echo 'env = "rc"' >> terraform.tfvars
env:
TFVARS_RC: ${{ secrets.TFVARS_RC }}
OAUTH_GITHUB: ${{ secrets.OAUTH_GITHUB }}
OAUTH_KAKAO: ${{ secrets.OAUTH_KAKAO }}

- name: Terraform Init
working-directory: ./apps/infra/rc/codedang
run: terraform init -backend-config="bucket=codedang-tf-state-rc"

- name: Terraform Plan
working-directory: ./apps/infra/rc/codedang
run: terraform plan -input=false -out=plan.out

- name: Terraform Apply
working-directory: ./apps/infra/rc/codedang
run: terraform apply -input=false plan.out
60 changes: 60 additions & 0 deletions .github/workflows/rc-destroy-target.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: RC - Destroy - Target

on:
workflow_dispatch:
inputs:
terraform_project:
description: 'Select Terraform Project to Destroy'
required: true
type: choice
options:
- 'network'
- 'storage'
- 'codedang'

env:
AWS_REGION: ap-northeast-2
ECS_CLUSTER: Codedang-Api

permissions: # permissions to the job (for the OpenID Connection)
id-token: write
contents: read

jobs:
rc-destroy-terraform-target-project:
name: RC - Destroy Terraform targeted Project
runs-on: ubuntu-latest
environment: production
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_FOR_DEPLOY_RC }}
aws-region: ${{ env.AWS_REGION }}

- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.2

- name: Create Terraform variable file
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: |
echo "$TFVARS_RC" >> terraform.tfvars
echo "$OAUTH_GITHUB" >> terraform.tfvars
echo "$OAUTH_KAKAO" >> terraform.tfvars
echo 'env = "rc"' >> terraform.tfvars
env:
TFVARS_RC: ${{ secrets.TFVARS_RC }}
OAUTH_GITHUB: ${{ secrets.OAUTH_GITHUB }}
OAUTH_KAKAO: ${{ secrets.OAUTH_KAKAO }}

- name: Destroy
working-directory: ./apps/infra/rc/${{ github.event.inputs.terraform_project }}
run: |
terraform init -backend-config="bucket=codedang-tf-state-rc"
terraform destroy -auto-approve
Loading

0 comments on commit beb62e1

Please sign in to comment.