-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (135 loc) · 6.03 KB
/
workflow.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: BnbBankCI
on:
push:
branches:
- staging
- production
jobs:
deploy-api:
name: Deploy Server Api
runs-on: ubuntu-latest
steps:
# This step checks out a copy of your repository.
- name: Checkout code
uses: actions/checkout@v3
- name: Get branch names
id: branch-names
uses: tj-actions/branch-names@v8
# This step sets up Node.js environment.
- name: Setup Node.js
uses: actions/setup-node@v3
if: success()
# This step installs the Serverless Framework globally.
- name: Install Serverless Framework
run: |
cd backend
npm install -g serverless serverless-prune-plugin serverless-iam-roles-per-function
if: success()
# This step sets up PHP environment with the specified version.
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
if: success()
# This step installs Composer dependencies with the specified options.
- name: Install Composer dependencies
run: |
cd backend
composer install --prefer-dist --optimize-autoloader --no-dev
if: success()
# This step expose variables to environment
- name: Expose required environment variables
run: |
echo "BRANCH=${{ steps.branch-names.outputs.current_branch }}" >> $GITHUB_ENV
echo "DOMAIN=${{ vars.DOMAIN }}" >> $GITHUB_ENV
echo "DB_URI=${{ secrets.DB_URI }}" >> $GITHUB_ENV
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> $GITHUB_ENV
echo "APP_KEY=${{ secrets.APP_KEY }}" >> $GITHUB_ENV
echo "APP_NAME=${{ vars.SUBDOMAIN }}-api" >> $GITHUB_ENV
echo "SUBDOMAIN=${{ vars.SUBDOMAIN }}-api" >> $GITHUB_ENV
echo "AWS_HOSTED_ZONE_ID=${{ vars.AWS_HOSTED_ZONE_ID }}" >> $GITHUB_ENV
echo "AWS_DEFAULT_REGION=${{ vars.AWS_DEFAULT_REGION }}" >> $GITHUB_ENV
echo "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
if: success()
# This step deploys your application to AWS Lambda using the Serverless Framework.
- name: Deploy to AWS Lambda
run: |
cd backend
echo "APP_KEY=${APP_KEY}" >> .env
echo "APP_NAME=${APP_NAME}" >> .env
echo "SUBDOMAIN=${SUBDOMAIN}" >> .env
echo "DOMAIN=${DOMAIN}" >> .env
echo "DB_URI=${DB_URI}" >> .env
echo "JWT_SECRET=${JWT_SECRET}" >> .env
echo "AWS_HOSTED_ZONE_ID=${AWS_HOSTED_ZONE_ID}" >> .env
serverless deploy --config serverless.yml --stage ${BRANCH} --region ${AWS_DEFAULT_REGION} --verbose
if: success()
deploy-web:
name: Deploy Web App
runs-on: ubuntu-latest
steps:
# This step checks out a copy of your repository.
- name: Checkout code
uses: actions/checkout@v3
- name: Get branch names
id: branch-names
uses: tj-actions/branch-names@v8
# This step sets up Node.js environment.
- name: Setup Node.js
uses: actions/setup-node@v3
if: success()
# This step installs the Serverless Framework globally.
- name: Install Serverless Framework
run: |
cd frontend
npm install -g serverless serverless-dotenv-plugin serverless-export-outputs fast-xml-parser
if: success()
# This step expose variables to environment
- name: Expose required environment variables
run: |
echo "BRANCH=${{ steps.branch-names.outputs.current_branch }}" >> $GITHUB_ENV
echo "DOMAIN=${{ vars.DOMAIN }}" >> $GITHUB_ENV
echo "APP_NAME=${{ vars.SUBDOMAIN }}-web" >> $GITHUB_ENV
echo "SUBDOMAIN=${{ vars.SUBDOMAIN }}" >> $GITHUB_ENV
echo "AWS_HOSTED_ZONE_ID=${{ vars.AWS_HOSTED_ZONE_ID }}" >> $GITHUB_ENV
echo "AWS_DEFAULT_REGION=${{ vars.AWS_DEFAULT_REGION }}" >> $GITHUB_ENV
echo "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
if: success()
# This step expose variables to environment
- name: Expose required environment variables (composed)
run: |
echo "S3_DOMAIN=${APP_NAME}.$([ "${BRANCH}" != "production" ] && echo "${BRANCH}.")${DOMAIN}" >> $GITHUB_ENV
if: success()
# This step build reactjs application to be deployed.
- name: Build ReactJS application
run: |
cd frontend
rm -rf .env
echo "REACT_APP_API_URL=https://${SUBDOMAIN}-api.$([ "${BRANCH}" != "production" ] && echo "${BRANCH}.")${DOMAIN}" >> .env
yarn --dev --production=false
yarn build
if: success()
# This step deploys the application resources to AWS using the Serverless Framework.
- name: Deploy resources to AWS with Serverless Framework
run: |
cd frontend
rm -rf .env
echo "DOMAIN=${DOMAIN}" >> .env
echo "AWS_HOSTED_ZONE_ID=${AWS_HOSTED_ZONE_ID}" >> .env
serverless deploy --config serverless-cert.yml --stage ${BRANCH} --region us-east-1 --verbose
SLS_WEBCERT_OUTPUT=$(cat sls-webcert-output.json)
$(echo "${SLS_WEBCERT_OUTPUT}" | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"')
echo "AWS_ACM_ARN=${CertificateDomainArn}" >> .env
serverless deploy --config serverless.yml --stage ${BRANCH} --region ${AWS_DEFAULT_REGION} --verbose
if: success()
# This step deploys your application to AWS Lambda using the Serverless Framework.
- name: Release application on AWS S3 and invalidate CloudFront cache
run: |
cd frontend
aws s3 sync build/ s3://${S3_DOMAIN}
SLS_OUTPUT=$(cat sls-output.json)
$(echo "${SLS_OUTPUT}" | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"')
aws cloudfront create-invalidation --distribution-id ${CDNDistributionId} --paths "/*"
if: success()