-
Notifications
You must be signed in to change notification settings - Fork 7
/
node-env-ci
executable file
·156 lines (140 loc) · 4.86 KB
/
node-env-ci
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
#!/usr/bin/env bash
set -euo pipefail
# Print env-ci's output as CI environment variables.
# See https://github.com/pvdlg/env-ci
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
YP_NPX_ARGS=
YP_NPX_ARGS="${YP_NPX_ARGS} --quiet"
YP_NPX_ARGS="${YP_NPX_ARGS} --package env-ci@5"
function main() {
SCRIPT="$(cat <<EOF
let _ = require('lodash-firecloud');
let envCi = require('env-ci');
let data = envCi();
// pass --yp for compatibility with yplatform (used in Makefile:test-env-ci target)
let ypCompat = process.argv[1] === '--yp';
// as listed on https://github.com/pvdlg/env-ci/blob/master/README.md
let map2yp = {
name: 'YP_CI_NAME',
service: 'YP_CI_PLATFORM',
isCi: 'CI',
branch: 'YP_CI_GIT_BRANCH',
commit: 'YP_CI_GIT_HASH',
tag: 'YP_CI_GIT_TAG',
// NOTE our wording implies 1 pipeline -> n jobs
// env-ci's wording implies 1 job -> n builds
build: 'YP_CI_JOB_ID',
buildUrl: 'YP_CI_JOB_URL',
job: 'YP_CI_PIPELINE_ID',
jobUrl: 'YP_CI_PIPELINE_URL',
isPr: 'YP_CI_IS_PR',
pr: 'YP_CI_PR_NUMBER',
prBranch: 'YP_CI_PR_GIT_BRANCH',
slug: 'YP_CI_REPO_SLUG',
root: 'YP_CI_ROOT'
};
let knownKeys = _.keys(map2yp);
let unknownKeys = _.keys(_.omit(data, ...knownKeys));
if (!_.isEmpty(unknownKeys)) {
console.error('env-ci has new keys, please update the node-env-ci script:', unknownKeys);
process.exit(1);
}
data = _.omitBy(data, function(_value, key) {
return _.isEmpty(map2yp[key]);
});
data = _.mapKeys(data, function(_value, key) {
return map2yp[key];
});
if (ypCompat) {
// our wording implies 1 pipeline -> n jobs
// env-ci's wording seems to imply 1 job -> n builds
let dataClone = _.clone(data);
switch (data.YP_CI_PLATFORM) {
case 'cirrus':
case 'drone':
case 'github':
case 'gitlab':
case 'travis':
data.YP_CI_JOB_ID = dataClone.YP_CI_PIPELINE_ID;
data.YP_CI_PIPELINE_ID = dataClone.YP_CI_JOB_ID;
data.YP_CI_JOB_URL = dataClone.YP_CI_PIPELINE_URL;
data.YP_CI_PIPELINE_URL = dataClone.YP_CI_JOB_URL;
break;
default:
true;
break;
}
data = _.omitBy(data, _.isUndefined);
// prefer empty instead of false
data = _.mapValues(data, function(value) {
value = _.toString(value);
if (value === 'false') {
return '';
}
return value;
});
if (data.YP_CI_PLATFORM === 'appveyor') {
data.YP_CI_JOB_ID=process.env.APPVEYOR_JOB_ID;
data.YP_CI_PIPELINE_ID=process.env.APPVEYOR_BUILD_NUMBER;
data.YP_CI_JOB_URL=_.join([
'https://ci.appveyor.com/project/',
process.env.APPVEYOR_ACCOUNT_NAME,
'/',
process.env.APPVEYOR_PROJECT_SLUG,
'/build/job/',
process.env.APPVEYOR_JOB_ID
], '');
data.YP_CI_PIPELINE_URL=_.join([
'https://ci.appveyor.com/project/',
process.env.APPVEYOR_ACCOUNT_NAME,
'/',
process.env.APPVEYOR_PROJECT_SLUG,
'/build/',
process.env.APPVEYOR_BUILD_NUMBER
], '');
}
if (data.YP_CI_PLATFORM === 'buddy') {
data.YP_CI_JOB_ID = process.env.BUDDY_EXECUTION_ID;
data.YP_CI_PIPELINE_ID = process.env.BUDDY_PIPELINE_ID;
data.YP_CI_JOB_URL = process.env.BUDDY_EXECUTION_URL;
data.YP_CI_PIPELINE_URL = process.env.BUDDY_PIPELINE_URL;
}
if (data.YP_CI_PLATFORM === 'bitrise') {
data.YP_CI_JOB_ID = process.env.BITRISE_BUILD_NUMBER;
data.YP_CI_PIPELINE_ID = process.env.BITRISE_BUILD_NUMBER;
data.YP_CI_JOB_URL = process.env.BITRISE_BUILD_URL;
data.YP_CI_PIPELINE_URL = process.env.BITRISE_BUILD_URL;
// env-ci reports e.g. 'd4c696b6b4e2be16' (BITRISE_APP_SLUG) instead of the repo slug
delete data.YP_CI_REPO_SLUG;
}
if (data.YP_CI_PLATFORM === 'circleci') {
data.YP_CI_PLATFORM = 'circle';
data.YP_CI_JOB_ID = process.env.CIRCLE_BUILD_NUM;
data.YP_CI_PIPELINE_ID = process.env.CIRCLE_WORKFLOW_ID;
}
if (data.YP_CI_PLATFORM === 'codeship') {
data.YP_CI_NAME = 'codeship';
}
if (data.YP_CI_PLATFORM === 'drone') {
delete data.YP_CI_JOB_URL;
}
if (data.YP_CI_PLATFORM === 'github') {
// env-ci reports e.g. 'refs/pull/239/merge' instead of the branch of the PR
delete data.YP_CI_PR_GIT_BRANCH;
}
if (data.YP_CI_PLATFORM === 'gitlab') {
data.YP_CI_PIPELINE_URL = _.replace(data.YP_CI_PIPELINE_URL, '/pipelines/', '/-/pipelines/');
}
if (data.YP_CI_PLATFORM === 'travis') {
data.YP_CI_JOB_ID = process.env.TRAVIS_JOB_ID;
data.YP_CI_PIPELINE_ID = process.env.TRAVIS_BUILD_NUMBER;
}
}
_.forEach(data, function(value, key) {
console.log(key + "=" + value);
})
EOF
)"
node -e "${SCRIPT}" -- "$@"
}
source ${YP_DIR}/sh/npx.inc.sh