• Stars
    star
    228
  • Rank 175,267 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Get environment variables exposed by CI services

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

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.

Supported CI

CI Service (name) service isCi branch commit tag build buildUrl job jobUrl isPr pr prBranch slug root
AppVeyor appveyor βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…
Azure Pipelines azure-devops βœ… βœ… βœ… ❌ βœ… ❌ ❌ ❌ βœ… βœ… βœ… ❌ βœ…
Bamboo bamboo βœ… βœ… βœ… ❌ βœ… βœ… βœ… ❌ ❌ ❌ ❌ ❌ βœ…
Bitbucket bitbucket βœ… βœ… βœ… βœ… βœ… βœ… ❌ ❌ ❌ ❌ ❌ βœ… βœ…
Bitrise bitrise βœ… βœ… βœ… βœ… βœ… βœ… ❌ ❌ βœ… βœ… βœ… βœ… ❌
Buddy buddy βœ… ⚠️ βœ… βœ… βœ… βœ… ❌ ❌ βœ… βœ… ❌ βœ… ❌
Buildkite buildkite βœ… βœ… βœ… βœ… βœ… βœ… ❌ ❌ βœ… βœ… βœ… βœ… βœ…
CircleCI circleci βœ… ⚠️ βœ… βœ… βœ… βœ… βœ… ❌ βœ… βœ… βœ… βœ… ❌
Cirrus CI cirrus βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…
Cloudflare Pages cloudflarePages βœ… βœ… βœ… ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ βœ…
AWS CodeBuild codebuild βœ… ⚠️ βœ… ❌ βœ… βœ… ❌ ❌ ❌ ❌ ❌ ❌ βœ…
Codefresh codefresh βœ… βœ… βœ… ❌ βœ… βœ… ❌ ❌ βœ… βœ… βœ… βœ… βœ…
Codeship codeship βœ… βœ… βœ… βœ… βœ… βœ… ❌ ❌ ❌ ❌ ❌ βœ… ❌
Drone drone βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…
GitHub Actions github βœ… βœ… βœ… ❌ βœ… ❌ ❌ ❌ βœ… βœ… βœ… βœ… βœ…
GitLab CI/CD gitlab βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…
Jenkins jenkins βœ… ⚠️ βœ… ❌ βœ… βœ… ❌ ❌ ⚠️ ⚠️ ⚠️ βœ… βœ…
Netlify netlify βœ… ⚠️ βœ… ❌ βœ… βœ… ❌ ❌ βœ… βœ… βœ… βœ… βœ…
Puppet puppet βœ… βœ… βœ… ❌ βœ… βœ… ❌ ❌ ❌ ❌ ❌ ❌ βœ…
Sail CI sail βœ… ⚠️ βœ… ❌ ❌ ❌ ❌ ❌ βœ… βœ… ❌ βœ… βœ…
Screwdriver.cd screwdriver βœ… ⚠️ βœ… ❌ βœ… βœ… βœ… ❌ βœ… βœ… βœ… βœ… βœ…
Scrutinizer scrutinizer βœ… βœ… βœ… ❌ βœ… ❌ ❌ ❌ βœ… βœ… βœ… ❌ ❌
Semaphore semaphore βœ… ⚠️ βœ… ⚠️ βœ… ❌ ❌ ❌ βœ… βœ… βœ… βœ… βœ…
Shippable shippable βœ… βœ… βœ… βœ… βœ… βœ… βœ… ❌ βœ… βœ… βœ… βœ… βœ…
TeamCity teamcity βœ… βœ… βœ… ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌ βœ… βœ…
Travis CI travis βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…
Vela vela βœ… βœ… βœ… βœ… βœ… βœ… ❌ ❌ βœ… βœ… βœ… βœ… βœ…
Vercel vercel βœ… βœ… βœ… ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌ βœ… ❌
Wercker wercker βœ… βœ… βœ… ❌ βœ… βœ… ❌ ❌ ❌ ❌ ❌ βœ… βœ…
JetBrains Space jetbrainsSpace βœ… βœ… βœ… ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌ βœ… ❌
Woodpecker CI woodpecker βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ… βœ…

⚠️ 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).

More Repositories

1

semantic-release

πŸ“¦πŸš€ Fully automated version management and package publishing
JavaScript
18,874
star
2

github

:octocat: semantic-release plugin to publish a GitHub release and comment on released Pull Requests/Issues
JavaScript
401
star
3

commit-analyzer

πŸ’‘ semantic-release plugin to analyze commits with conventional-changelog
JavaScript
361
star
4

cli

πŸ†‘πŸ“ Setup automated semver compliant package publishing
JavaScript
359
star
5

release-notes-generator

πŸ“‹ semantic-release plugin to generate changelog content with conventional-changelog
JavaScript
306
star
6

changelog

πŸ“˜ semantic-release plugin to create or update a changelog file
JavaScript
253
star
7

git

πŸ”€ semantic-release plugin to commit release assets to the project's git repository
JavaScript
252
star
8

npm

🚒 semantic-release plugin to publish a npm package
JavaScript
242
star
9

gitlab

🦊 semantic-release plugin to publish a GitLab release
JavaScript
233
star
10

cracks

πŸ’’πŸ” breaking change detection
JavaScript
111
star
11

gitlab-config

🦊 Semantic-release shareable config for GitLab
JavaScript
39
star
12

travis-deploy-once

🚫Test multiple node versions on Travis. Deploy once. If all of them pass.
JavaScript
34
star
13

issue-parser

Parser for Github, GitLab and Bitbucket issues actions, references and mentions
JavaScript
22
star
14

release-notes-generator-v3

β›” This repository has been archived
JavaScript
11
star
15

twitter-together

Submit tweets for https://twitter.com/SemanticRelease using pull requests
11
star
16

condition-travis

🚫 semantic-release plugin to check Travis CI environment before publishing.
JavaScript
9
star
17

error

πŸ’₯ πŸ’¬ errors but with error code
JavaScript
9
star
18

evolution

Proposals for changes to semantic-release
8
star
19

apm-config

:atom: semantic-release shareable config to publish Atom packages with apm
JavaScript
7
star
20

wordpress

🐢 Semantic Release plugin for packaging up WordPress plugins / themes
TypeScript
7
star
21

commit-analyzer-v2

🚫 This repository has been archived
JavaScript
4
star
22

npm-registry-docker

🚒 CouchDB Docker image running npm-registry-couchapp
Shell
3
star
23

.github

Common configuration for the semantic-release organization
3
star
24

last-release-git-tag

🚫 Determine the version of the last release with git tags
JavaScript
2
star
25

last-release-npm

🚫 determine the version of the last release via the npm registry
JavaScript
2
star
26

condition-codeship

🚫 make sure the right builds on codeship get published
JavaScript
1
star
27

condition-nsp

JavaScript
1
star
28

semantic-release.github.io

Project Website
CSS
1
star
29

welcome

πŸ“¦πŸ€— Welcome to the semantic-release community
1
star