From 9c1182619251702f6c79a497fa61f4b6029a16be Mon Sep 17 00:00:00 2001 From: mametaro99 Date: Sat, 11 Jan 2025 11:15:04 +0900 Subject: [PATCH 1/3] =?UTF-8?q?next=E3=81=AE=E3=82=BF=E3=82=B9=E3=82=AF?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=81=AE=E8=A9=B3=E7=B4=B0=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next/task-definition.json | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 next/task-definition.json diff --git a/next/task-definition.json b/next/task-definition.json new file mode 100644 index 0000000..5c15710 --- /dev/null +++ b/next/task-definition.json @@ -0,0 +1,53 @@ +{ + "family": "zenn-clone-task-definition-frontend", + "containerDefinitions": [ + { + "name": "next", + "image": "667785573706.dkr.ecr.us-east-1.amazonaws.com/zenn-clone-next:latest", + "cpu": 0, + "portMappings": [ + { + "name": "next-80-tcp", + "containerPort": 80, + "hostPort": 80, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/zenn-clone-task-definition-frontend", + "mode": "non-blocking", + "awslogs-create-group": "true", + "max-buffer-size": "25m", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "systemControls": [] + } + ], + "taskRoleArn": "arn:aws:iam::667785573706:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::667785573706:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "volumes": [], + "placementConstraints": [], + "requiresCompatibilities": [ + "FARGATE" + ], + "cpu": "256", + "memory": "512", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "enableFaultInjection": false +} \ No newline at end of file From fa03cee99dd77206092174b993ceb1b2a84a2ea2 Mon Sep 17 00:00:00 2001 From: mametaro99 Date: Sat, 11 Jan 2025 11:15:41 +0900 Subject: [PATCH 2/3] =?UTF-8?q?ECS=E3=81=B8=E3=81=AE=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E3=83=87=E3=83=97=E3=83=AD=E3=82=A4=E7=94=A8=E3=81=AE=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 124 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..075dcd3 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,124 @@ +# CIチェックが通ること確認した上で、Rails, Next を自動デプロイ +name: Continuous Delivery + +# 当 workflow 実行タイミング +# main ブランチにコード差分が push されたときのみ +on: + push: + branches: [ "main" ] + +# 当 workflow 内で使用できる変数を定義 +# $AWS_REGION のような形で参照可能 +env: + AWS_REGION: us-east-1 + ECS_CLUSTER: zenn-clone-cluster + ECS_SERVICE_BACKEND: zenn-clone-backend-service + ECS_SERVICE_FRONTEND: zenn-clone-frontend-service + ECS_TASK_DEFINITION_BACKEND: ./rails/task-definition.json + ECS_TASK_DEFINITION_FRONTEND: ./next/task-definition.json + ECR_REPOSITORY_RAILS: zenn-clone-rails + ECR_REPOSITORY_NEXT: zenn-clone-next + CONTAINER_NAME_RAILS: rails + CONTAINER_NAME_NEXT: next + +# ECR push するための権限設定 +permissions: + contents: read + +# 当 workflow が行う処理 +jobs: + # 処理① ci.yml によるチェック + ci: + uses: ./.github/workflows/ci.yml + # 処理② Railsの自動デプロイ。動作には ci のクリアが必要 + deploy-rails: + needs: [ci] + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_RAILS:$IMAGE_TAG -f ./rails/Dockerfile.prod ./rails + docker push $ECR_REGISTRY/$ECR_REPOSITORY_RAILS:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY_RAILS:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Fill in the new image ID in the Amazon ECS task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION_BACKEND }} + container-name: ${{ env.CONTAINER_NAME_RAILS }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE_BACKEND }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true + # 処理③ Railsの自動デプロイ。動作には ci のクリアが必要 + deploy-next: + needs: [ci] + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_NEXT:$IMAGE_TAG -f ./next/Dockerfile.prod ./next + docker push $ECR_REGISTRY/$ECR_REPOSITORY_NEXT:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY_NEXT:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Fill in the new image ID in the Amazon ECS task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION_FRONTEND }} + container-name: ${{ env.CONTAINER_NAME_NEXT }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE_FRONTEND }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true \ No newline at end of file From e50e1169f45c486d3e42ba942dd1ca206335d874 Mon Sep 17 00:00:00 2001 From: mametaro99 Date: Sat, 11 Jan 2025 11:16:15 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=90=E3=83=83=E3=82=AF=E3=82=A8?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=81=AEECS=E3=82=BF=E3=82=B9=E3=82=AF?= =?UTF-8?q?=E3=81=AE=E6=83=85=E5=A0=B1=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rails/task-definition.json | 174 +++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 rails/task-definition.json diff --git a/rails/task-definition.json b/rails/task-definition.json new file mode 100644 index 0000000..5925f4f --- /dev/null +++ b/rails/task-definition.json @@ -0,0 +1,174 @@ +{ + "taskDefinitionArn": "arn:aws:ecs:us-east-1:667785573706:task-definition/zenn-clone-task-definition-backend:10", + "containerDefinitions": [ + { + "name": "rails", + "image": "667785573706.dkr.ecr.us-east-1.amazonaws.com/zenn-clone-rails", + "cpu": 0, + "portMappings": [ + { + "name": "rails-3000-tcp", + "containerPort": 3000, + "hostPort": 3000, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [ + { + "name": "RAILS_MASTER_KEY", + "value": "baa4eee02f9c7fc3a88c9f113fb2ee53" + }, + { + "name": "RAILS_LOG_TO_STDOUT", + "value": "true" + } + ], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/zenn-clone-task-definition-backend", + "mode": "non-blocking", + "awslogs-create-group": "true", + "max-buffer-size": "25m", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "healthCheck": { + "command": [ + "CMD-SHELL", + "curl --unix-socket /myapp/tmp/sockets/puma.sock localhost/api/v1/health_check || exit 1" + ], + "interval": 30, + "timeout": 5, + "retries": 3 + }, + "systemControls": [] + }, + { + "name": "nginx", + "image": "667785573706.dkr.ecr.us-east-1.amazonaws.com/zenn-clone-nginx", + "cpu": 0, + "portMappings": [ + { + "name": "nginx-80-tcp", + "containerPort": 80, + "hostPort": 80, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [ + { + "sourceContainer": "rails", + "readOnly": false + } + ], + "dependsOn": [ + { + "containerName": "rails", + "condition": "HEALTHY" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/zenn-clone-task-definition-backend", + "mode": "non-blocking", + "awslogs-create-group": "true", + "max-buffer-size": "25m", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "healthCheck": { + "command": [ + "CMD-SHELL", + "curl -f http://localhost/api/v1/health_check || exit 1" + ], + "interval": 30, + "timeout": 5, + "retries": 3 + }, + "systemControls": [] + } + ], + "family": "zenn-clone-task-definition-backend", + "taskRoleArn": "arn:aws:iam::667785573706:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::667785573706:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "revision": 10, + "volumes": [], + "status": "ACTIVE", + "requiresAttributes": [ + { + "name": "ecs.capability.execution-role-awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.ecr-auth" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.28" + }, + { + "name": "com.amazonaws.ecs.capability.task-iam-role" + }, + { + "name": "ecs.capability.container-health-check" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" + }, + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.24" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" + }, + { + "name": "ecs.capability.container-ordering" + } + ], + "placementConstraints": [], + "compatibilities": [ + "EC2", + "FARGATE" + ], + "requiresCompatibilities": [ + "FARGATE" + ], + "cpu": "256", + "memory": "512", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "registeredAt": "2025-01-09T02:12:49.753Z", + "registeredBy": "arn:aws:iam::667785573706:root", + "enableFaultInjection": false, + "tags": [] +} \ No newline at end of file