-
Notifications
You must be signed in to change notification settings - Fork 19
160 lines (155 loc) · 5.98 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
153
154
155
156
157
158
159
160
name: Deploy
on:
workflow_dispatch:
inputs:
version:
description: 'Version to deploy'
required: true
default: 'e.g. v1.20.1-beta.1'
env:
type: choice
description: Environment to deploy to
options:
- dev
- test
- prod
default: 'dev'
env:
project-directory: ./
repository: 'Greenstand/treetracker-wallet-admin-client'
jobs:
prepare:
name: checking and prepare settings for jobs
runs-on: ubuntu-latest
outputs:
CHANNEL: ${{ steps.resolver.outputs.CHANNEL }}
S3_BUCKET_SECRET_NAME: ${{ steps.resolver.outputs.S3_BUCKET_SECRET_NAME }}
CDN_ID_SECRET_NAME: ${{ steps.resolver.outputs.CDN_ID_SECRET_NAME }}
steps:
- uses: actions/setup-node@v1
with:
node-version: '20.x'
- uses: actions/checkout@v2
with:
# have to checkout master, to read releaserc, which means we must update the rc file in master with the newest settings
ref: v2
- run: |
echo "::debug:: begin preparing...";
echo "::debug:: version to deploy: $VERSION_TO_DEPLOY";
echo "::debug:: env to deploy: ${{ github.event.inputs.env }}";
ls -a
node <<EOF >> $GITHUB_OUTPUT
const s3BucketSecretNames = {
"main-dev": "DEV_CDN_S3_BUCKET",
"main-test": "TEST_CDN_S3_BUCKET",
"main-prod": "PROD_CDN_S3_BUCKET",
"beta-dev": "DEV_STAGING_CDN_S3_BUCKET",
"beta-test": "TEST_STAGING_CDN_S3_BUCKET",
"beta-prod": "STAGING_CDN_S3_BUCKET",
}
const cdnIdSecretNames = {
"main-dev": "DEV_CDN_DISTRIBUTION_ID",
"main-test": "TEST_CDN_DISTRIBUTION_ID",
"main-prod": "PROD_CDN_DISTRIBUTION_ID",
"beta-dev": "DEV_STAGING_CDN_DISTRIBUTION_ID",
"beta-test": "TEST_STAGING_CDN_DISTRIBUTION_ID",
"beta-prod": "STAGING_CDN_DISTRIBUTION_ID",
}
const releaseJson = require("./.releaserc.json");
const version = process.env.VERSION_TO_DEPLOY
const env = process.env.ENV_TO_DEPLOY;
const m = version.match(/^v\d+\.\d+.\d+(-([\w-\/\.]+)\.\d+)?$/)
let channel;
let s3BucketSecretName;
let cdnIdSecretName;
if(!m){
throw '::error:: wrong version:' + version;
}else{
if(!m[1]){
channel = "main";
}else{
const releaseName = m[2];
channel = releaseJson.branches.reduce((a,c) => a || (c.prerelease === releaseName && c.channel) || (c.name === releaseName && c.channel), false);
}
}
s3BucketSecretName = s3BucketSecretNames[channel + '-' + env];
if(!s3BucketSecretName){
throw '::error:: can not find s3 bucket secret name by, channel:' + channel + ' env:' + env;
}
cdnIdSecretName = cdnIdSecretNames[channel + '-' + env];
console.log("CHANNEL=" + channel);
console.log("S3_BUCKET_SECRET_NAME=" + s3BucketSecretName);
console.log("CDN_ID_SECRET_NAME=" + cdnIdSecretName);
EOF
id: resolver
env:
VERSION_TO_DEPLOY: ${{ github.event.inputs.version }}
ENV_TO_DEPLOY: ${{ github.event.inputs.env }}
- run: |
echo "resolver: ${{ steps.resolver.outputs.CHANNEL}}"
echo "resolver: ${{ steps.resolver.outputs.S3_BUCKET_SECRET_NAME}}"
echo "resolver: ${{ steps.resolver.outputs.CDN_ID_SECRET_NAME}}"
name: Print resolver
frontend:
name: Build Frontend Project
runs-on: ubuntu-latest
needs: prepare
if: |
!contains(github.event.head_commit.message, 'skip-ci')
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.inputs.version }}
- name: Use Node.js 20.x
uses: actions/setup-node@v1
with:
node-version: '20.x'
- name: npm clean install
run: npm ci --legacy-peer-deps
working-directory: ${{ env.project-directory }}
#if input 'env' is 'prod' then run 'npm run build'
- name: build for prod
if: "contains(github.event.inputs.env, 'prod')"
run: npm run build
working-directory: ${{ env.project-directory }}
- name: build for test
if: "contains(github.event.inputs.env, 'test')"
run: npm run build:test
working-directory: ${{ env.project-directory }}
- name: build for dev
if: "contains(github.event.inputs.env, 'dev')"
run: npm run build:dev
working-directory: ${{ env.project-directory }}
- uses: actions/upload-artifact@v2
with:
name: frontend-bundle
path: build
deploy:
name: Deploy to CDN
runs-on: ubuntu-latest
needs: [frontend, prepare]
if: |
github.repository == ${{ github.env.repository }}
steps:
- name: Print vars
run: |
echo "channel: ${{ needs.prepare.outputs.CHANNEL }}"
echo "s3: ${{ needs.prepare.outputs.S3_BUCKET_SECRET_NAME }}"
echo "cdn: ${{ needs.prepare.outputs.CDN_ID_SECRET_NAME }}"
- name: Download bundled frontend resources
uses: actions/download-artifact@v2
with:
name: frontend-bundle
path: build
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_KEY_ID_PROD }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_PROD }}
aws-region: us-east-1
- name: Copy front end resources to s3 bucket
run: |
aws s3 sync build s3://${{secrets[needs.prepare.outputs.S3_BUCKET_SECRET_NAME]}} --delete
- name: Invalidate cloudfront caches
run: |
aws cloudfront create-invalidation --distribution-id ${{ secrets[needs.prepare.outputs.CDN_ID_SECRET_NAME]}} --paths "/*"