Skip to content

Commit 4e627d4

Browse files
JustinBeckwithalexander-fenster
authored andcommitted
Retry npm install in CI (#70)
1 parent 2838bfe commit 4e627d4

File tree

2 files changed

+141
-11
lines changed

2 files changed

+141
-11
lines changed

packages/google-cloud-oslogin/.circleci/config.yml

+81-11
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@ workflows:
2323
- node8
2424
- node10
2525
filters: *all_commits
26-
- publish_npm:
26+
- system_tests:
27+
requires:
28+
- lint
29+
- docs
30+
filters: &master_and_releases
31+
branches:
32+
only: master
33+
tags: &releases
34+
only: '/^v[\d.]+$/'
35+
- sample_tests:
2736
requires:
2837
- lint
2938
- docs
39+
filters: *master_and_releases
40+
- publish_npm:
41+
requires:
42+
- system_tests
43+
- sample_tests
3044
filters:
3145
branches:
3246
ignore: /.*/
33-
tags:
34-
only: '/^v[\d.]+$/'
47+
tags: *releases
3548
nightly:
3649
triggers:
3750
- schedule:
@@ -46,17 +59,17 @@ jobs:
4659
- image: 'node:6'
4760
user: node
4861
steps: &unit_tests_steps
49-
- checkout
62+
- checkout
5063
- run: &npm_install_and_link
5164
name: Install and link the module
5265
command: |-
5366
mkdir -p /home/node/.npm-global
54-
npm install
67+
./.circleci/npm-install-retry.js
5568
environment:
5669
NPM_CONFIG_PREFIX: /home/node/.npm-global
5770
- run: npm test
5871
- run: node_modules/.bin/codecov
59-
72+
6073
node8:
6174
docker:
6275
- image: 'node:8'
@@ -74,12 +87,12 @@ jobs:
7487
steps:
7588
- checkout
7689
- run: *npm_install_and_link
77-
- run:
90+
- run: &samples_npm_install_and_link
7891
name: Link the module being tested to the samples.
7992
command: |
8093
cd samples/
81-
npm install
8294
npm link ../
95+
./../.circleci/npm-install-retry.js
8396
environment:
8497
NPM_CONFIG_PREFIX: /home/node/.npm-global
8598
- run:
@@ -94,14 +107,71 @@ jobs:
94107
steps:
95108
- checkout
96109
- run: *npm_install_and_link
110+
- run: npm run docs
111+
sample_tests:
112+
docker:
113+
- image: 'node:8'
114+
user: node
115+
steps:
116+
- checkout
97117
- run:
98-
name: Build documentation.
99-
command: npm run docs
118+
name: Decrypt credentials.
119+
command: |
120+
if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
121+
openssl aes-256-cbc -d -in .circleci/key.json.enc \
122+
-out .circleci/key.json \
123+
-k "${SYSTEM_TESTS_ENCRYPTION_KEY}"
124+
fi
125+
- run: *npm_install_and_link
126+
- run: *samples_npm_install_and_link
127+
- run:
128+
name: Run sample tests.
129+
command: npm run samples-test
130+
environment:
131+
GCLOUD_PROJECT: long-door-651
132+
GOOGLE_APPLICATION_CREDENTIALS: /home/node/samples/.circleci/key.json
133+
NPM_CONFIG_PREFIX: /home/node/.npm-global
134+
- run:
135+
name: Remove unencrypted key.
136+
command: |
137+
if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
138+
rm .circleci/key.json
139+
fi
140+
when: always
141+
working_directory: /home/node/samples/
142+
system_tests:
143+
docker:
144+
- image: 'node:8'
145+
user: node
146+
steps:
147+
- checkout
148+
- run:
149+
name: Decrypt credentials.
150+
command: |
151+
if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
152+
openssl aes-256-cbc -d -in .circleci/key.json.enc \
153+
-out .circleci/key.json \
154+
-k "${SYSTEM_TESTS_ENCRYPTION_KEY}"
155+
fi
156+
- run: *npm_install_and_link
157+
- run:
158+
name: Run system tests.
159+
command: npm run system-test
160+
environment:
161+
GOOGLE_APPLICATION_CREDENTIALS: .circleci/key.json
162+
- run:
163+
name: Remove unencrypted key.
164+
command: |
165+
if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
166+
rm .circleci/key.json
167+
fi
168+
when: always
100169
publish_npm:
101170
docker:
102171
- image: 'node:8'
103172
user: node
104173
steps:
105174
- checkout
106-
- run: 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc'
175+
- run: ./.circleci/npm-install-retry.js
176+
- run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
107177
- run: npm publish --access=public
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
3+
let spawn = require('child_process').spawn;
4+
5+
//
6+
//USE: ./index.js <ms npm can be idle> <number of attempts> [... NPM ARGS]
7+
//
8+
9+
let timeout = process.argv[2] || 60000;
10+
let attempts = process.argv[3] || 3;
11+
let args = process.argv.slice(4);
12+
if (args.length === 0) {
13+
args = ['install'];
14+
}
15+
16+
(function npm() {
17+
let timer;
18+
args.push('--verbose');
19+
let proc = spawn('npm', args);
20+
proc.stdout.pipe(process.stdout);
21+
proc.stderr.pipe(process.stderr);
22+
proc.stdin.end();
23+
proc.stdout.on('data', () => {
24+
setTimer();
25+
});
26+
proc.stderr.on('data', () => {
27+
setTimer();
28+
});
29+
30+
// side effect: this also restarts when npm exits with a bad code even if it
31+
// didnt timeout
32+
proc.on('close', (code, signal) => {
33+
clearTimeout(timer);
34+
if (code || signal) {
35+
console.log('[npm-are-you-sleeping] npm exited with code ' + code + '');
36+
37+
if (--attempts) {
38+
console.log('[npm-are-you-sleeping] restarting');
39+
npm();
40+
} else {
41+
console.log('[npm-are-you-sleeping] i tried lots of times. giving up.');
42+
throw new Error("npm install fails");
43+
}
44+
}
45+
});
46+
47+
function setTimer() {
48+
clearTimeout(timer);
49+
timer = setTimeout(() => {
50+
console.log('[npm-are-you-sleeping] killing npm with SIGTERM');
51+
proc.kill('SIGTERM');
52+
// wait a couple seconds
53+
timer = setTimeout(() => {
54+
// its it's still not closed sigkill
55+
console.log('[npm-are-you-sleeping] killing npm with SIGKILL');
56+
proc.kill('SIGKILL');
57+
}, 2000);
58+
}, timeout);
59+
}
60+
})();

0 commit comments

Comments
 (0)