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 13 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
18 changes: 17 additions & 1 deletion 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
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 @@ -115,4 +131,4 @@ if (process.env.TRAVIS) {
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 }
39 changes: 36 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const {
commit_message,
pull_request_number,
branch,
ci
ci,
platform
} = require("./index");

if (ci) {
if (ci && platform === "github") {
console.log("values: ", {
repo,
sha,
Expand Down Expand Up @@ -62,7 +63,7 @@ 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 ||
Expand Down Expand Up @@ -118,6 +119,38 @@ if (ci) {
t.is(branch, real_branch);
}
});
} else if (ci && platform === "gitlab") {
test("ci is correctly set", t => {
kuldeepkeshwar marked this conversation as resolved.
Show resolved Hide resolved
t.is(ci, "gitlab");
});
test("repo is correctly set", t => {
const real_repo = process.env.CI_PROJECT_PATH;
t.is(real_repo, repo);
});
test("branch is correctly set", t => {
const real_branch = process.env.CI_COMMIT_REF_NAME;
t.is(real_branch, branch);
});
test("commit message is correctly set", t => {
const real_commit_message = process.env.CI_COMMIT_MESSAGE;
t.is(real_commit_message, commit_message);
});
test("pull request is correctly set", t => {
const real_pull_request_number = process.env.CI_MERGE_REQUEST_ID;
t.is(real_pull_request_number, pull_request_number);
});
test("sha is correctly set", t => {
const real_sha = process.env.CI_COMMIT_SHA;
t.is(real_sha, sha);
});
test("event is correctly set", t => {
const real_event = process.env.CI_PIPELINE_SOURCE;
t.is(real_event, event);
});
test("job url is correctly set", t => {
const real_job_url = process.env.CI_JOB_URL;
t.is(jobUrl, real_job_url);
});
} else {
test.skip("These tests can only run in CI environments", t => t.pass());
}