-
Notifications
You must be signed in to change notification settings - Fork 0
170 lines (147 loc) · 6.96 KB
/
main.yaml
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Deploy
on:
push:
branches: [ "main" ]
jobs:
build-api:
name: Build API Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS ECR credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_REGION}}
- name: Login to Amazon ECR
id: login-aws-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push API image to ECR
env:
ECR_REGISTRY: ${{ steps.login-aws-ecr.outputs.registry }}
ECR_REPOSITORY: ${{secrets.API_IMAGE_REPO}}
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./api
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
build-frontend:
name: Build Frontend Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS ECR credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_REGION}}
- name: Login to Amazon ECR
id: login-aws-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push Frontened image to ECR
env:
ECR_REGISTRY: ${{ steps.login-aws-ecr.outputs.registry }}
ECR_REPOSITORY: ${{secrets.FRONTEND_IMAGE_REPO}}
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./frontend
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
deploy-api:
name: Deploy API to EC2
runs-on: ubuntu-latest
needs: build-api
steps:
- name: Configure AWS ECR credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_REGION}}
- name: Login to Amazon ECR
id: login-aws-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Pull and Run Docker Image from ECR
run: |
command_id=$(aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets "Key=instanceids,Values=${{ secrets.INSTANCE_ID_1}}, ${{ secrets.INSTANCE_ID_2}}" \
--comment "Pull Docker Image and run it" \
--parameters commands='[
"aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ${{ steps.login-aws-ecr.outputs.registry }}",
"docker stop ${{ vars.API_CONTAINER_NAME }} || true",
"docker pull ${{ steps.login-aws-ecr.outputs.registry }}/${{secrets.API_IMAGE_REPO}}:latest",
"docker run --rm -d -p 3000:3000 -e DB_HOST=${{ secrets.DB_HOST}} -e DB_USERNAME=${{ secrets.DB_USERNAME}} -e DB_PASSWORD=${{ secrets.DB_PASSWORD}} -e ENVIRONMENT=production --name ${{ vars.API_CONTAINER_NAME }} ${{steps.login-aws-ecr.outputs.registry}}/${{secrets.API_IMAGE_REPO}}:latest"]' --query "Command.CommandId" --output text)
echo "command_id=$command_id" >> $GITHUB_ENV
- name: Wait for Docker Commands to Complete on Instance 1
run: |
echo ${{ env.command_id }}
aws ssm wait command-executed \
--command-id ${{ env.command_id }} \
--instance-id ${{ secrets.INSTANCE_ID_1}} || true
- name: Wait for Docker Commands to Complete on Instance 2
run: |
echo ${{ env.command_id }}
aws ssm wait command-executed \
--command-id ${{ env.command_id }} \
--instance-id ${{ secrets.INSTANCE_ID_2}} || true
- name: Check Command Status
run: |
status=$(aws ssm list-commands --command-id ${{ env.command_id }} --query "Commands[0].StatusDetails" --output text)
echo "$status"
if [ "$status" == "Success" ]; then
echo "Your API is running"
else
echo "Command failed"
exit 1
fi
deploy-frontend:
name: Deploy Frontend to EC2
runs-on: ubuntu-latest
needs: build-frontend
steps:
- name: Configure AWS ECR credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_REGION}}
- name: Login to Amazon ECR
id: login-aws-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Pull and Run Docker Image from ECR
run: |
command_id=$(aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets "Key=instanceids,Values=${{ secrets.INSTANCE_ID_1}}, ${{ secrets.INSTANCE_ID_2}}" \
--comment "Pull Docker Image and run it" \
--parameters commands='[
"aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ${{ steps.login-aws-ecr.outputs.registry }}",
"docker stop ${{ vars.FRONTEND_CONTAINER_NAME }} || true",
"docker pull ${{ steps.login-aws-ecr.outputs.registry }}/${{secrets.FRONTEND_IMAGE_REPO}}:latest",
"docker run --rm -d -p 80:80 --name ${{ vars.FRONTEND_CONTAINER_NAME }} ${{steps.login-aws-ecr.outputs.registry}}/${{secrets.FRONTEND_IMAGE_REPO}}:latest"]' --query "Command.CommandId" --output text)
echo "command_id=$command_id" >> $GITHUB_ENV
- name: Wait for Docker Commands to Complete on Instance 1
run: |
echo ${{ env.command_id }}
aws ssm wait command-executed \
--command-id ${{ env.command_id }} \
--instance-id ${{ secrets.INSTANCE_ID_1}} || true
- name: Wait for Docker Commands to Complete on Instance 2
run: |
echo ${{ env.command_id }}
aws ssm wait command-executed \
--command-id ${{ env.command_id }} \
--instance-id ${{ secrets.INSTANCE_ID_2}} || true
- name: Check Command Status
run: |
status=$(aws ssm list-commands --command-id ${{ env.command_id }} --query "Commands[0].StatusDetails" --output text)
echo "$status"
if [ "$status" == "Success" ]; then
echo "YAY we did it! Your frontend is running"
else
echo "Command failed"
exit 1
fi