11.0.0 • Published 3 months ago

env-ci v11.0.0

Weekly downloads
1,162,390
License
MIT
Repository
github
Last release
3 months ago

env-ci

Get environment variables exposed by CI services.

Build Status npm latest version

Adapted from codecov-node.

Install

$ npm install --save env-ci

Usage

import envCi from "env-ci";

const {
  name,
  service,
  isCi,
  branch,
  commit,
  tag,
  build,
  buildUrl,
  job,
  jobUrl,
  isPr,
  pr,
  prBranch,
  slug,
  root,
} = envCi();

if (isCI) {
  console.log(`Building repo ${slug} on ${name} service`);

  if (isPr) {
    console.log(
      `Building Pull Request #${pr} originating from branch ${prBranch} and targeting branch ${branch}`,
    );
  } else {
    console.log(`Building branch ${branch}`);
  }

  if (service === "travis") {
    // Do something specific to Travis CI
  }
}

Supported variables

VariableDescription
nameCI service Commercial name (e.g. Travis CI, CircleCI, GitLab CI/CD)
serviceStandardized CI service name (e.g. travis, circleci, gitlab)
isCitrue is running on a CI, false otherwise
branchGit branch being built or targeted by a Pull Request
commitCommit sha that triggered the CI build
tagGit tag that triggered the CI build
buildCI service build number
buildUrlLink to the CI service build
jobCI service job number
jobUrlLink to the CI service job
isPrtrue if the build has been triggered by a Pull Request, false otherwise
prPull Request number (only for builds triggered by a Pull Request)
prBranchGit branch branch from which the Pull Request originated (only for builds triggered by a Pull Request)
slugThe slug (in form: owner_name/repo_name) of the repository currently being built
rootThe path to the directory where the repository is being built

Note: Some variables can be detected only on certain CI services. See Supported CI.

Note: The pr and prBranch properties are only available for builds triggered when a Pull Request is opened/updated and not on builds triggered by a push on a branch even if that branch happens to be the branch from which the Pull Request originated.

Supported CI

CI Service (name)serviceisCibranchcommittagbuildbuildUrljobjobUrlisPrprprBranchslugroot
AppVeyorappveyor:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Azure Pipelinesazure-devops:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::x::x::x::white_check_mark::white_check_mark::white_check_mark::x::white_check_mark:
Bamboobamboo:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::white_check_mark::x::x::x::x::x::white_check_mark:
Bitbucketbitbucket:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::x::x::x::white_check_mark::white_check_mark:
Bitrisebitrise:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x:
Buddybuddy:white_check_mark::warning::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::x::white_check_mark::x:
Buildkitebuildkite:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
CircleCIcircleci:white_check_mark::warning::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x:
Cirrus CIcirrus:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Cloudflare PagescloudflarePages:white_check_mark::white_check_mark::white_check_mark::x::x::x::x::x::x::x::x::x::white_check_mark:
AWS CodeBuildcodebuild:white_check_mark::warning::white_check_mark::x::white_check_mark::white_check_mark::x::x::x::x::x::x::white_check_mark:
Codefreshcodefresh:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Codeshipcodeship:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::x::x::x::white_check_mark::x:
Dronedrone:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
GitHub Actionsgithub:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::x::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
GitLab CI/CDgitlab:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Jenkinsjenkins:white_check_mark::warning::white_check_mark::x::white_check_mark::white_check_mark::x::x::warning::warning::warning::white_check_mark::white_check_mark:
Netlifynetlify:white_check_mark::warning::white_check_mark::x::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Puppetpuppet:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::x::x::x::x::x::x::white_check_mark:
Sail CIsail:white_check_mark::warning::white_check_mark::x::x::x::x::x::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark:
Screwdriver.cdscrewdriver:white_check_mark::warning::white_check_mark::x::white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Scrutinizerscrutinizer:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::x::x::x::white_check_mark::white_check_mark::white_check_mark::x::x:
Semaphoresemaphore:white_check_mark::warning::white_check_mark::warning::white_check_mark::x::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Shippableshippable:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
TeamCityteamcity:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::x::x::x::x::x::x::white_check_mark::white_check_mark:
Travis CItravis:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Velavela:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::x::x::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:
Vercelvercel:white_check_mark::white_check_mark::white_check_mark::x::x::x::x::x::x::x::x::white_check_mark::x:
Werckerwercker:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::white_check_mark::x::x::x::x::x::white_check_mark::white_check_mark:
JetBrains SpacejetbrainsSpace:white_check_mark::white_check_mark::white_check_mark::x::white_check_mark::x::x::x::x::x::x::white_check_mark::x:
Woodpecker CIwoodpecker:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:

:warning: See Caveats

Note: Unsupported properties will always be undefined. For example if a Ci services doesn't support triggering builds when a Pull Request is opened/updated, isPr will be undefined.

Note: If none of the above CI services is detected, commit and branch are determined based on the local Git repository, and isCi is determined based on the CI environment variable.

API

envCi(options) => Result

options

Type: Object

env

Type: Object Default: process.env

The object to read environment variables from.

cwd

Type: String Default: process.cwd()

The current working directory in which to execute git commands used to determine the commit and branch Result properties in case no supported CI is detected.

Result

Type: Object

Environment variables values exposed by the CI service.

Caveats

AWS CodeBuild

AWS CodeBuild doesn't provide an environment variable to determine the current Git branch being built. In addition, it clones the repository in a detached head state so the branch cannot be determined with git rev-parse --abbrev-ref HEAD. To work around this limitation, env-ci look for the remote branches having the same HEAD as the local detached HEAD to determine the branch from which the detached HEAD was created. In the rare case where there is multiple remote branches with the same HEAD as the local detached HEAD, env-ci will arbitrarily pick the first one. This can lead to an inaccurate branch value in such circumstances.

