-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.yaml
553 lines (534 loc) · 25.4 KB
/
node.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Inspired from circleci orbs for alpine
display:
home_url: https://nodejs.org/
source_url: https://github.com/circleci-public/node-orb
commands:
install:
description: |
Install custom versions of Node.js, and optionally NPM/Yarn, in any
execution environment (Docker/Linux, macOS, machine) that does not have
it preinstalled.
Recommendation: It is highly recommended to utilize an environment such as Docker with Node preinstalled.
parameters:
install-npm:
default: true
description: Install NPM?
type: boolean
install-yarn:
default: false
description: Install Yarn?
type: boolean
lts:
default: false
description: Install the latest LTS version by default.
type: boolean
node-install-dir:
default: /usr/local
description: |
Where should Node.js be installed?
type: string
node-version:
default: node
description: |
Specify the full version tag to install. To install the latest LTS version, set `lts` to true. The latest (current) version of Node.js will be installed by default. For a full list of releases, see the following: https://nodejs.org/en/download/releases
type: string
npm-version:
default: latest
description: |
Pick a version of NPM to install: https://nodejs.org/en/download/releases
type: string
yarn-version:
default: ""
description: |
Pick a version of Yarn to install (if no version is specified, the latest stable version will be installed): https://github.com/yarnpkg/yarn/releases
type: string
steps:
- run:
command: |
# Only install nvm if it's not already installed
# Nvm not yet working
apk add --no-cache nodejs-current npm
# if command -v nvm &> /dev/null; then
# echo "nvm is already installed. Skipping nvm install.";
# else
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash;
# echo 'export NVM_DIR="$HOME/.nvm"' >> $BASH_ENV;
# echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV;
# source $BASH_ENV;
# fi
# if [ "$NODE_PARAM_LTS" = "1" ]; then
# nvm install --lts
# else
# nvm install "$NODE_PARAM_VERSION"
# nvm alias default "$NODE_PARAM_VERSION"
# echo 'nvm use default &>/dev/null' >> $BASH_ENV
# fi
environment:
NODE_PARAM_LTS: <<parameters.lts>>
NODE_PARAM_VERSION: <<parameters.node-version>>
name: Install Node.js <<^parameters.lts>> <<parameters.node-version>> <</parameters.lts>> <<#parameters.lts>> LTS <</parameters.lts>>
- when:
condition: <<parameters.install-npm>>
steps:
- install-npm:
version: <<parameters.npm-version>>
- when:
condition: <<parameters.install-yarn>>
steps:
- install-yarn:
version: <<parameters.yarn-version>>
install-npm:
description: |
Install/upgrade the NPM package manager to a custom version. Requires an existing installation of Node.js; if none exists, use this orb's `install` command.
parameters:
version:
default: latest
description: |
Pick a version of NPM to install: https://nodejs.org/en/download/releases
type: string
steps:
- run:
command: |
apk add --no-cache npm
npm i -g npm@latest
environment:
NODE_PARAM_NPM_VERSION: <<parameters.version>>
name: Install NPM
install-packages:
description: |
Install your Node packages with automated caching and best practices applied.
parameters:
app-dir:
default: .
description: Path to the directory containing your package.json file. Not needed if package.json lives in the root.
type: string
cache-path:
default: ""
description: |
By default, this orb will utilize 'npm ci' and cache the '~/.npm' directory. Override which path to cache with this parameter.
type: string
cache-version:
default: v1
description: Change the default cache version if you need to clear the cache for any reason.
type: string
include-branch-in-cache-key:
default: true
description: |
If true, this cache bucket will only apply to jobs within the same branch.
type: boolean
override-ci-command:
default: ""
description: |
By default, packages will be installed with "npm ci" or "yarn install --frozen-lockfile".
Optionally supply a custom package installation command, with any additional flags needed.
type: string
pkg-manager:
default: npm
description: Select the default node package manager to use.
enum:
- npm
- yarn
type: enum
with-cache:
default: true
description: Cache your node packages automatically for faster install times.
type: boolean
steps:
- run:
command: |
if [ ! -f "package.json" ]; then
echo
echo "---"
echo "Unable to find your package.json file. Did you forget to set the app-dir parameter?"
echo "---"
echo
echo "Current directory: $(pwd)"
echo
echo
echo "List directory: "
echo
ls
exit 1
fi
name: Checking for package.json.
working_directory: <<parameters.app-dir>>
- when:
condition: << parameters.with-cache >>
steps:
- run:
command: |
if [ -f "package-lock.json" ]; then
echo "Found package-lock.json file, assuming lockfile"
cp package-lock.json /tmp/node-project-lockfile
elif [ -f "npm-shrinkwrap.json" ]; then
echo "Found npm-shrinkwrap.json file, assuming lockfile"
cp npm-shrinkwrap.json /tmp/node-project-lockfile
elif [ -f "yarn.lock" ]; then
echo "Found yarn.lock file, assuming lockfile"
cp yarn.lock /tmp/node-project-lockfile
fi
name: Determine lockfile
working_directory: <<parameters.app-dir>>
- restore_cache:
keys:
- node-deps-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/node-project-lockfile" }}-{{ arch }}
- when:
condition:
equal:
- npm
- << parameters.pkg-manager >>
steps:
- run:
command: |
if [[ ! -z "<< parameters.override-ci-command >>" ]]; then
echo "Running override package installation command:"
<< parameters.override-ci-command >>
else
npm ci
fi
name: Installing NPM packages
working_directory: <<parameters.app-dir>>
- when:
condition: << parameters.with-cache >>
steps:
- when:
condition: << parameters.cache-path >>
steps:
- save_cache:
key: node-deps-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/node-project-lockfile" }}-{{ arch }}
paths:
- <<parameters.cache-path>>
- unless:
condition: << parameters.cache-path >>
steps:
- save_cache:
key: node-deps-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/node-project-lockfile" }}-{{ arch }}
paths:
- ~/.npm
- when:
condition:
equal:
- yarn
- << parameters.pkg-manager >>
steps:
- run:
command: |
if [[ ! -z "<< parameters.override-ci-command >>" ]]; then
echo "Running override package installation command:"
<< parameters.override-ci-command >>
else
yarn install --frozen-lockfile
fi
name: Installing YARN packages
working_directory: <<parameters.app-dir>>
- when:
condition: << parameters.with-cache >>
steps:
- when:
condition: << parameters.cache-path >>
steps:
- save_cache:
key: node-deps-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/node-project-lockfile" }}-{{ arch }}
paths:
- <<parameters.cache-path>>
- unless:
condition: << parameters.cache-path >>
steps:
- save_cache:
key: node-deps-<<parameters.cache-version>>-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/node-project-lockfile" }}-{{ arch }}
paths:
- <<parameters.app-dir>>/node_modules
install-yarn:
description: |
Install a custom version of the Yarn package manager
parameters:
version:
default: ""
description: |
Pick a version of Yarn to install (if no version is specified, the latest stable version will be installed): https://github.com/yarnpkg/yarn/releases
type: string
steps:
- run:
command: |
if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi
# FUNCTIONS
get_yarn_version () {
if [[ "$NODE_PARAM_YARN_VERSION" == "" ]]; then
YARN_ORB_VERSION=$(curl -Ls -o /dev/null -w %{url_effective} \
"https://github.com/yarnpkg/yarn/releases/latest" | sed 's:.*/::' | cut -d 'v' -f 2 | cut -d 'v' -f 2)
echo "Latest version of Yarn is $YARN_ORB_VERSION"
else
YARN_ORB_VERSION="$NODE_PARAM_YARN_VERSION"
echo "Selected version of Yarn is $YARN_ORB_VERSION"
fi
}
installation_check () {
echo "Checking if YARN is already installed..."
if command -v yarn > /dev/null 2>&1; then
if yarn --version | grep "$YARN_ORB_VERSION" > /dev/null 2>&1; then
echo "Yarn $YARN_ORB_VERSION is already installed"
exit 0
else
echo "A different version of Yarn is installed ($(yarn --version)); removing it"
if uname -a | grep Darwin > /dev/null 2>&1; then
brew uninstall yarn > /dev/null 2>&1
elif cat /etc/issue | grep Alpine > /dev/null 2>&1; then
apk del yarn > /dev/null 2>&1
elif cat /etc/issue | grep Debian > /dev/null 2>&1; then
$SUDO apt-get remove yarn > /dev/null 2>&1 && \
$SUDO apt-get purge yarn > /dev/null 2>&1
elif cat /etc/issue | grep Ubuntu > /dev/null 2>&1; then
$SUDO apt-get remove yarn > /dev/null 2>&1 && \
$SUDO apt-get purge yarn > /dev/null 2>&1
elif command -v yum > /dev/null 2>&1; then
yum remove yarn > /dev/null 2>&1
fi
$SUDO rm -rf "$HOME/.yarn" > /dev/null 2>&1
$SUDO rm -f /usr/local/bin/yarn /usr/local/bin/yarnpkg > /dev/null 2>&1
fi
fi
}
get_yarn_version
installation_check
# install yarn
echo "Installing YARN v$YARN_ORB_VERSION"
curl -L -o yarn.tar.gz --silent "https://yarnpkg.com/downloads/$YARN_ORB_VERSION/yarn-v$YARN_ORB_VERSION.tar.gz"
$SUDO tar -xzf yarn.tar.gz && rm yarn.tar.gz
$SUDO mkdir -p /opt/yarn
$SUDO mv yarn-v${YARN_ORB_VERSION}/* /opt/yarn
$SUDO rm -rf "yarn-v${YARN_ORB_VERSION}"
$SUDO chmod 777 "/opt/yarn"
$SUDO ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn
$SUDO ln -s /opt/yarn/bin/yarnpkg /usr/local/bin/yarnpkg
$SUDO ln -s /opt/yarn/bin/yarn.js /usr/local/bin/yarn.js
$SUDO mkdir -p ~/.config
if uname -a | grep Darwin; then
$SUDO chown -R "$USER:$GROUP" ~/.config
$SUDO chown -R "$USER:$GROUP" /opt/yarn
else
$SUDO chown -R "$(whoami):$(whoami)" /opt/yarn
$SUDO chown -R "$(whoami):$(whoami)" ~/.config
fi
# test/verify version
echo "Verifying YARN install"
if yarn --version | grep "$YARN_ORB_VERSION" > /dev/null 2>&1; then
echo "Success! Yarn $(yarn --version) has been installed to $(which yarn)"
else
echo "Something went wrong; the specified version of Yarn could not be installed"
exit 1
fi
environment:
NODE_PARAM_YARN_VERSION: <<parameters.version>>
name: Install Yarn
executors:
default:
description: |
Select the version of Node.js to use. Uses CircleCI's highly cached convenience images built for CI.
Any available tag from this list can be used: https://hub.docker.com/r/cimg/node/tags
docker:
- image: cimg/node:<<parameters.tag>>
parameters:
tag:
default: "13.11"
description: |
Pick a specific cimg/node image version tag: https://hub.docker.com/r/cimg/node
type: string
jobs:
test:
description: |
Simple drop-in job to test your Node.js application automatically.
executor:
name: default
tag: << parameters.version >>
parameters:
app-dir:
default: ~/project
description: Path to the directory containing your package.json file. Not needed if package.json lives in the root.
type: string
cache-version:
default: v1
description: Change the default cache version if you need to clear the cache for any reason.
type: string
override-ci-command:
default: ""
description: |
By default, packages will be installed with "npm ci" or "yarn install --frozen-lockfile".
Optionally supply a custom package installation command, with any additional flags needed.
type: string
pkg-manager:
default: npm
description: Select the default node package manager to use.
enum:
- npm
- yarn
type: enum
run-command:
default: test
description: The name of the script within your package.json which will run your tests.
type: string
setup:
default: []
description: Provide any optional steps you would like to run prior to installing the node dependencies. This is a good place to install global modules.
type: steps
version:
default: 16.15.1
description: |
A full version tag must be specified. Example: "16.15.1" For a full list of releases, see the following: https://nodejs.org/en/download/releases
type: string
steps:
- checkout
- steps: << parameters.setup >>
- install-packages:
app-dir: <<parameters.app-dir>>
cache-version: <<parameters.cache-version>>
override-ci-command: <<parameters.override-ci-command>>
pkg-manager: <<parameters.pkg-manager>>
- when:
condition:
equal:
- npm
- << parameters.pkg-manager >>
steps:
- run:
command: npm run <<parameters.run-command>>
name: Run NPM Tests
working_directory: <<parameters.app-dir>>
- when:
condition:
equal:
- yarn
- << parameters.pkg-manager >>
steps:
- run:
command: yarn run <<parameters.run-command>>
name: Run YARN Tests
working_directory: <<parameters.app-dir>>
examples:
install_nodejs:
description: |
Download and install any version of Node.js. Optionally installs Yarn with a parameter. You may use the default executor of this orb for a Node execution environment as well.
Recommendation: It is highly recommended to utilize an environment such as Docker with Node preinstalled.
usage:
version: "2.1"
orbs:
node: circleci/[email protected]
jobs:
install-node-example:
docker:
- image: cimg/base:stable
steps:
- checkout
- node/install:
install-yarn: true
- run: node --version
workflows:
test_my_app:
jobs:
- install-node-example
node_test_and_deploy:
description: |
Drop-in solution to automatically test your Node.js applications. This job will automatically download your code into any version node environment, install your dependencies with caching enabled, and execute your testing script.
This example shows how easily with two orbs you can test your Node application and automatically deploy to Heroku when successful, and on the master branch.
usage:
version: "2.1"
orbs:
heroku: circleci/[email protected]
node: circleci/[email protected]
workflows:
test-and-deploy:
jobs:
- node/test
- heroku/deploy-via-git:
filters:
branches:
only:
- master
requires:
- node/test
npm_install:
description: |
By default, the "install-packages" command will attempt to utilize "npm ci". The command can however be overwriten to support an alternative package install command such as "npm install".
usage:
version: "2.1"
orbs:
node: circleci/[email protected]
jobs:
test:
executor: node/default
steps:
- checkout
- node/install-packages:
cache-path: ~/project/node_modules
override-ci-command: npm install
- run: npm run test
workflows:
test_my_app:
jobs:
- test
run_matrix_testing:
description: |
Easily test your application against multiple versions of Node.js in just a few lines. This example uses "Matrix" jobs to call the same node/test job multiple times at once, each eith the Node version we have specified.
Read more here: https://circleci.com/blog/you-are-what-you-git-how-your-vcs-branching-model-affects-your-delivery-cadence/
usage:
version: "2.1"
orbs:
node: circleci/[email protected]
workflows:
matrix-tests:
jobs:
- node/test:
matrix:
parameters:
version:
- 13.11.0
- 12.16.0
- 10.19.0
run_tests_with_npm:
description: |
How to quickly and easily run your Node.js tests on CircleCI with caching automatically included. This example assumes you are using NPM as your package manager.
The "npm ci" command will automatically be applied and the "~/.npm" directory cached.
usage:
version: "2.1"
orbs:
node: circleci/[email protected]
jobs:
test:
executor:
name: node/default
tag: "13.14"
steps:
- checkout
- node/install-packages
- run:
command: npm run test
workflows:
test_my_app:
jobs:
- test
run_tests_with_yarn:
description: |
Utilize the YARN package manager with the CircleCI Node orb. Caching of your Node packages is enabled by default.
usage:
version: "2.1"
orbs:
node: circleci/[email protected]
jobs:
test:
executor:
name: node/default
tag: "13"
steps:
- checkout
- node/install-packages:
pkg-manager: yarn
- run:
command: yarn run test
name: Run YARN tests
workflows:
test_my_app:
jobs:
- test