Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitlab CI support #10

Merged
merged 17 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
stages:
- test

test:
stage: test
image: node:8
script:
- npm i --silent
- npm test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

 

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

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

Expand Down
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a risky default

let repo, sha, event, commit_message, pull_request_number, branch, ci, jobUrl, buildUrl

if (process.env.TRAVIS) {
Expand Down Expand Up @@ -56,6 +60,18 @@ if (process.env.TRAVIS) {
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

Expand Down Expand Up @@ -110,9 +126,9 @@ if (process.env.TRAVIS) {
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_PULL_REQUEST_NUMBER
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, jobUrl, buildUrl }
module.exports = { repo, sha, event, commit_message, branch, pull_request_number, ci, platform, jobUrl, buildUrl }
19 changes: 14 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const {
commit_message,
pull_request_number,
branch,
ci
ci,
platform
} = require("./index");

if (ci) {
Expand All @@ -30,9 +31,13 @@ if (ci) {
else if (process.env.DRONE) t.is(ci, "drone");
else if (process.env.CI_NAME === "codeship") t.is(ci, "codeship");
else if (process.env.GITHUB_ACTION) t.is(ci, "github_actions");
else if (process.env.GITLAB_CI) t.is(ci, "gitlab");
});

test("repo is correctly set", t => t.is(repo, "siddharthkp/ci-env"));
test("repo is correctly set", t => {
if (process.env.GITLAB_CI) t.is(repo, process.env.CI_PROJECT_PATH);
else t.is(repo, "siddharthkp/ci-env");
});

test("sha is set", t => {
const real_sha =
Expand All @@ -41,7 +46,8 @@ if (ci) {
process.env.CIRCLE_SHA1 ||
process.env.WERCKER_GIT_COMMIT ||
process.env.DRONE_COMMIT ||
process.env.GITHUB_SHA;
process.env.GITHUB_SHA ||
process.env.CI_COMMIT_SHA; //gitlab

t.is(sha, real_sha);
});
Expand All @@ -62,10 +68,11 @@ if (ci) {
pullRequestNumber = process.env.CI_PULL_REQUEST.split("/").pop();
if(process.env.GITHUB_ACTION && event === "pull_request")
pullRequestNumber = process.env.GITHUB_REF.split('/')[2];

const real_pull_request_number =
process.env.TRAVIS_PULL_REQUEST ||
process.env.DRONE_PULL_REQUEST ||
process.env.CI_MERGE_REQUEST_ID || //gitlab
pullRequestNumber ||
""; // wercker does not expose pull request number

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

Expand Down Expand Up @@ -113,7 +121,8 @@ if (ci) {
process.env.WERCKER_GIT_BRANCH ||
process.env.DRONE_BRANCH ||
process.env.CI_BRANCH || // codeship
process.env.GITHUB_REF.split('/')[2];
process.env.CI_COMMIT_REF_NAME || // gitlab
process.env.GITHUB_REF.split('/')[2]

t.is(branch, real_branch);
}
Expand Down