Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 20, 2017
1 parent b683801 commit 41eea14
Show file tree
Hide file tree
Showing 33 changed files with 1,266 additions and 1 deletion.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

# Created by https://www.gitignore.io/api/macos,windows,linux,node

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/macos,windows,linux,node

yarn.lock
package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
language: node_js
node_js:
- 9
- 8
- 6
- 4

# Trigger a push build on master and greenkeeper branches + PRs build on every branches
# Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147)
branches:
only:
- master
- /^greenkeeper.*$/

# Retry install on fail to avoid failing a build on network/disk/external errors
install:
- travis_retry npm install

script:
- npm run test

jobs:
include:
- stage: release
node_js: node
os: linux
script: npm run test
after_success:
- npm run codecov
- npm run semantic-release
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--install.no-lockfile true
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# env-ci
Get environment variables exposed by CI services

Get environment variables exposed by CI services.

[![Travis](https://img.shields.io/travis/pvdlg/env-ci.svg)](https://travis-ci.org/pvdlg/env-ci)
[![Codecov](https://img.shields.io/codecov/c/github/pvdlg/env-ci.svg)](https://codecov.io/gh/pvdlg/env-ci)
[![Greenkeeper badge](https://badges.greenkeeper.io/pvdlg/env-ci.svg)](https://greenkeeper.io/)

Adapted from [codecov-node](https://github.com/codecov/codecov-node/blob/master/lib/detect.js).

## Install

```bash
$ npm install --save env-ci
```

## Usage

```js
const {isCi, service, commit, build, branch, job, pr, isPr, slug, root} = require('env-ci');
```

## Variables

| Variable | Description |
|-----------|-----------------------------------------------------------------------------------|
| `isCi` | `true` is running on a CI, `false` otherwise |
| `service` | CI service name |
| `commit` | Commit sha that triggered the CI build |
| `build` | CI service build number |
| `branch` | Git branch being built or targeted by a pull request |
| `job` | CI service job number |
| `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 |

## Supported CI

| Service | `isCi` | `service` | `commit` | `build` | `branch` | `job` | `pr` | `isPr` | `slug` | `root` |
|-------------------------------------------------------------|:------:|:---------:|:--------:|:-------:|:--------:|:-----:|:----:|:------:|:------:|:------:|
| [AppVeyor](https://www.appveyor.com) |||||||||||
| [Buildkite](https://buildkite.com) |||||||||||
| [Circleci](https://circleci.com) |||||||||||
| [Codeship](https://codeship.com) |||||||||||
| [Drone](http://try.drone.io) |||||||||||
| [Gitlab CI](https://about.gitlab.com/features/gitlab-ci-cd) |||||||||||
| [Jenkins](https://jenkins-ci.org) |||||||||||
| [Semaphore](https://semaphoreci.com) |||||||||||
| [Shippable](https://www.shippable.com) |||||||||||
| [Travis](https://travis-ci.org/) |||||||||||
| [Wercker](http://www.wercker.com/) |||||||||||

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.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const git = require('./lib/git');

const services = {
travis: require('./lib/travis'),
circle: require('./lib/circle'),
appveyor: require('./lib/appveyor'),
wercker: require('./lib/wercker'),
codeship: require('./lib/codeship'),
jenkins: require('./lib/jenkins'),
semaphore: require('./lib/semaphore'),
shippable: require('./lib/shippable'),
drone: require('./lib/drone'),
buildkite: require('./lib/buildkite'),
gitlab: require('./lib/gitlab'),
};

module.exports = () => {
for (const name of Object.keys(services)) {
if (services[name].detect()) {
return Object.assign({isCi: true}, services[name].configuration());
}
}
return Object.assign({isCi: Boolean(process.env.CI)}, git.configuration());
};
20 changes: 20 additions & 0 deletions lib/appveyor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://www.appveyor.com/docs/environment-variables/

module.exports = {
detect() {
return Boolean(process.env.APPVEYOR);
},
configuration() {
return {
service: 'appveyor',
commit: process.env.APPVEYOR_REPO_COMMIT,
build: process.env.APPVEYOR_BUILD_NUMBER,
branch: process.env.APPVEYOR_REPO_BRANCH,
job: process.env.APPVEYOR_JOB_NUMBER,
pr: process.env.APPVEYOR_PULL_REQUEST_NUMBER,
isPr: Boolean(process.env.APPVEYOR_PULL_REQUEST_NUMBER),
slug: process.env.APPVEYOR_REPO_NAME,
root: process.env.APPVEYOR_BUILD_FOLDER,
};
},
};
19 changes: 19 additions & 0 deletions lib/buildkite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://buildkite.com/docs/builds/environment-variables

module.exports = {
detect() {
return Boolean(process.env.BUILDKITE);
},
configuration() {
return {
service: 'buildkite',
build: process.env.BUILDKITE_BUILD_NUMBER,
commit: process.env.BUILDKITE_COMMIT,
branch: process.env.BUILDKITE_BRANCH,
slug: `${process.env.BUILDKITE_ORGANIZATION_SLUG}/${process.env.BUILDKITE_PROJECT_SLUG}`,
pr: process.env.BUILDKITE_PULL_REQUEST === 'false' ? undefined : process.env.BUILDKITE_PULL_REQUEST,
isPr: process.env.BUILDKITE_PULL_REQUEST && process.env.BUILDKITE_PULL_REQUEST !== 'false',
root: process.env.BUILDKITE_BUILD_CHECKOUT_PATH,
};
},
};
19 changes: 19 additions & 0 deletions lib/circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://circleci.com/docs/1.0/environment-variables/

module.exports = {
detect() {
return Boolean(process.env.CIRCLECI);
},
configuration() {
return {
service: 'circleci',
build: `${process.env.CIRCLE_BUILD_NUM}.${process.env.CIRCLE_NODE_INDEX}`,
job: `${process.env.CIRCLE_BUILD_NUM}.${process.env.CIRCLE_NODE_INDEX}`,
commit: process.env.CIRCLE_SHA1,
branch: process.env.CIRCLE_BRANCH,
pr: process.env.CI_PULL_REQUEST ? process.env.CI_PULL_REQUEST.split('/').pop() : undefined,
isPr: Boolean(process.env.CI_PULL_REQUEST),
slug: `${process.env.CIRCLE_PROJECT_USERNAME}/${process.env.CIRCLE_PROJECT_REPONAME}`,
};
},
};
16 changes: 16 additions & 0 deletions lib/codeship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://documentation.codeship.com/basic/builds-and-configuration/set-environment-variables/

module.exports = {
detect() {
return process.env.CI_NAME && process.env.CI_NAME === 'codeship';
},
configuration() {
return {
service: 'codeship',
build: process.env.CI_BUILD_NUMBER,
commit: process.env.CI_COMMIT_ID,
branch: process.env.CI_BRANCH,
slug: process.env.CI_REPO_NAME,
};
},
};
19 changes: 19 additions & 0 deletions lib/drone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// http://readme.drone.io/0.5/usage/environment-reference/

module.exports = {
detect() {
return Boolean(process.env.DRONE);
},
configuration() {
return {
service: 'drone',
commit: process.env.DRONE_COMMIT_SHA,
build: process.env.DRONE_BUILD_NUMBER,
branch: process.env.DRONE_BRANCH,
job: process.env.DRONE_JOB_NUMBER,
pr: process.env.DRONE_PULL_REQUEST,
isPr: process.env.DRONE_BUILD_EVENT === 'pull_request',
slug: `${process.env.DRONE_REPO_OWNER}/${process.env.DRONE_REPO_NAME}`,
};
},
};
26 changes: 26 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const execa = require('execa');

function configuration() {
return {
commit: head(),
branch: branch(),
};
}

function head() {
try {
return execa.sync('git', ['rev-parse', 'HEAD']).stdout;
} catch (err) {
return undefined;
}
}

function branch() {
try {
return execa.sync('git', ['rev-parse', '--abbrev-ref', 'HEAD']).stdout;
} catch (err) {
return undefined;
}
}

module.exports = {configuration, head, branch};
23 changes: 23 additions & 0 deletions lib/gitlab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://docs.gitlab.com/ce/ci/variables/README.html

module.exports = {
detect() {
return Boolean(process.env.GITLAB_CI);
},
configuration() {
return {
service: 'gitlab',
commit: process.env.CI_COMMIT_SHA,
build: process.env.CI_JOB_NAME,
job: process.env.CI_JOB_STAGE,
branch: process.env.CI_COMMIT_REF_NAME,
slug: process.env.CI_REPOSITORY_URL
? process.env.CI_REPOSITORY_URL.split('/')
.slice(3, 5)
.join('/')
.replace('.git', '')
: undefined,
root: process.env.CI_PROJECT_DIR,
};
},
};
Loading

0 comments on commit 41eea14

Please sign in to comment.