Get environment variables exposed by CI services.
Adapted from codecov-node.
$ npm install --save env-ci
const envCi = require('env-ci');
const {isCi, name, service, commit, build, buildUrl, branch, job, jobUrl, pr, isPr, slug, root} = envCi();
if (isCI) {
console.log(`Building branch ${branch} of repo ${slug} on ${name}`);
if (isPr) {
console.log(`Building Pull Request #${pr}`);
}
if (service === 'travis') {
// Do something specific to Travis CI
}
}
Variable | Description |
---|---|
isCi |
true is running on a CI, false otherwise |
name |
CI service Commercial name (e.g. Travis CI , CircleCI , GitLab CI/CD ) |
service |
Standardized CI service name (e.g. travis , circleci , gitlab ) |
commit |
Commit sha that triggered the CI build |
build |
CI service build number |
buildUrl |
Link to the CI service build |
branch |
Git branch being built or targeted by a pull request |
job |
CI service job number |
jobUrl |
Link to the CI service job |
pr |
Pull Request number |
isPr |
true is the build has been triggered by a Pull Request, false otherwise |
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 |
CI Service (name ) |
service |
isCi |
commit |
build |
buildUrl |
branch |
job |
jobUrl |
pr |
isPr |
slug |
root |
---|---|---|---|---|---|---|---|---|---|---|---|---|
AppVeyor | appveyor |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Bamboo | bamboo |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
Bitbucket | bitbucket |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
Bitrise | bitrise |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
Buildkite | buildkite |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
CircleCI | circleci |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ |
AWS CodeBuild | codebuild |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
Codeship | codeship |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ |
Drone | drone |
✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ |
Gitlab CI/CD | gitlab |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ |
Jenkins | jenkins |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
Semaphore | semaphore |
✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Shippable | shippable |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
TeamCity | teamcity |
✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
Travis CI | travis |
✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Wercker | wercker |
✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
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.
Certain CI services don't provide an environment variable to determine the current Git branch being built.
In such cases the branch is determined with the command git rev-parse --abbrev-ref HEAD
.
However, if the local repository is in a detached head state the branch cannot be determined directly. In such case, env-ci
will 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 for certain CI services in such circumstances.
Affected CI services: