Skip to content

Commit 24db072

Browse files
Merge pull request #10 from swapnilmishra/master
gitlab CI support
2 parents 0464652 + 68762c8 commit 24db072

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

.gitlab-ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
stages:
2+
- test
3+
4+
test:
5+
stage: test
6+
image: node:8
7+
script:
8+
- npm i --silent
9+
- npm test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
 
99

10-
Supports travis, circle, wercker, drone, codeship, now(zeit), netlify and GitHub Actions.
10+
Supports travis, circle, gitlab, wercker, drone, codeship, now(zeit), netlify and GitHub Actions.
1111

1212
Kinda supports custom CI as well. [Specs here](https://github.com/siddharthkp/ci-env/blob/master/index.js#L68-L79)
1313

index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
let drone = require('./utils/drone')
2+
// platform denotes code hosting provider i.e github, gitlab, bitbucket etc.
3+
// 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.
4+
// Default value is github
5+
let platform = 'github';
26
let repo, sha, event, commit_message, pull_request_number, branch, ci, jobUrl, buildUrl
37

48
if (process.env.TRAVIS) {
@@ -56,6 +60,18 @@ if (process.env.TRAVIS) {
5660
pull_request_number = process.env.DRONE_PULL_REQUEST
5761
branch = process.env.DRONE_BRANCH || process.env.CI_BRANCH
5862
ci = 'drone'
63+
} else if (process.env.GITLAB_CI){
64+
// Reference: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
65+
// except buildUrl we get all the other variables for gitlab CI
66+
repo = process.env.CI_PROJECT_PATH
67+
branch = process.env.CI_COMMIT_REF_NAME
68+
commit_message = process.env.CI_COMMIT_MESSAGE
69+
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
70+
sha=process.env.CI_COMMIT_SHA
71+
event = process.env.CI_PIPELINE_SOURCE
72+
jobUrl = process.env.CI_JOB_URL
73+
platform = 'gitlab'
74+
ci = 'gitlab'
5975
} else if (process.env.CI_NAME === 'codeship') {
6076
// Reference: https://documentation.codeship.com/basic/builds-and-configuration/set-environment-variables/#default-environment-variables
6177

@@ -110,9 +126,9 @@ if (process.env.TRAVIS) {
110126
sha = process.env.CI_COMMIT_SHA
111127
event = process.env.CI_EVENT || 'push'
112128
commit_message = process.env.CI_COMMIT_MESSAGE
113-
pull_request_number = process.env.CI_PULL_REQUEST_NUMBER
129+
pull_request_number = process.env.CI_MERGE_REQUEST_ID
114130
branch = process.env.CI_BRANCH
115131
ci = 'custom'
116132
}
117133

118-
module.exports = { repo, sha, event, commit_message, branch, pull_request_number, ci, jobUrl, buildUrl }
134+
module.exports = { repo, sha, event, commit_message, branch, pull_request_number, ci, platform, jobUrl, buildUrl }

test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const {
99
commit_message,
1010
pull_request_number,
1111
branch,
12-
ci
12+
ci,
13+
platform
1314
} = require("./index");
1415

1516
if (ci) {
@@ -30,9 +31,13 @@ if (ci) {
3031
else if (process.env.DRONE) t.is(ci, "drone");
3132
else if (process.env.CI_NAME === "codeship") t.is(ci, "codeship");
3233
else if (process.env.GITHUB_ACTION) t.is(ci, "github_actions");
34+
else if (process.env.GITLAB_CI) t.is(ci, "gitlab");
3335
});
3436

35-
test("repo is correctly set", t => t.is(repo, "siddharthkp/ci-env"));
37+
test("repo is correctly set", t => {
38+
if (process.env.GITLAB_CI) t.is(repo, process.env.CI_PROJECT_PATH);
39+
else t.is(repo, "siddharthkp/ci-env");
40+
});
3641

3742
test("sha is set", t => {
3843
const real_sha =
@@ -41,7 +46,8 @@ if (ci) {
4146
process.env.CIRCLE_SHA1 ||
4247
process.env.WERCKER_GIT_COMMIT ||
4348
process.env.DRONE_COMMIT ||
44-
process.env.GITHUB_SHA;
49+
process.env.GITHUB_SHA ||
50+
process.env.CI_COMMIT_SHA; //gitlab
4551

4652
t.is(sha, real_sha);
4753
});
@@ -62,10 +68,11 @@ if (ci) {
6268
pullRequestNumber = process.env.CI_PULL_REQUEST.split("/").pop();
6369
if(process.env.GITHUB_ACTION && event === "pull_request")
6470
pullRequestNumber = process.env.GITHUB_REF.split('/')[2];
65-
71+
6672
const real_pull_request_number =
6773
process.env.TRAVIS_PULL_REQUEST ||
6874
process.env.DRONE_PULL_REQUEST ||
75+
process.env.CI_MERGE_REQUEST_ID || //gitlab
6976
pullRequestNumber ||
7077
""; // wercker does not expose pull request number
7178

@@ -78,6 +85,7 @@ if (ci) {
7885
real_jobUrl = `https://travis-ci.org/${repo}/jobs/${
7986
process.env.TRAVIS_JOB_ID
8087
}`;
88+
else if (process.env.GITLAB_CI) real_jobUrl = process.env.CI_JOB_URL;
8189
t.is(jobUrl, real_jobUrl);
8290
});
8391

@@ -113,7 +121,8 @@ if (ci) {
113121
process.env.WERCKER_GIT_BRANCH ||
114122
process.env.DRONE_BRANCH ||
115123
process.env.CI_BRANCH || // codeship
116-
process.env.GITHUB_REF.split('/')[2];
124+
process.env.CI_COMMIT_REF_NAME || // gitlab
125+
process.env.GITHUB_REF.split('/')[2]
117126

118127
t.is(branch, real_branch);
119128
}

0 commit comments

Comments
 (0)