-
Notifications
You must be signed in to change notification settings - Fork 23
249 lines (233 loc) · 9.9 KB
/
build.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
name: Build & Deploy Cookbook
on:
push:
branches:
- develop
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
pull_request:
defaults:
run:
shell: bash
jobs:
pre_build:
name: Check if build is needed
runs-on: ubuntu-latest
outputs:
#Build on PRs, PR's merge to default branch, new releases & changes not including 'libs' (which gets build as part of new release)
do_build: ${{ github.event_name == 'pull_request' || github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/v') || steps.filter.outputs.libs == 'false' }}
node_version: ${{ steps.get_node_version.outputs.node_version }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Check modified files
id: filter
uses: dorny/paths-filter@v2.9.2
with:
filters: |
libs:
- 'libs/**'
- name: Get Node.JS version from package.json
id: get_node_version
run: echo ::set-output name=node_version::$(jq -r .engines.node ./package.json)
lint_and_unittest:
name: Lint & unit test
needs: pre_build
if: ${{ needs.pre_build.outputs.do_build == 'true' }}
runs-on: ubuntu-latest
env:
NODE_VERSION: ${{ needs.pre_build.outputs.node_version }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Kirby setup
uses: ./.github/actions/kirby-setup
- name: Run linting
run: npx nx run-many -t lint && npx stylelint '**/*.scss'
- name: Run tests
run: npx nx run-many -t test --output-style=stream -- --browsers=ChromeCustom --progress false --watch false --code-coverage
build_kirby:
name: Build Kirby lib
needs: pre_build
if: ${{ needs.pre_build.outputs.do_build == 'true' }}
runs-on: ubuntu-latest
env:
NODE_VERSION: ${{ needs.pre_build.outputs.node_version }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Kirby setup
uses: ./.github/actions/kirby-setup
- name: Build Kirby lib
run: npx nx build designsystem
build_kirby_cookbook:
name: Build Kirby Cookbook
needs: pre_build
if: ${{ needs.pre_build.outputs.do_build == 'true' }}
runs-on: ubuntu-latest
env:
NODE_VERSION: ${{ needs.pre_build.outputs.node_version }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Kirby setup
uses: ./.github/actions/kirby-setup
- name: Build Kirby Cookbook
run: npx nx build cookbook --configuration=production
- name: Upload Kirby Cookbook dist files
uses: actions/cache@v2
with:
path: |
dist/
key: cookbook-dist-${{github.run_id}}
deploy_to_cookbook:
name: Deploy cookbook
needs: [lint_and_unittest, build_kirby, build_kirby_cookbook]
runs-on: ubuntu-latest
env:
DOMAIN: 'kirby.design'
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Get deployment name for default or feature branch
if: "github.event_name == 'pull_request' || github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/v')"
run: |
branchname="${{ github.head_ref || github.ref }}"
echo "Github branchname: ${branchname}"
branchname="${branchname##*/}"
echo "Removed leading paths - branchname: ${branchname}"
branchname=$(tr [:upper:] [:lower:] <<< "$branchname")
echo "Converted to lowercase - branchname: ${branchname}"
branchname="${branchname//[^a-zA-Z0-9]/-}"
echo "Replaced illegal chars - branchname: ${branchname}"
branchname=$(tr -s '-' '-' <<< "$branchname")
echo "Removed duplicated dashes - branchname: ${branchname}"
branchname="${branchname%%-}"
echo "Removed trailing dash - final branchname: ${branchname}"
releasename="kby-${branchname}"
releasename=$(cut -c 1-53 <<< "$releasename")
echo "Releasename: ${releasename}"
echo "GH_DEPLOY_ENVIRONMENT=pr-${branchname}" >> $GITHUB_ENV
echo "RELEASENAME=${releasename}" >> $GITHUB_ENV
- name: Get deployment name for release branch
if: "github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')"
run: |
echo "GH_DEPLOY_ENVIRONMENT=production" >> $GITHUB_ENV
echo "RELEASENAME=designsystem" >> $GITHUB_ENV
- name: Get hostname for default or feature branch
if: "github.event_name != 'push' || !startsWith(github.ref, 'refs/tags/v')"
run: |
chmod +x ./scripts/dns.pl
hostname=$(perl ./scripts/dns.pl $DOMAIN "${RELEASENAME}")
echo "HOSTNAME=${hostname}" >> $GITHUB_ENV
- name: Get hostname for release branch
if: "github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')"
run: echo "HOSTNAME=cookbook" >> $GITHUB_ENV
- name: Create Github Deployment
id: create_deployment
uses: octokit/request-action@v2.x
with:
route: POST /repos/:repository/deployments
repository: ${{ github.repository }}
ref: ${{ github.event_name == 'pull_request' && format('refs/heads/{0}', github.head_ref) || github.ref }}
environment: ${{ env.GH_DEPLOY_ENVIRONMENT }}
production_environment: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }}
auto_merge: false
required_contexts: '[]' # Skip commit checks
mediaType: '{"previews": ["flash", "ant-man"]}' # some parameters need preview: https://docs.github.com/en/rest/reference/repos#list-deployment-statuses-preview-notices
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set deployment status to in progress
uses: octokit/request-action@v2.x
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
state: in_progress
mediaType: '{"previews": ["flash", "ant-man"]}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download Kirby Cookbook dist files
uses: actions/cache@v2
with:
path: |
dist/
key: cookbook-dist-${{github.run_id}}
- name: Deploy cookbook to GitHub pages
run: |
set -e
echo "Hostname: ${HOSTNAME}"
git clone https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/kirbydesign/designsystem.git -b gh-pages designsystem_web
git config --global user.name 'DRB-bot'
git config --global user.email 'dbrr00@bankdata.dk'
mkdir -p designsystem_web/branch
if [ $HOSTNAME == "cookbook" ]; then
sed -i -E "s,<base href=\"[^\"]*\".*>,<base href=\"/\">,g" dist/apps/cookbook/index.html
cp -R dist/apps/cookbook/* designsystem_web/
cp designsystem_web/index.html designsystem_web/404.html
else
mkdir -p designsystem_web/branch/$HOSTNAME
sed -i -E "s,<base href=\"[^\"]*\".*>,<base href=\"/branch/$HOSTNAME/\">,g" dist/apps/cookbook/index.html
cp -R dist/apps/cookbook/* designsystem_web/branch/$HOSTNAME
cp designsystem_web/branch/$HOSTNAME/index.html designsystem_web/branch/$HOSTNAME/404.html
fi
cd designsystem_web/branch/
folders=$(ls -d */)
cd ../
branches=$(git branch -r)
#find deployment names for all branches
branchList=""
for branch in $branches; do
name="${branch##*/}"
name=$(tr [:upper:] [:lower:] <<< "$name")
name="${name//[^a-zA-Z0-9]/-}"
name=$(tr -s '-' '-' <<< "$name")
name="${name%%-}"
name="kby-${name}"
name=$(cut -c 1-53 <<< "$name")
branchList="$branchList $name"
done
#delete deployments related to closed branches
for item in $folders
do
echo "Verify branch ${item::-1} exist"
if ! grep -q "${item::-1}" <<< "$branchList"; then
echo "Deleting branch folder ${item::-1} (branch closed)"
rm -Rf branch/${item::-1}
fi
done
git add .
if ! git diff-index --quiet HEAD; then
git commit -m "Release: $RELEASENAME"
git push
fi
shell: bash
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set deployment status to success
id: successful_deployment
uses: octokit/request-action@v2.x
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
environment_url: ${{ env.HOSTNAME == 'cookbook' && format('https://cookbook.{0}', env.DOMAIN) || format('https://cookbook.{0}/branch/{1}/index.html', env.DOMAIN, env.HOSTNAME) }}
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
state: success
mediaType: '{"previews": ["ant-man"]}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set deployment status to failure
id: failed_deployment
uses: octokit/request-action@v2.x
if: failure()
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }}
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
state: failure
mediaType: '{"previews": ["ant-man"]}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}