Repository navigation
139 lines (124 loc) · 4.95 KB
/
Copy pathcreate-release.yml
File metadata and controls
139 lines (124 loc) · 4.95 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
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
name: Deploy Kaapi to EC2 Production
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+" # Deploy only when tags like v1.0.0, v2.1.0, etc., are created
concurrency:
group: production-ec2-deploy
cancel-in-progress: false
jobs:
verify-ci:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
steps:
- name: Confirm CI passed on the tagged commit
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
SHA="${{ github.sha }}"
echo "Checking Kaapi CI conclusion for ${SHA}"
CONCLUSION=$(gh api \
"repos/${{ github.repository }}/actions/runs?head_sha=${SHA}" \
--jq 'first(.workflow_runs[]
| select(.name == "Kaapi CI" and .status == "completed")
| .conclusion) // "missing"')
echo "Kaapi CI conclusion: ${CONCLUSION}"
if [ "${CONCLUSION}" != "success" ]; then
echo "::error::Kaapi CI has not passed for ${SHA} (conclusion: ${CONCLUSION}). Aborting release."
exit 1
fi
deploy:
needs: verify-ci
runs-on: ubuntu-latest
environment: AWS_ENV
permissions:
id-token: write
contents: read
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Trigger deploy on EC2 via SSM
id: ssm
env:
BUILD_DIRECTORY: ${{ secrets.BUILD_DIRECTORY }}
APP_NAME: ${{ secrets.PM2_APP_NAME }}
AWS_REGION: ${{ secrets.AWS_REGION }}
INSTANCE_ID: ${{ secrets.EC2_INSTANCE_ID }}
DEPLOY_SHA: ${{ github.sha }}
run: |
: "${BUILD_DIRECTORY:?BUILD_DIRECTORY secret is not set}"
REMOTE_CMD="git config --global --add safe.directory $BUILD_DIRECTORY \
&& cd $BUILD_DIRECTORY \
&& git fetch origin --tags --prune \
&& git checkout $DEPLOY_SHA \
&& npm ci \
&& npm run build \
&& pm2 restart $APP_NAME"
# Build the parameters as JSON so quoting in REMOTE_CMD can't break it.
PARAMS=$(jq -n --arg cmd "$REMOTE_CMD" '{commands: [$cmd]}')
CMD_ID=$(aws ssm send-command \
--instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" \
--comment "Deploy kaapi-frontend ${{ github.ref_name }}" \
--parameters "$PARAMS" \
--region "$AWS_REGION" \
--query "Command.CommandId" --output text)
echo "cmd_id=$CMD_ID" >> "$GITHUB_OUTPUT"
echo "Sent SSM command: $CMD_ID"
- name: Wait for SSM command to finish
timeout-minutes: 20
env:
INSTANCE_ID: ${{ secrets.EC2_INSTANCE_ID }}
CMD_ID: ${{ steps.ssm.outputs.cmd_id }}
run: |
while true; do
if aws ssm wait command-executed \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID"; then
break # waiter succeeds only on Status == Success
fi
STATUS=$(aws ssm get-command-invocation \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID" \
--query "Status" --output text 2>/dev/null || echo "Pending")
case "$STATUS" in
Success) break ;;
Failed|Cancelled|Cancelling|TimedOut)
echo "Deployment ended with status: $STATUS"
aws ssm get-command-invocation \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID" \
--query '{Status:Status,Stdout:StandardOutputContent,Stderr:StandardErrorContent}' \
--output json
exit 1 ;;
*) echo "Still running (status: $STATUS) — waiter capped out, re-waiting" ;;
esac
done
echo "Deployment completed successfully."
aws ssm get-command-invocation \
--command-id "$CMD_ID" \
--instance-id "$INSTANCE_ID" \
--query '{Status:Status,Stdout:StandardOutputContent,Stderr:StandardErrorContent}' \
--output json
notify:
needs: [verify-ci, deploy]
if: always()
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: ./.github/actions/discord-notify
with:
webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
name: kaapi-frontend-production
release: ${{ github.ref_name }}
sha: ${{ github.sha }}
run-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
ok: ${{ needs.deploy.result == 'success' }}
# Call out the specific case where the release was blocked by CI.
failure-reason: ${{ needs.verify-ci.result != 'success' && 'CI never passed on the tagged commit — release blocked.' || '' }}