-
Notifications
You must be signed in to change notification settings - Fork 0
95 lines (84 loc) · 4.11 KB
/
prod_ecs.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
on:
# workflow_dispatch:
push:
# branches: [ develop ]
branches: [ main ]
name: Deploy to Amazon ECS
env:
APP_NAME: ${{ github.event.repository.name }}
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
- 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: ap-northeast-1
- 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 }}
ECR_REPOSITORY_APP: ${{ env.APP_NAME }}-app
ECR_REPOSITORY_NGINX: ${{ env.APP_NAME }}-nginx
IMAGE_TAG: latest
DOCKER_BUILDKIT: 1
run: |
# Build a docker container and push it to ECR so that it can be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG -f .docker/nginx/prod.Dockerfile ./
docker push $ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG"
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG -f .docker/app/prod.Dockerfile ./
docker push $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG"
# Rewrite task-definition
- name: rewrite task-definition
run: sed -i -e s/SED_TARGET_AWS_ACCOUNT_ID/${{ secrets.AWS_ACCOUNT_ID }}/g .aws/ecs/prod/migration-task-definition.json &&
sed -i -e s/SED_TARGET_AWS_ACCOUNT_ID/${{ secrets.AWS_ACCOUNT_ID }}/g .aws/ecs/prod/task-definition.json &&
sed -i -e s/SED_TARGET_APP_NAME/${{ env.APP_NAME }}/g .aws/ecs/prod/migration-task-definition.json &&
sed -i -e s/SED_TARGET_APP_NAME/${{ env.APP_NAME }}/g .aws/ecs/prod/task-definition.json &&
sed -i -e s/SED_TARGET_LOKI_ID/${{ secrets.LOKI_ID }}/g .aws/ecs/prod/task-definition.json &&
sed -i -e s/SED_TARGET_LOKI_SECRET/${{ secrets.LOKI_SECRET }}/g .aws/ecs/prod/task-definition.json
# Migration
- name: migration task def
id: migration-task-def
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: .aws/ecs/prod/migration-task-definition.json
cluster: ${{ env.APP_NAME }}
- name: migrate
run: aws ecs run-task
--launch-type FARGATE
--network-configuration "awsvpcConfiguration={subnets=[${{ secrets.SUBNETS }}], securityGroups=[${{ secrets.SECURITY_GROUPS }}], assignPublicIp=ENABLED}"
--cluster ${{ env.APP_NAME }}
--task-definition ${{ steps.migration-task-def.outputs.task-definition-arn }}
# Deploy
- name: Render Amazon ECS task definition for first container
id: render-web-container
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .aws/ecs/prod/task-definition.json
container-name: nginx
image: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-1.amazonaws.com/${{ env.APP_NAME }}-nginx:latest
- name: Modify Amazon ECS task definition with second container
id: render-app-container
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.render-web-container.outputs.task-definition }}
container-name: app
image: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-1.amazonaws.com/${{ env.APP_NAME }}-app:latest
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-app-container.outputs.task-definition }}
service: ${{ env.APP_NAME }}
cluster: ${{ env.APP_NAME }}
# wait-for-service-stability: true