-
Notifications
You must be signed in to change notification settings - Fork 0
130 lines (110 loc) · 4.88 KB
/
deploy.yml
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
name: Deploy to AWS App Runner
# Trigger this workflow on any push to the 'master' branch
on:
push:
branches:
- master
jobs:
# Job for deploying the backend service
deploy-backend:
name: Deploy Backend to AWS App Runner
runs-on: ubuntu-latest # Run on the latest version of Ubuntu
steps:
# Step 1: Check out the code from the repository
- name: Check out code
uses: actions/checkout@v3 # GitHub Action to check out the code
# Step 2: Set up AWS CLI with necessary credentials
- name: Set up AWS CLI
uses: aws-actions/configure-aws-credentials@v2 # GitHub Action to configure AWS credentials
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # AWS Access Key ID stored as a GitHub secret
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # AWS Secret Access Key stored as a GitHub secret
aws-region: us-east-1 # AWS Region stored as a GitHub secret
# Step 3: Deploy the backend service to AWS App Runner with retry logic
- name: Deploy Backend
run: |
SERVICE_ARN=$(aws apprunner list-services --query "ServiceSummaryList[?ServiceName=='${{ secrets.APP_RUNNER_SERVICE_NAME_BACKEND }}'].ServiceArn | [0]" --output text)
if [ -z "$SERVICE_ARN" ]; then
echo "Error: Service ARN not found for service name ${{ secrets.APP_RUNNER_SERVICE_NAME_BACKEND }}."
exit 1
fi
# Retry logic with exponential backoff
RETRY_COUNT=0
MAX_RETRIES=15
BACKOFF_TIME=5 # Initial backoff time in seconds
# Construct the JSON source configuration using jq
SOURCE_CONFIG=$(jq -n \
--arg repoUrl "${{ secrets.REPOSITORY_URL }}" \
'{ CodeRepository: { RepositoryUrl: $repoUrl, SourceCodeVersion: { Type: "BRANCH", Value: "master" }, SourceDirectory: "linguaphoto" } }'
)
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if aws apprunner update-service \
--service-arn "$SERVICE_ARN" \
--source-configuration "$SOURCE_CONFIG"; then
echo "Service updated successfully."
exit 0
else
RETRY_COUNT=$((RETRY_COUNT + 1))
echo "Update failed. Attempt $RETRY_COUNT of $MAX_RETRIES. Retrying in $BACKOFF_TIME seconds..."
sleep $BACKOFF_TIME
BACKOFF_TIME=$((BACKOFF_TIME * 2)) # Exponential backoff
fi
done
echo "Failed to update the service after $MAX_RETRIES attempts."
exit 1
# Job for deploying the frontend service
deploy-frontend:
name: Deploy Frontend to CloudFront
runs-on: ubuntu-latest # Run on the latest version of Ubuntu
steps:
# Step 1: Check out the repository
- name: Check out repository
uses: actions/checkout@v3
# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20.10.0"
# Step 3: Restore cache (Node modules and mypy cache)
- name: Restore cache
id: restore-cache
uses: actions/cache/restore@v3
with:
path:
frontend/node_modules/
key: deploy-${{ github.sha }}-${{ hashFiles('frontend/package-lock.json') }}-${{ hashFiles('linguaphoto/requirements.txt') }}
restore-keys: |
deploy-${{ github.sha }}-
deploy-
# Step 4: Install Node.js packages
- name: Install Node packages
working-directory: frontend
run: npm install
# Step 5: Build the frontend
- name: Build frontend
working-directory: frontend
run: npm run build
# Step 6: Configure AWS credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# Step 7: Deploy to S3
- name: Replace S3 bucket contents
run: |
aws s3 rm s3://${{ secrets.S3_BUCKET }}/build --recursive
aws s3 cp frontend/build s3://${{ secrets.S3_BUCKET }}/build --recursive
# Step 8: Invalidate CloudFront cache
- name: Invalidate CloudFront cache
run: |
aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*"
# Step 9: Save cache (only on the master branch)
- name: Save cache
uses: actions/cache/save@v3
if: github.ref == 'refs/heads/master'
with:
path:
frontend/node_modules/
key: deploy-${{ github.sha }}-${{ hashFiles('frontend/package-lock.json') }}-${{ hashFiles('linguaphoto/requirements.txt') }}