-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
226 lines (209 loc) · 8.83 KB
/
index.js
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
let drone = require('./utils/drone');
// platform denotes code hosting provider i.e github, gitlab, bitbucket etc.
// Had to introduce this variable as there are cases when CI is run on the same platform where code is hosted as those cases need to be handled differently.
// Default value is github
let platform = 'github';
let repo,
sha,
event,
commit_message,
pull_request_target_branch,
pull_request_number,
branch,
ci,
jobUrl,
buildUrl;
if (process.env.TRAVIS) {
// Reference: https://docs.travis-ci.com/user/environment-variables
repo = process.env.TRAVIS_REPO_SLUG;
sha = process.env.TRAVIS_PULL_REQUEST_SHA || process.env.TRAVIS_COMMIT;
event = process.env.TRAVIS_EVENT_TYPE;
commit_message = process.env.TRAVIS_COMMIT_MESSAGE;
pull_request_number = process.env.TRAVIS_PULL_REQUEST;
jobUrl = `https://travis-ci.org/${repo}/jobs/${process.env.TRAVIS_JOB_ID}`;
buildUrl = `https://travis-ci.org/${repo}/builds/${process.env.TRAVIS_JOB_ID}`;
branch =
process.env.TRAVIS_EVENT_TYPE === 'push'
? process.env.TRAVIS_BRANCH
: process.env.TRAVIS_PULL_REQUEST_BRANCH;
pull_request_target_branch =
process.env.TRAVIS_EVENT_TYPE === 'push'
? ''
: process.env.TRAVIS_BRANCH;
ci = 'travis';
} else if (process.env.CIRCLECI) {
// Reference: https://circleci.com/docs/1.0/environment-variables
repo =
process.env.CIRCLE_PROJECT_USERNAME +
'/' +
process.env.CIRCLE_PROJECT_REPONAME;
sha = process.env.CIRCLE_SHA1;
event = 'push';
commit_message = ''; // circle does not expose commit message
if (process.env.CI_PULL_REQUEST) {
pull_request_number = process.env.CI_PULL_REQUEST.split('/').pop(); // take number from returns url
event = 'pull_request';
} else pull_request_number = '';
branch = process.env.CIRCLE_BRANCH;
ci = 'circle';
} else if (process.env.WERCKER) {
// Reference: https://devcenter.wercker.com/docs/environment-variables/available-env-vars
repo =
process.env.WERCKER_GIT_OWNER + '/' + process.env.WERCKER_GIT_REPOSITORY;
sha = process.env.WERCKER_GIT_COMMIT;
event = 'push';
commit_message = ''; // wercker does not expose commit message
pull_request_number = ''; // wercker does not expose pull request number
pull_request_target_branch = ''; // wercker does not expose pr target branch
branch = process.env.WERCKER_GIT_BRANCH;
ci = 'wercker';
} else if (process.env.DRONE) {
// Reference: http://readme.drone.io/usage/environment-reference
repo =
process.env.DRONE_REPO ||
process.env.CI_REPO ||
drone.getLegacyRepo(process.env);
sha = process.env.DRONE_COMMIT || process.env.CI_COMMIT;
// DRONE_BUILD_EVENT available in drone > v0.5
// DRONE_EVENT, CI_EVENT available in drone < v0.5
// no EVENT available in drone < v0.4
event =
process.env.DRONE_BUILD_EVENT ||
process.env.DRONE_EVENT ||
process.env.CI_EVENT ||
'push';
commit_message = ''; // drone does not expose commit message
pull_request_number = process.env.DRONE_PULL_REQUEST;
pull_request_target_branch = process.env.DRONE_TARGET_BRANCH;
branch = process.env.DRONE_BRANCH || process.env.CI_BRANCH;
ci = 'drone';
} else if (process.env.GITLAB_CI) {
// Reference: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
// except buildUrl we get all the other variables for gitlab CI
repo = process.env.CI_PROJECT_PATH;
branch = process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || process.env.CI_COMMIT_REF_NAME;
commit_message = process.env.CI_COMMIT_MESSAGE;
pull_request_number = process.env.CI_MERGE_REQUEST_ID || ''; // no pull request numnber in case the CI is run for the branch without a pull request
pull_request_target_branch = process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME || process.env.CI_EXTERNAL_PULL_REQUEST_TARGET_BRANCH_NAME;
sha = process.env.CI_COMMIT_SHA;
event = process.env.CI_PIPELINE_SOURCE;
jobUrl = process.env.CI_JOB_URL;
platform = 'gitlab';
ci = 'gitlab';
} else if (process.env.CI_NAME === 'codeship') {
// Reference: https://documentation.codeship.com/basic/builds-and-configuration/set-environment-variables/#default-environment-variables
repo = process.env.CI_REPO_NAME;
branch = process.env.CI_BRANCH;
commit_message = process.env.CI_COMMIT_MESSAGE || process.env.CI_MESSAGE;
event = 'push';
pull_request_number = process.env.CI_PR_NUMBER;
pull_request_target_branch = ''; // codeship does not export pr target branch
(sha = process.env.CI_COMMIT_ID), (buildUrl = process.env.CI_BUILD_URL);
ci = 'codeship';
} else if (process.env.GITHUB_ACTION) {
// GitHub Actions
// Reference: https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/
// for pull_request event, GITHUB_REF is of the form refs/pull/<pull_request_number>/merge
// for push event, GITHUB_REF is of the form refs/heads/<branch>
const pull_request_numberORbranch = process.env.GITHUB_REF.split('/')[2];
repo = process.env.GITHUB_REPOSITORY;
sha = process.env.GITHUB_SHA;
event = process.env.GITHUB_EVENT_NAME;
commit_message = '';
pull_request_number =
event === 'pull_request' ? pull_request_numberORbranch : '';
// GITHUB_HEAD_REF for pull requests. For commits, GITHUB_REF is of the form refs/heads/master, for example
branch =
event === 'pull_request'
? process.env.GITHUB_HEAD_REF
: pull_request_numberORbranch;
// GITHUB_BASE_REF for pull requests, otherwise GITHUB_BASE_REF is empty
pull_request_target_branch = process.env.GITHUB_BASE_REF;
ci = 'github_actions';
} else if (process.env.CF_PAGES) {
// Reference: https://developers.cloudflare.com/pages/platform/build-configuration#environment-variables
repo = '';
event = 'push';
pull_request_number = '';
pull_request_target_branch = '';
sha = process.env.CF_PAGES_COMMIT_SHA;
branch = process.env.CF_PAGES_BRANCH;
ci = 'cloudflare';
} else if (process.env.NETLIFY) {
// Reference: https://www.netlify.com/docs/continuous-deployment/#environment-variables
repo = process.env.REPOSITORY_URL.split('@github.com/').pop();
event = process.env.PULL_REQUEST ? 'pull_request' : 'push';
pull_request_number = process.env.PULL_REQUEST ? process.env.REVIEW_ID : '';
pull_request_target_branch = ''; // netlify does not export pr target branch
sha = process.env.COMMIT_REF;
branch = process.env.HEAD;
ci = 'netlify';
} else if (process.env.NOW_GITHUB_ORG) {
// Reference: https://zeit.co/docs/v2/advanced/now-for-github/
repo = process.env.NOW_GITHUB_ORG + '/' + process.env.NOW_GITHUB_REPO;
event = 'push';
pull_request_number = '';
sha = process.env.NOW_GITHUB_COMMIT_SHA;
branch = process.env.NOW_GITHUB_COMMIT_REF;
ci = 'now';
} else if(process.env.BUDDY) {
repo = process.env.BUDDY_PROJECT_NAME;
branch = process.env.BUDDY_EXECUTION_BRANCH;
commit_message = process.env.BUDDY_EXECUTION_REVISION_MESSAGE;
pull_request_number = process.env.BUDDY_EXECUTION_PULL_REQUEST_NO;
pull_request_target_branch = process.env.BUDDY_EXECUTION_PULL_REQUEST_BASE_BRANCH;
sha = process.env.BUDDY_EXECUTION_REVISION;
event = process.env.BUDDY_EXECUTION_MODE;
jobUrl = process.env.BUDDY_EXECUTION_REVISION_URL;
platform = 'buddy';
ci = 'buddy';
} else if(process.env.CF_BUILD_URL) {
// Reference: https://codefresh.io/docs/docs/codefresh-yaml/variables/#system-provided-variables
repo = process.env.CF_REPO_NAME;
branch = process.env.CF_BRANCH;
commit_message = process.env.CF_COMMIT_MESSAGE;
pull_request_number = process.env.CF_PULL_REQUEST_NUMBER;
pull_request_target_branch = process.env.CF_PULL_REQUEST_TARGET;
sha = process.env.CF_REVISION;
event = 'push';
buildUrl = process.env.CF_BUILD_URL;
platform = 'codefresh';
ci = 'codefresh';
} else if (process.env.CI === 'woodpecker') {
// Reference: https://woodpecker-ci.org/docs/usage/environment#built-in-environment-variables
repo = process.env.CI_REPO;
sha = process.env.CI_COMMIT_SHA;
event = process.env.CI_BUILD_EVENT;
commit_message = process.env.CI_COMMIT_MESSAGE;
pull_request_number = process.env.CI_COMMIT_PULL_REQUEST || '';
pull_request_target_branch = process.env.CI_COMMIT_TARGET_BRANCH;
branch = process.env.CI_COMMIT_BRANCH;
buildUrl = process.env.CI_BUILD_LINK;
jobUrl = process.env.CI_BUILD_LINK;
platform = 'woodpecker';
ci = 'woodpecker';
} else if (process.env.CI) {
// Generic variables for docker images, custom CI builds, etc.
platform = process.env.CI_PLATFORM;
repo = process.env.CI_REPO_OWNER + '/' + process.env.CI_REPO_NAME;
sha = process.env.CI_COMMIT_SHA;
event = process.env.CI_EVENT || 'push';
commit_message = process.env.CI_COMMIT_MESSAGE;
pull_request_number = process.env.CI_MERGE_REQUEST_ID;
pull_request_target_branch = process.env.CI_TARGET_BRANCH;
branch = process.env.CI_BRANCH;
ci = process.env.CI;
}
module.exports = {
repo,
sha,
event,
commit_message,
branch,
pull_request_number,
pull_request_target_branch,
ci,
platform,
jobUrl,
buildUrl,
};