Buddy

For builds triggered when a Pull Request is opened/updated, Buddy doesn't provide an environment variable indicating the branch from which the Pull Request originated nor the target branch. It also build from a branch named pull/<PR number> so the target branch cannot be determined with a git command. Therefore, in the case of Pull Request builds, env-ci will not be able to determine the branch and prBranch properties.

See feature request.

CircleCI

For builds triggered when a Pull Request is opened/updated, CircleCI doesn't provide an environment variable indicating the target branch. Therefore, in the case of Pull Request builds, env-ci will not be able to determine the branch property. However prBranch will be set.

See feature request.

Cloudflare Pages

For builds triggered when a Pull Request is opened/updated, Cloudflare Pages will re-use the branch variable for the originating branch and not provide a target. Therefore env-ci will not be able to determine the prBranch property however branch will always be set.

Jenkins

Triggering build when a Pull Request is opened/updated is supported only via the ghprb-plugin and gitlab-plugin. Therefore env-ci will set isPr, pr and prBranch and define branch with the Pull Request target branch only if one those plugin is used.

Netlify

For builds triggered when a Pull Request is opened/updated, Netlify doesn't provide an environment variable indicating the target branch. Therefore, in the case of Pull Request builds, env-ci will not be able to determine the branch property. However prBranch will be set.

See feature request

Sail

For builds triggered when a Pull Request is opened/updated, Sail doesn't provide an environment variable indicating the target branch, and the one for the current branch is set to pull/<PR number> independently of the the branch name from which the Pull Request originated. Therefore, in the case of Pull Request builds, env-ci will not be able to determine the branch and prBranch properties.

Semaphore

For builds triggered when a Pull Request is opened/updated, Semaphore 1.0 doesn't provide an environment variable indicating the target branch. Therefore, in the case of Pull Request builds, env-ci will not be able to determine the branch property. However prBranch will be set. On Semaphore 2.0 the branch and prBranch properties will work as expected.

The property tag is only available on Semaphore 2.0.

Screwdriver

For builds triggered when a Pull Request is opened/updated, Screwdriver sets the env.GIT_BRANCH as head:pr branch type (Example:origin/refs/pull/1/head:pr) while at commit level (non PR) it does set it with the actual branch (Example: origin/main).

archetype-library@graphql-hive/cli@auto-canary/npmkilli8n-react-native-fast-image@educationperfect/semantic-release@bgcbrasil/sharedbgc-default-npmbgc-crawler-sidekick@bgcbrasil/dynamodb-operations@bgcbrasil/rds-operations@bgcbrasil/swagger-ui-formatter@bgcbrasil/usage-notifier-dynamodb-plugin@bgcbrasil/usage-notifier-rds-plugin@bgcbrasil/utilsbgc-ssm-securestring-pluginbgc-usage-notifier-pluginbgc-middleware@mink-opn/build-tokens@infinitebrahmanuniverse/nolb-env@everything-registry/sub-chunk-1589@rabailriaz/hisaab-web-portalbgc-mtls-integration-plugingogency-test-2gogencyissue-reporterjest-discord-reporteristanbul-gh-pr-uncoveredmonorepo-semantic-releasemarkdown-injectmycoverage-clilvbyte-cdnmiguelcostero-ng2-toastynpm-nomaster-commitsbgc-shared-team-ebgc-sdkgrishjan-create-checkfotingogdugit-branching-workflowgitlab-labels-versioningoctokit-clireact-picky-with-clearplaywright-dashboardplayright-dashboardreact-chromatics3-moduleresume34567resume_fivesemantic-release-bamboosemantic-release-notessemantic-release-squashsemantic-release-squash3semantic-release-ramkraosemantic-release-plussemantic-release-tsheets-testsemantic-release-wo-notessemantic-release-verify-patchsemantic-release-gitmoji-actionsemantic-release-flexsemantic-releaseset-ci@goraxe/semantic-release@hex-public/semantic-release@hydrosquall/auto-plugin-npm@hydrosquall/npmtwine-libtwine-libraryviswiz-cypressviswiztradeshift-scriptstravis-semantic-releasevue-v3-yandex-metrika@intuit-auto/core@intuit-auto/npm@iolaus/core@juice-js/semantic-release-npm@kalkanisys/vue-select@killianhmyd/semantic-release@jellywelly/iterare@innodata/vue-v3-ya-metrika@joao.kikuchi/motor-energia-components@inova/semverweb-yii2webiny-semantic-releasetldrawlignin@flaky-bot/cypress-plugin@flaky-bot/jest-pluginsuperset-plugin-chart-hello-world2date-to-block-ethdecorate-gh-prdebugbearstorybook-chromastorybook-chromaticspigot-ui-inventoryrocali-apollodiffjamteleprompttemp-semantic-release-debugsb-apollorewart-frontend-library
11.0.0

3 months ago

10.0.0

6 months ago

9.1.1

9 months ago

9.1.0

11 months ago

8.0.1-beta.2

1 year ago

9.0.0

12 months ago

8.0.1-beta.1

1 year ago

8.0.0-beta.1

1 year ago

7.4.0-beta.1

1 year ago

8.0.0

1 year ago

7.3.0

2 years ago

7.2.1

2 years ago

7.2.0

2 years ago

5.5.0

2 years ago

7.1.0

2 years ago

6.0.0

2 years ago

7.0.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.3.3

2 years ago

5.3.2

2 years ago

5.3.1

2 years ago

5.3.0

2 years ago

5.1.0

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.2.0

2 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.5.2

4 years ago

4.5.1

4 years ago

4.5.0

4 years ago

4.4.0

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.3

4 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.6.0

6 years ago

2.5.0

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.7.2

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago

0.0.0

6 years ago