Skip to content

Commit f110c95

Browse files
SD-10: Add stage deployment
1 parent 8990e1d commit f110c95

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Build and push app to Amplify
2+
description: This action builds frontend and deploys it onto Amplify
3+
4+
inputs:
5+
aws-access-key-id:
6+
type: string
7+
required: true
8+
description: Access key ID to AWS account
9+
aws-secret-access-key:
10+
type: string
11+
required: true
12+
description: Access secret key to AWS account
13+
s3-bucket-name:
14+
type: string
15+
required: true
16+
description: Name of S3 bucket to store ZIP package
17+
aws-default-region:
18+
type: string
19+
description: AWS Amplify region
20+
default: "eu-central-1"
21+
amplify-app-id:
22+
type: string
23+
required: true
24+
description: APP ID of AWS Amplify app
25+
environment:
26+
type: choice
27+
options:
28+
- stage
29+
- prod
30+
required: true
31+
description: Environment name
32+
node-version:
33+
type: string
34+
description: Version of Node
35+
default: "20"
36+
37+
38+
runs:
39+
using: composite
40+
steps:
41+
- name: Call action get-ref-properties
42+
id: get-ref-properties
43+
uses: Cardinal-Cryptography/github-actions/get-ref-properties@v6
44+
45+
- name: Setup package name
46+
id: get-package-name
47+
shell: bash
48+
run: |
49+
date=$(date '+%Y-%m-%d-%H:%M')
50+
51+
if [ ! -z ${{ steps.get-ref-properties.outputs.tag }} ]; then
52+
version=$(echo ${{ steps.get-ref-properties.outputs.tag }})
53+
elif [ ! -z ${{ steps.get-ref-properties.outputs.sha }} ]; then
54+
version=$(echo ${{ steps.get-ref-properties.outputs.sha }})
55+
else
56+
echo "ERROR - nor commit SHA nor tag is set"
57+
fi
58+
59+
package_name=$(echo "release-${{ inputs.environment }}-${version}-${date}.zip")
60+
echo "package_name=${package_name}" >> $GITHUB_OUTPUT
61+
62+
- name: Setup node
63+
uses: actions/setup-node@v4
64+
with:
65+
node-version: ${{ inputs.node-version }}
66+
67+
- name: Clean install
68+
shell: bash
69+
run: npm ci
70+
71+
- name: Build an app
72+
shell: bash
73+
run: printenv | grep -i public_var && npm run build
74+
75+
- name: Compress app to ZIP file
76+
shell: bash
77+
run:
78+
cd dist/ && zip -r ../${{ steps.get-package-name.outputs.package_name }} .
79+
80+
- name: Upload ZIP file to S3
81+
uses: Cardinal-Cryptography/github-actions/copy-file-to-s3@v6
82+
env:
83+
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
84+
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
85+
AWS_DEFAULT_REGION: ${{ inputs.aws-default-region }}
86+
with:
87+
compression: false
88+
source-filename: "${{ steps.get-package-name.outputs.package_name }}"
89+
source-path: "${{ steps.get-package-name.outputs.package_name }}"
90+
s3-bucket-path: zips
91+
s3-bucket-filename: "${{ steps.get-package-name.outputs.package_name }}"
92+
s3-bucket-name: "${{ inputs.s3-bucket-name }}"
93+
if-exist: fallback
94+
95+
- name: Deploy app to Amplify
96+
shell: bash
97+
env:
98+
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
99+
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
100+
AWS_DEFAULT_REGION: ${{ inputs.aws-default-region }}
101+
APP_ID: ${{ inputs.amplify-app-id }}
102+
BRANCH_NAME: ${{ inputs.environment }}
103+
S3_BUCKET: ${{ inputs.s3-bucket-name }}
104+
run: |
105+
#!/bin/bash
106+
job_id=$(aws amplify start-deployment --region ${{ env.AWS_DEFAULT_REGION }} --app-id ${{ env.APP_ID }} --branch-name ${{ env.BRANCH_NAME }} --source-url s3://${{ env.S3_BUCKET }}/zips/${{ steps.get-package-name.outputs.package_name }} --output text --query "jobSummary.jobId")
107+
108+
i=1
109+
while [ $i -le 6 ]
110+
do
111+
if aws amplify list-jobs --region ${{ env.AWS_DEFAULT_REGION }} --app-id ${{ env.APP_ID }} --branch-name ${{ env.BRANCH_NAME }} --output text --query "jobSummaries[?jobId == '${job_id}'].status" | grep -i "succeed"; then
112+
echo "Done"
113+
exit 0
114+
else
115+
echo "Waiting... Status:"
116+
aws amplify list-jobs --region ${{ env.AWS_DEFAULT_REGION }} --app-id ${{ env.APP_ID }} --branch-name ${{ env.BRANCH_NAME }} --output text --query "jobSummaries[?jobId == '${job_id}']"
117+
i=$((i+1))
118+
sleep 10
119+
fi
120+
done
121+
echo "TIMEOUT - plase check the application manually in AWS Amplify console, exiting..."
122+
exit 1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: (STAGE) Build and deploy frontend
3+
4+
on:
5+
workflow_dispatch:
6+
7+
jobs:
8+
build-and-push:
9+
name: Build and push
10+
runs-on: ubuntu-20.04
11+
environment:
12+
name: stage
13+
steps:
14+
- name: GIT | Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Export variables
18+
env:
19+
VARS_CONTEXT: ${{ toJson(vars) }}
20+
run: |
21+
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
22+
to_envs() { jq -r "to_entries[] | select(.key|startswith(\"PUBLIC_VAR\")) | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
23+
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
24+
25+
- name: Build and deploy - STAGE
26+
uses: ./.github/actions/build-and-push
27+
with:
28+
environment: stage
29+
aws-access-key-id: ${{ secrets.AWS_S3_STAGE_ACCESS_KEY_ID }}
30+
aws-secret-access-key: ${{ secrets.AWS_S3_STAGE_SECRET_ACCESS_KEY }}
31+
s3-bucket-name: ${{ secrets.AWS_S3_STAGE_BUCKET_NAME }}
32+
amplify-app-id: ${{ secrets.AWS_AMPLIFY_STAGE_APP_ID }}

0 commit comments

Comments
 (0)