-
Notifications
You must be signed in to change notification settings - Fork 23
186 lines (164 loc) · 6.47 KB
/
build-and-deploy.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
name: Build and deploy
on:
push:
branches:
- master
paths:
- 'content/**'
- 'template/**'
- 'package-lock.json'
- '.ecomplus.cd.json'
- '.netlify/**'
- '.github/build-and-deploy'
- '.github/workflows/build-and-deploy.yml'
jobs:
build-and-deploy:
name: Build Storefront and deploy
runs-on: ubuntu-latest
timeout-minutes: 30
if: |
github.event.head_commit.message != 'Initial commit' &&
!contains(github.event.head_commit.message, '[skip ci]')
steps:
- name: Set remote repo URL
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "::set-output name=url::https://${GITHUB_ACTOR}:${TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
id: remote_repo
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '16.x'
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Get ISG state
id: isg_state
run: |
if [ -f .isg ]; then
echo "::set-output name=rand::$(cat .isg)";
else
echo "::set-output name=rand::0";
fi
- name: Cache output
continue-on-error: true
uses: actions/cache@v4
with:
path: .deploy/publish
key: ${{ hashFiles('.bundles.json') }}-${{ hashFiles('template/p*/**') }}-${{ hashFiles('content/**') }}-${{ steps.isg_state.outputs.rand }}
- name: Install dependencies
run: npm ci --only=production
- name: Check last changed files
uses: lots0logs/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
id: changed_files
- name: Run partial build
env:
CHANGED_FILES: ${{ steps.changed_files.outputs.all }}
run: |
IS_ECOMPLUS_CD=$(node -p "const f = JSON.parse('${CHANGED_FILES}'); f.length === 1 && f[0] === '.ecomplus.cd.json'");
if [ $IS_ECOMPLUS_CD != false ] && [ -f .bundles.json ] && [ -f .deploy/publish/storefront.js ]; then
BUILD_SLUG=$(node -p "require('./.ecomplus.cd.json').slug || false");
if [ $BUILD_SLUG != false ]; then
npm run build -- --prerender=index,$BUILD_SLUG --no-bundler;
echo "::set-output name=isg::2";
else
npm run build -- --prerender-limit=1000 --no-bundler;
echo "::set-output name=isg::1";
fi
else
npm run build -- --prerender=index,app/index,search,404 --prerender-limit=300;
echo "::set-output name=isg::0";
fi
id: partial_build
- name: Output ISG state
id: new_isg_state
env:
ISG_ENUM: ${{ steps.partial_build.outputs.isg }}
run: |
if [ $ISG_ENUM != 0 ] || [ ! -f .isg ]; then
ISG_STATE=$RANDOM;
echo $ISG_STATE > .isg;
else
ISG_STATE=$(cat .isg);
fi
echo "::set-output name=rand::$ISG_STATE";
- name: Config Git and check diff
run: |
git config --local user.email '[email protected]'
git config --local user.name 'GitHub Action'
git add .isg .bundles.json content template
(git diff-index --quiet HEAD && echo "::set-output name=diff::0") || echo "::set-output name=diff::1"
id: git_diff
- name: Commit local file changes
if: steps.git_diff.outputs.diff == 1
run: |
git commit -m 'chore(post-build): update template defaults, bundles and isg states [skip ci]'
- name: Push changes
if: steps.git_diff.outputs.diff == 1
continue-on-error: true
env:
REMOTE_REPO: ${{ steps.remote_repo.outputs.url }}
run: git push "${REMOTE_REPO}" HEAD:master --follow-tags
- name: Clear cache if not ISG
if: steps.partial_build.outputs.isg == 0
continue-on-error: true
run: rm -rf .deploy
- name: Setup output directory to deploy
run: |
mkdir -p .deploy/publish
cp .nvmrc .deploy/ 2>/dev/null || :
cp -r dist/* .deploy/publish/
rm -rf .deploy/publish/netlify.toml
cp -r .netlify/* .deploy/
cp -r template .deploy/functions/ssr/
rm -rf .deploy/functions/ssr/template/public
rm -rf .deploy/functions/ssr/template/scss
cp -r content .deploy/functions/ssr/
cp .bundles.json .deploy/functions/ssr/bundles.json
mkdir -p .deploy/functions/ssr/template/pages/@ecomplus/storefront-template/template
cp -r node_modules/@ecomplus/widget-* .deploy/functions/ssr/template/pages/@ecomplus/ 2>/dev/null || :
cp -r node_modules/@ecomplus/storefront-template/template/pages .deploy/functions/ssr/template/pages/@ecomplus/storefront-template/template/ || :
- name: Clear functions directory
continue-on-error: true
run: rm -rf .deploy/functions/ssr/template/pages/**/node_modules
- name: Zip Lambda functions
run: |
npx zip-it-and-ship-it .deploy/functions .deploy/functions-zip
rm -rf .deploy/functions
- name: Reset dist to clear Git ref
if: steps.partial_build.outputs.isg == 0
continue-on-error: true
env:
REMOTE_REPO: ${{ steps.remote_repo.outputs.url }}
run: |
git push "${REMOTE_REPO}" --delete dist;
git checkout --orphan dist
git rm -rf .
git commit --no-verify --allow-empty -m "Reset dist [skip ci]"
git push "${REMOTE_REPO}" HEAD:dist --force --follow-tags
git checkout master
- name: Push to branch dist to trigger deploy
uses: JamesIves/github-pages-deploy-action@releases/v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: dist
FOLDER: .deploy
CLEAN: true
- name: Cache output force update
if: steps.new_isg_state.outputs.rand != steps.isg_state.outputs.rand
continue-on-error: true
uses: actions/cache@v4
with:
path: .deploy/publish
key: ${{ hashFiles('.bundles.json') }}-${{ hashFiles('template/p*/**') }}-${{ hashFiles('content/**') }}-${{ steps.new_isg_state.outputs.rand }}