-
Notifications
You must be signed in to change notification settings - Fork 4
253 lines (222 loc) · 8.67 KB
/
deploy-examples-dev.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
name: Deploy Examples
on:
push:
branches:
- '**'
workflow_dispatch:
inputs:
deploy-all:
description: 'Deploys all examples to the live environment'
required: false
default: 'false'
type: boolean
# In case you push new commit into the MR / the previous runs will automatically stop
concurrency:
group: examples-${{ github.ref }}
cancel-in-progress: true
env:
# Node version for all the jobs
NODE_VERSION: 18
# Set env var based on branch name
GIT_REF: ${{ github.ref }}
EDGIO_ENV: ${{ github.ref == 'refs/heads/main' && 'live' || 'test' }}
EDGIO_TEAM: edgio-community
EDGIO_SITE: examples
EDGIO_DEPLOY_TOKEN: ${{secrets.EDGIO_EXAMPLES_DEPLOY_TOKEN}}
EDGIO_V7_DEPLOY_TOKEN: ${{secrets.EDGIO_V7_EXAMPLES_DEPLOY_TOKEN}}
COMMENT_ID: preview-${{ github.ref }}
DEPLOY_ALL: ${{ github.event.inputs.deploy-all }}
jobs:
prepare:
name: Prepare env for examples
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.setMatrix.outputs.matrix }}
branch: ${{ steps.setBranch.outputs.branch }}
pr: ${{ steps.finder.outputs.pr }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }} # NODE_VERSION
registry-url: https://registry.npmjs.org/
- name: Cache node modules
uses: actions/cache@v3
env:
cache-name: cache-packages-node-modules-node16-v1
with:
path: |
node_modules
*/*/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('package-lock.json', 'packages/**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- name: Extract branch name
id: setBranch
shell: bash
run: echo -e "branch=$(echo ${GITHUB_REF#refs/heads/} | sed 's/[^[:alnum:].-]/-/g')" >> $GITHUB_OUTPUT
- run: npm ci
- name: Get matrix of modified examples
id: setMatrix
run: |
matrix=$(node scripts/helpers/printModifiedExamples.js)
echo -e "matrix=$matrix" >> $GITHUB_OUTPUT
if [[ "$matrix" == "[]" ]]; then
echo "No jobs to run. Exiting workflow."
exit 0
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: jwalton/gh-find-current-pr@v1
id: finder
- name: Create comment for preview links
uses: marocchino/[email protected]
with:
number: ${{ steps.finder.outputs.pr }}
header: deploy-status
recreate: true
message: |
Preview links for commit ${{ github.sha }}:
examples:
name: Examples
needs: prepare
if: needs.prepare.outputs.matrix != '[]'
runs-on: ubuntu-latest
continue-on-error: true
strategy:
max-parallel: 5 # Prevent exceeding rate limit on Edgio platform during deployment
matrix:
example: ${{ fromJson( needs.prepare.outputs.matrix ) }}
env:
EXAMPLE_DIR: examples/${{ matrix.example }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }} # NODE_VERSION
registry-url: https://registry.npmjs.org/
# - uses: actions/setup-python@v2
# with:
# python-version: 2.7
- name: Cache node modules
uses: actions/cache@v3
env:
cache-name: cache-examples-node-modules-${{ matrix.example }}-node16-v2
with:
path: |
examples/${{ matrix.example }}/node_modules
*/*/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles(format('examples/{0}/package-lock.json', matrix.test), 'packages/*/package-lock.json') }}
# Install dependencies for this example
- name: Install dependencies
run: |
npm i -g @edgio/cli
# Check if package-lock.json file exists
if [ -f "package-lock.json" ]; then
# Use npm
npm ci --legacy-peer-deps
else
# Check if yarn.lock file exists
if [ -f "yarn.lock" ]; then
# Use yarn
yarn install --immutable
else
# No lock file found
echo "No lock file found. Please run 'npm ci' or 'yarn install --frozen-lockfile' to install dependencies."
fi
fi
working-directory: ${{ env.EXAMPLE_DIR }}
- name: Disable analytics
run: edgio config set-analytics false
- name: Get Edgio version and set environment variable
run: |
edgio_version=$(edgio --version)
echo "Edgio version is $edgio_version"
echo "EDGIO_VERSION=$edgio_version" >> $GITHUB_ENV
if [[ $edgio_version =~ ^7.* ]]; then
echo "ENV_CMD_ARGS=--site=$EDGIO_SITE --team=$EDGIO_TEAM --token=$EDGIO_V7_DEPLOY_TOKEN --ignore-error CONFIG_SOURCE_SWITCH" >> $GITHUB_ENV
else
echo "ENV_CMD_ARGS=--site=$EDGIO_SITE --team=$EDGIO_TEAM --token=$EDGIO_DEPLOY_TOKEN" >> $GITHUB_ENV
fi
- name: Prepare environments for the example
run: |
output=$(eval "edgio env list $ENV_CMD_ARGS")
# Get the lines that contain the list of environments
envs=$(echo "$output" | grep -A 1000 "Environments:")
# Remove the "Environments: " prefix from the first line and remove all lines before the first one
envs=$(echo "$envs" | tail -n +2)
# Replace all newline characters with a space
envs=$(echo "$envs" | tr '\n' ' ')
if [[ " $envs " =~ " $ENV_NAME " ]]; then
# environment exists in the list; skip it
echo "Environment '${ENV_NAME}' already exists in the list of environments and is being skipped."
else
# environment doesn't exist in the list; add it
eval "edgio env add $ENV_CMD_ARGS --environment=$ENV_NAME"
fi
env:
ENV_NAME: ${{ matrix.example }}-${{ env.EDGIO_ENV }}
- name: Validate package scripts
run: node scripts/helpers/validatePackageScripts.js ${{ env.EXAMPLE_DIR }}
- name: Deploy example
id: deploy
shell: bash
run: |
# Set branch argument only if BRANCH_NAME is not main
if [ "${{ env.BRANCH_NAME }}" != "main" ]; then
BRANCH_ARG="--branch=${{ env.BRANCH_NAME }}"
else
BRANCH_ARG=""
fi
# Run the command and redirect stderr to stdout; we return true
# to prevent the script from exiting if the command fails
DEPLOY=$(npm run edgio:deploy -- ${{ env.ENV_CMD_ARGS }} $BRANCH_ARG --environment=${{ env.ENV_NAME }} 2>&1) || true
# DEPLOY="${DEPLOY//'%'/'%25'}"
# DEPLOY="${DEPLOY//$'\n'/'%0A'}"
# DEPLOY="${DEPLOY//$'\r'/'%0D'}"
echo "$DEPLOY"
# Save the output of the command to the output variable
echo DEPLOY_OUTPUT=$DEPLOY >> $GITHUB_OUTPUT
# Get the permalink link from the output of the command
echo "$DEPLOY" |
sed -n 's/.*\(https.*-perma\.link\).*/\1/p' |
{
read permalink || permalink=""
if [[ -n $permalink ]]; then
echo "permalink=$permalink" >> $GITHUB_OUTPUT
else
echo "permalink=" >> $GITHUB_OUTPUT
fi
}
# Get the edge link from the output of the command
echo "$DEPLOY" |
sed -n 's/.*\(https.*\(-limelight\|\.edgio\)\.link\).*/\1/p' |
{
read edge || edge=""
if [[ -n $edge ]]; then
echo "edge=$edge" >> $GITHUB_OUTPUT
echo "status=✅ Successful!" >> $GITHUB_OUTPUT
exit 0
else
echo "edge=" >> $GITHUB_OUTPUT
echo "status=❌ See status check" >> $GITHUB_OUTPUT
exit 1
fi
}
env:
ENV_NAME: ${{ matrix.example }}-${{ env.EDGIO_ENV }}
BRANCH_NAME: ${{ needs.prepare.outputs.branch }}
working-directory: ${{ env.EXAMPLE_DIR }}
- name: Publish Preview Link
if: ${{ always() }}
uses: marocchino/[email protected]
with:
number: ${{ needs.prepare.outputs.pr }}
header: deploy-status
append: true
message: |
##### `${{ matrix.example }}`: ${{ steps.deploy.outputs.status }}
- Permalink: ${{ steps.deploy.outputs.permalink }}
- Edge: ${{ steps.deploy.outputs.edge }}