-
Notifications
You must be signed in to change notification settings - Fork 0
84 lines (74 loc) · 2.94 KB
/
deploy-dev.yml
File metadata and controls
84 lines (74 loc) · 2.94 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
name: Deploy API to AWS
on:
pull_request:
branches:
- master
types:
- closed
jobs:
deploy-api:
if: github.event.pull_request.merged
runs-on: ubuntu-latest
env:
CODE_BUCKET: ${{vars.CODE_BUCKET}}
REGION: ${{ vars.AWS_REGION }}
EC2_INSTANCE_ID: ${{ vars.EC2_INSTANCE_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache api files
id: cache-api
uses: actions/cache@v4
env:
cache-name: cache-api-files
with:
path: .
key: ${{ hashFiles('package*.json') }}-${{ hashFiles('src/**/*') }}
- if: ${{ steps.cache-api.outputs.cache-hit == 'true' }}
name: Check api changes
continue-on-error: true
run: echo 'No api changes found. Skip api build and deployment.'
- if: ${{ steps.cache-api.outputs.cache-hit != 'true' }}
name: Build
run: |
npm ci
npm run build
- if: ${{ steps.cache-api.outputs.cache-hit != 'true' }}
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: ${{vars.AWS_REGION}}
- if: ${{ steps.cache-api.outputs.cache-hit != 'true' }}
name: Zip application
run: zip -r app.zip . -x ".git/*" ".github/*" "infra/*" "docker/*" "test/*" --quiet
- if: ${{ steps.cache-api.outputs.cache-hit != 'true' }}
name: Upload to S3
run: |
FILE_NAME="app-$(date +%Y-%m-%d_%H:%M)-${{ github.sha }}.zip"
aws s3 cp app.zip s3://$CODE_BUCKET/$FILE_NAME
echo "FILE_NAME=$FILE_NAME" >> $GITHUB_ENV
- if: ${{ steps.cache-api.outputs.cache-hit != 'true' }}
name: Update EC2 from S3 and restart docker-compose
run: |
# Send SSM command to download code and build artifact, restart docker-compose, and run migrations
aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets "Key=instanceids,Values=$EC2_INSTANCE_ID" \
--comment "Deploy $GITHUB_SHA" \
--parameters commands='[
"set -e",
"cd /var/www/app",
"aws s3 cp s3://'"$CODE_BUCKET"'/'"$FILE_NAME"' app.zip --region '"$REGION"'",
"sudo -u ec2-user unzip -qo app.zip",
"sleep 15",
"sudo -u ec2-user /usr/local/bin/docker-compose -f docker-compose-dev.yml restart api-run postgres redis",
"sleep 15",
"sudo -u ec2-user /usr/local/bin/docker-compose -f docker-compose-dev.yml exec -T api-run npm run migration:run"
]' \
--region "$REGION" \
--query "Command.CommandId" \
--output text > command_id.txt
COMMAND_ID=$(cat command_id.txt)
echo "SSM CommandId: $COMMAND_ID"