Repository navigation
125 lines (111 loc) · 4.55 KB
/
Copy pathdeploy-staging.yml
File metadata and controls
125 lines (111 loc) · 4.55 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
name: Deploy Kaapi to EC2 Staging
on:
workflow_run:
workflows: ["Kaapi CI"]
branches: [main]
types: [completed]
workflow_dispatch:
concurrency:
group: staging-ec2-deploy
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
environment: Staging
# workflow_dispatch runs unconditionally; workflow_run only on a green CI from a push to main (not PRs).
if: >-
${{ github.event_name == 'workflow_dispatch' ||
github.event.workflow_run.conclusion == 'success' }}
permissions:
id-token: write
contents: read
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ secrets.DEPLOY_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.INSTANCE_ID }}
ROOT_USER: ${{ secrets.USER }}
DEPLOY_SHA: ${{ github.event.workflow_run.head_sha || github.sha }}
run: |
: "${BUILD_DIRECTORY:?BUILD_DIRECTORY secret is not set}"
: "${ROOT_USER:?USER secret is not set}"
REMOTE_CMD="export HOME=/home/$ROOT_USER \
&& export NVM_DIR=/home/$ROOT_USER/.nvm \
&& . \$NVM_DIR/nvm.sh \
&& 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 \
&& sudo -iu $ROOT_USER 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 kaapi-staging" \
--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.INSTANCE_ID }}
CMD_ID: ${{ steps.ssm.outputs.cmd_id }}
run: |
: "${INSTANCE_ID:?INSTANCE_ID secret is not set}"
: "${CMD_ID:?SSM command ID missing from previous step}"
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: [deploy]
if: ${{ always() && needs.deploy.result != 'skipped' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: ./.github/actions/discord-notify
with:
webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
name: kaapi-frontend-staging
release: "#${{ github.run_number }}"
sha: ${{ github.event.workflow_run.head_sha || github.sha }}
run-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
ok: ${{ needs.deploy.result == 'success' }}