forked from siddharthkp/ci-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (116 loc) · 5.82 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
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_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
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
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
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_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
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
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
ci = 'github_actions'
} 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:''
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.CI) {
// Generic variables for docker images, custom CI builds, etc.
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
branch = process.env.CI_BRANCH
ci = 'custom'
}
module.exports = { repo, sha, event, commit_message, branch, pull_request_number, ci, platform, jobUrl, buildUrl }