diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d6f062d --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,9 @@ +stages: + - test + +test: + stage: test + image: node:8 + script: + - npm i --silent + - npm test diff --git a/README.md b/README.md index 4dcdae9..f2f8e02 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/index.js b/index.js index 1054ca8..32f04bd 100644 --- a/index.js +++ b/index.js @@ -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'; let repo, sha, event, commit_message, pull_request_number, branch, ci, jobUrl, buildUrl if (process.env.TRAVIS) { @@ -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 @@ -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 } diff --git a/test.js b/test.js index d261dff..4115878 100644 --- a/test.js +++ b/test.js @@ -9,7 +9,8 @@ const { commit_message, pull_request_number, branch, - ci + ci, + platform } = require("./index"); if (ci) { @@ -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 = @@ -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); }); @@ -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 @@ -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); }); @@ -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); }