-
Notifications
You must be signed in to change notification settings - Fork 1
99 lines (91 loc) · 3.26 KB
/
Copy pathdeploy-api.yml
File metadata and controls
99 lines (91 loc) · 3.26 KB
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
name: Deploy API
on:
push:
branches: [prod]
paths:
- "src/**"
- "Dockerfile"
- ".dockerignore"
- "package.json"
- "yarn.lock"
- "tsconfig.json"
- "infra/scripts/**"
- ".github/workflows/deploy-api.yml"
workflow_dispatch:
inputs:
frontend_url:
description: "Frontend origin for CORS and email links"
required: false
default: https://dev.myartverse.app
frontend_domain:
description: "Frontend hostname (no protocol)"
required: false
default: dev.myartverse.app
concurrency:
group: deploy-api-${{ github.ref }}
cancel-in-progress: true
env:
AWS_REGION: us-east-2
FRONTEND_URL: https://dev.myartverse.app
FRONTEND_DOMAIN: dev.myartverse.app
permissions:
contents: read
jobs:
deploy:
name: Build, push, and deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure AWS 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: ${{ env.AWS_REGION }}
- name: Deploy to EC2 via ECR
env:
FRONTEND_URL: ${{ github.event_name == 'workflow_dispatch' && inputs.frontend_url || 'https://dev.myartverse.app' }}
FRONTEND_DOMAIN: ${{ github.event_name == 'workflow_dispatch' && inputs.frontend_domain || 'dev.myartverse.app' }}
run: |
chmod +x infra/scripts/deploy-api.sh infra/scripts/refresh-env.sh
./infra/scripts/deploy-api.sh
- name: Health check
run: |
API_URL=$(aws cloudformation describe-stacks \
--stack-name MyArtverseStack \
--region "$AWS_REGION" \
--query "Stacks[0].Outputs[?OutputKey=='ApiUrl'].OutputValue" \
--output text)
echo "Checking $API_URL/health"
for i in $(seq 1 18); do
if curl -fsS "$API_URL/health" | grep -q '"status":"ok"'; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i failed, retrying..."
sleep 10
done
echo "Health check failed — fetching container logs from EC2..."
INSTANCE_ID=$(aws cloudformation describe-stacks \
--stack-name MyArtverseStack \
--region "$AWS_REGION" \
--query "Stacks[0].Outputs[?OutputKey=='Ec2InstanceId'].OutputValue" \
--output text)
CMD_ID=$(aws ssm send-command \
--region "$AWS_REGION" \
--instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":["docker ps -a","docker logs myartverse-api --tail 100 2>&1"]}' \
--query Command.CommandId \
--output text)
aws ssm wait command-executed \
--region "$AWS_REGION" \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID" || true
aws ssm get-command-invocation \
--region "$AWS_REGION" \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID" \
--query StandardOutputContent \
--output text || true
exit 1