diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be66fd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# common +coverage +node_modules +*.log +.DS_Store +.nyc_output diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5115a2d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +sudo: false + +language: node_js + +cache: + directories: + - node_modules + +notifications: + email: false + +node_js: +- 5 + +before_install: +- npm i -g npm@^2.0.0 + +before_script: +- npm prune + +after_success: +- npm run coverage:upload +- npm run semantic-release diff --git a/README.md b/README.md index 612bbdb..cf5a6fa 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ # condition-jenkins + +[![npm version](https://badge.fury.io/js/%40krux%2Fcondition-jenkins.svg)](http://badge.fury.io/js/%40krux%2Fcondition-jenkins) +[![Build Status](https://jenkins-ci.org/krux/condition-jenkins.svg)](https://jenkins-ci.org/krux/condition-jenkins) +[![Coverage Status](https://coveralls.io/repos/krux/condition-jenkins/badge.svg?service=github)](https://coveralls.io/github/krux/condition-jenkins) + +[![Dependency Status](https://david-dm.org/krux/condition-jenkins/master.svg)](https://david-dm.org/krux/condition-jenkins/master) +[![devDependency Status](https://david-dm.org/krux/condition-jenkins/master/dev-status.svg)](https://david-dm.org/krux/condition-jenkins/master#info=devDependencies) diff --git a/index.js b/index.js new file mode 100644 index 0000000..298428e --- /dev/null +++ b/index.js @@ -0,0 +1,26 @@ +var SRError = require('@semantic-release/error') + +module.exports = function (pluginConfig, config, cb) { + var env = config.env + var options = config.options + + if (!env.hasOwnProperty('JENKINS_URL')) { + return cb(new SRError( + 'semantic-release didn’t run on Jenkins CI and therefore a new version won’t be published.\n' + + 'You can customize this behavior using "verifyConditions" plugins: git.io/sr-plugins', + 'ENOJENKINS' + )) + } + + if (options.branch !== env.GIT_BRANCH) { + return cb(new SRError( + 'This test run was triggered on the branch ' + env.GIT_BRANCH + + ', while semantic-release is configured to only publish from ' + + options.branch + '.\n' + + 'You can customize this behavior using the "branch" option: git.io/sr-options', + 'EBRANCHMISMATCH' + )) + } + + cb(null) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..cd30a20 --- /dev/null +++ b/package.json @@ -0,0 +1,50 @@ +{ + "name": "@krux/condition-jenkins", + "description": "make sure things only the right builds on jenkins get published", + "version": "0.0.0-placeholder", + "author": "Stephan Bönnemann (http://boennemann.me)", + "bugs": { + "url": "https://github.com/krux/condition-jenkins/issues" + }, + "dependencies": { + "@semantic-release/error": "^1.0.0", + "semver": "^5.0.3" + }, + "devDependencies": { + "coveralls": "^2.11.2", + "nyc": "^5.0.0", + "proxyquire": "^1.7.1", + "semantic-release": "^6.0.3", + "standard": "^6.0.3", + "tap": "^5.0.0" + }, + "files": [ + "index.js" + ], + "homepage": "https://github.com/krux/condition-jenkins#readme", + "keywords": [ + "publish", + "release", + "semantic-release", + "jenkins-ci" + ], + "license": "MIT", + "publishConfig": { + "access": "public", + "tag": "next" + }, + "release": { + "branch": "next" + }, + "repository": { + "type": "git", + "url": "https://github.com/krux/condition-jenkins.git" + }, + "scripts": { + "coverage": "nyc report", + "coverage:upload": "npm run -s coverage -- --reporter=text-lcov | coveralls", + "pretest": "standard", + "semantic-release": "semantic-release pre && npm publish && semantic-release post", + "test": "nyc tap --no-cov test.js" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..a7e7b29 --- /dev/null +++ b/test.js @@ -0,0 +1,60 @@ +var proxyquire = require('proxyquire') +var test = require('tap').test +var SRError = require('@semantic-release/error') + +var condition = proxyquire('./', {}) + +test('raise errors in jenkins environment', function (t) { + t.test('only runs on jenkins', function (tt) { + tt.plan(2) + + condition({}, {env: {}}, function (err) { + tt.ok(err instanceof SRError) + tt.is(err.code, 'ENOJENKINS') + }) + }) + + t.test('only running on specified branch', function (tt) { + tt.plan(5) + + condition({}, { + env: { + JENKINS_URL: 'http://localhost/', + GIT_BRANCH: 'origin/master' + }, + options: { + branch: 'origin/master' + } + }, function (err) { + tt.is(err, null) + }) + + condition({}, { + env: { + JENKINS_URL: 'http://localhost/', + GIT_BRANCH: 'origin/notmaster' + }, + options: { + branch: 'origin/master' + } + }, function (err) { + tt.ok(err instanceof SRError) + tt.is(err.code, 'EBRANCHMISMATCH') + }) + + condition({}, { + env: { + JENKINS_URL: 'http://localhost/', + GIT_BRANCH: 'origin/master' + }, + options: { + branch: 'origin/foo' + } + }, function (err) { + tt.ok(err instanceof SRError) + tt.is(err.code, 'EBRANCHMISMATCH') + }) + }) + + t.end() +})