Get environment variables exposed by CI services
npm install env-ciGet environment variables exposed by CI services.


Adapted from codecov-node.
``bash`
$ npm install --save env-ci
`js
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},Building branch ${branch}
);
} else {
console.log();
}
if (service === "travis") {
// Do something specific to Travis CI
}
}
`
| Variable | Description |
| ---------- | ------------------------------------------------------------------------------------------------------ |
| name | CI service Commercial name (e.g. Travis CI, CircleCI, GitLab CI/CD) |service
| | Standardized CI service name (e.g. travis, circleci, gitlab) |isCi
| | true is running on a CI, false otherwise |branch
| | Git branch being built or targeted by a Pull Request |commit
| | Commit sha that triggered the CI build |tag
| | Git tag that triggered the CI build |build
| | CI service build number |buildUrl
| | Link to the CI service build |job
| | CI service job number |jobUrl
| | Link to the CI service job |isPr
| | true if the build has been triggered by a Pull Request, false otherwise |pr
| | Pull Request number (only for builds triggered by a Pull Request) |prBranch
| | Git branch branch from which the Pull Request originated (only for builds triggered by a Pull Request) |slug
| | The slug (in form: owner_name/repo_name) of the repository currently being built |root
| | The 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.
| CI Service (name) | service | isCi | branch | commit | tag | build | buildUrl | job | jobUrl | isPr | pr | prBranch | slug | root |appveyor
| -------------------------------------------------------------------------------------------------------------------------------------- | :---------------: | :----------------: | :-------------------------: | :----------------: | :---------------------: | :----------------: | :----------------: | :----------------: | :----------------: | :-------------------: | :-------------------: | :-------------------: | :----------------: | :----------------: |
| AppVeyor | | :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-devops
| Azure Pipelines | | :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: |bamboo
| Bamboo | | :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: |bitbucket
| Bitbucket | | :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: |bitrise
| Bitrise | | :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: |buddy
| Buddy | | :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: | :x: | :white_check_mark: | :x: |buildkite
| Buildkite | | :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: |circleci
| CircleCI | | :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
| Cirrus CI | | :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: |cloudflarePages
| Cloudflare Pages | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: |codebuild
| AWS CodeBuild | | :white_check_mark: | :warning: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: |codefresh
| Codefresh | | :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: |codeship
| Codeship | | :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: |drone
| Drone | | :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
| GitHub Actions | | :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: |gitlab
| GitLab CI/CD | | :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: |jenkins
| Jenkins | | :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: |netlify
| Netlify | | :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: |puppet
| Puppet | | :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
| Sail CI | | :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
| Screwdriver.cd | | :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: |scrutinizer
| Scrutinizer | | :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: |semaphore
| Semaphore | | :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: |shippable
| Shippable | | :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: |teamcity
| TeamCity | | :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
| Travis CI | | :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: |vela
| Vela | | :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: |vercel
| Vercel | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :x: |wercker
| Wercker | | :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: |jetbrainsSpace
| JetBrains Space | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :white_check_mark: | :x: |woodpecker
| Woodpecker CI | | :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 triggeringisPr
builds when a Pull Request is opened/updated, will be undefined.
Note: If none of the above CI services is detected, commit and branch are determined based on the local GitisCi
repository, and is determined based on the CI environment variable.
#### options
Type: Object
#### env
Type: Objectprocess.env
Default:
The object to read environment variables from.
#### cwd
Type: Stringprocess.cwd()
Default:
The current working directory in which to execute git commands used to determine the commitbranch
and Result properties in case no supported CI is detected.
Type: Object
Environment variables values exposed by the CI service.
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.env-ci
To work around this limitation, look for the remote branches having the same HEAD as the localHEAD
detached to determine the branch from which the detached HEAD was created.HEAD
In the rare case where there is multiple remote branches with the same as the local detached HEAD, env-cibranch
will arbitrarily pick the first one. This can lead to an inaccurate value in such circumstances.
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.prBranch
However will be set.
See feature request.
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 propertybranch
however will always be set.
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 prBranchbranch
and define with the Pull Request target branch only if one those plugin is used.
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.prBranch
However will be set.
See feature request
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/ independently of the the branch name fromenv-ci
which the Pull Request originated.
Therefore, in the case of Pull Request builds, will not be able to determine the branch and prBranch
properties.
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.prBranch
However will be set.branch
On Semaphore 2.0 the and prBranch properties will work as expected.
The property tag is only available on Semaphore 2.0.
For builds triggered when a Pull Request is opened/updated, Screwdriver sets the env.GIT_BRANCH as head:pr branchorigin/refs/pull/1/head:pr
type (Example:) while at commit level (non PR) it does set it with the actual branch (Example: origin/main`).