This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
152 lines (124 loc) · 5.53 KB
/
deploy.yaml
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
140
141
142
143
144
145
146
147
148
149
150
151
152
name: Deploy App
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install Amplify CLI
run: |
npm install -g @aws-amplify/cli
- name: Install NPM Dependencies
run: |
cd api
npm install
- name: Configure AWS credentials
id: 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: ${{ secrets.AWS_REGION }}
- name: Deploy Amplify Resources
run: |
cd api
export CI=1
npm ci
npx ampx pipeline-deploy --branch ${{ github.ref_name }} --app-id ${{ secrets.AMPLIFY_APP_ID }}
- name: Generate Amplify Outputs
run: |
cd api
npx ampx generate outputs --format dart --out-dir lib --branch ${{ github.ref_name }} --app-id ${{ secrets.AMPLIFY_APP_ID }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: true
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: fluttercon_usa_2024/api-${{ github.ref_name}}
IMAGE_TAG: ${{ github.sha }}
run: |
if ! aws ecr describe-repositories --repository-names $ECR_REPOSITORY 2>/dev/null; then
echo "ECR repository does not exist. Creating it..."
aws ecr create-repository --repository-name $ECR_REPOSITORY --image-scanning-configuration scanOnPush=true --encryption-configuration encryptionType=AES256
else
echo "ECR repository already exists."
fi
cd api
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Generate App Runner service name
id: generate-service-name
run: |
# Remove 'refs/heads/' prefix if present
BRANCH_NAME=${GITHUB_REF_NAME#refs/heads/}
# Replace invalid characters with hyphens and convert to lowercase
SANITIZED_BRANCH=$(echo $BRANCH_NAME | tr '[:upper:]' '[:lower:]' | sed -e 's/[^a-z0-9-]/-/g' -e 's/--*/-/g' -e 's/^-//' -e 's/-$//')
# Ensure the name starts with a letter or number
if [[ ! $SANITIZED_BRANCH =~ ^[a-z0-9] ]]; then
SANITIZED_BRANCH="b-$SANITIZED_BRANCH"
fi
# Truncate to 40 characters (leaving room for prefix)
SANITIZED_BRANCH=${SANITIZED_BRANCH:0:40}
# Create the full service name
SERVICE_NAME="fluttercon-usa-2024-api-${SANITIZED_BRANCH}"
# Truncate the full name to 40 characters if necessary
SERVICE_NAME=${SERVICE_NAME:0:40}
echo "SERVICE_NAME=$SERVICE_NAME" >> $GITHUB_OUTPUT
echo "Generated App Runner service name: $SERVICE_NAME"
- name: Deploy to App Runner
id: deploy-apprunner
uses: awslabs/amazon-app-runner-deploy@main
with:
service: ${{ steps.generate-service-name.outputs.SERVICE_NAME }}
image: ${{ steps.build-image.outputs.image }}
access-role-arn: ${{ secrets.ROLE_ARN }}
runtime: NODEJS_12
region: ${{ secrets.AWS_REGION }}
cpu : 1
memory : 2
port: 8080
wait-for-service-stability-seconds: 600
- name: Get Api URL
id: get-app-runner-url
run: |
SERVICE_ID="${{ steps.deploy-apprunner.outputs.service-id }}"
echo "Using Service ID: $SERVICE_ID"
# Extract SERVICE_NAME from SERVICE_ID
SERVICE_NAME="${{ steps.generate-service-name.outputs.SERVICE_NAME }}"
echo "Using: $SERVICE_NAME"
# Construct the service ARN
AWS_ACCOUNT_ID="${{ secrets.AWS_ACCOUNT_ID }}"
AWS_REGION="${{ secrets.AWS_REGION }}"
SERVICE_ARN="arn:aws:apprunner:${AWS_REGION}:${AWS_ACCOUNT_ID}:service/${SERVICE_NAME}/${SERVICE_ID}"
echo "Constructed Service ARN: $SERVICE_ARN"
SERVICE_URL=$(aws apprunner describe-service --service-arn "$SERVICE_ARN" --query 'Service.ServiceUrl' --output text)
if [ -z "$SERVICE_URL" ]; then
echo "Error: Unable to retrieve Service URL"
echo "Full service details:"
aws apprunner describe-service --service-arn "$SERVICE_ARN" --output json
exit 1
fi
echo "API_URL=https://$SERVICE_URL" >> $GITHUB_ENV
echo "API URL: $API_URL"
- name: Update Amplify Environment Variables
run: |
echo "Setting BASE_URL to ${API_URL}"
aws amplify update-branch --app-id ${{ secrets.AMPLIFY_APP_ID }} --branch ${{ github.ref_name }} --environment-variables BASE_URL=$API_URL
- name: Start Amplify Build
run: |
aws amplify start-job --app-id ${{ secrets.AMPLIFY_APP_ID }} --branch ${{ github.ref_name }} --job-type RELEASE