Skip to content

Commit

Permalink
Merge pull request #12 from A21z/2.0.0
Browse files Browse the repository at this point in the history
Version 2.0.0
  • Loading branch information
A21z committed Dec 4, 2015
2 parents 3a25d4f + fd00e29 commit 609d19d
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 206 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"standard"
]
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ results

node_modules
npm-debug.log
node-cd.sh
node-cd.sh

.idea/
.nvmrc
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 0.6
- 0.8
- 0.10.30
script: "cd src && npm install"
- 4.2.2
- 5.0.0
script: "npm test"
sudo: false
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
node-cd
=======

**Featherweight Github/Bitbucket Continuous Deployment**
**Featherweight Github/Bitbucket/Contentful Continuous Deployment**

Continuously deploy any code from Github to your server.
Continuously deploy any code from Github/Bitbucket/Contentful to your server.

node-cd is a simple node.js app handling Github's and Bitbucket's post-receive hooks.
It can execute any script you want on your server: deployment, testing, etc.
node-cd is a simple node.js app handling Github's, Bitbucket's and Contentful's post-receive hooks.
It can execute any script you want on your server: deployment, testing, etc.

## Installation

git clone https://github.com/A21z/node-cd.git
cd node-cd/src
npm install

## Usage

* `cp node-cd.template.sh node-cd.sh`
* Edit the `node-cd.sh` file to execute whatever you like after your commits (ex: stop server, git pull, start server)
* Edit the `bitbucket.sh`, `contentful.sh` or `github.sh` file to execute whatever you like after your commits (ex: stop server, git pull, start server)
* **For GitHub**: Set your post-receive hook as described [here](https://help.github.com/articles/post-receive-hooks) with the url `http://yourserver.com:61440/github`
* **For Bitbucket**: Set your post-receive hook as described [here](https://confluence.atlassian.com/display/BITBUCKET/POST+hook+management) with the url `http://yourserver.com:61440/bitbucket`
* **For Contentful**: Set your webhook in your Settings > Webhooks with the url `http://yourserver.com:61440/contentful`
* Add execution permission on scripts
`chmod +x bitbucket.sh contentful.sh github.sh`
* Run the app
`WWW_PORT=61440 node node-cd.js`
`WWW_PORT=61440 node src/index.js`

[![Build Status](https://travis-ci.org/A21z/node-cd.svg)](https://travis-ci.org/A21z/node-cd)
2 changes: 2 additions & 0 deletions bitbucket.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "Updating Template"
echo "====================================="
75 changes: 39 additions & 36 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
Private = {
server: {port: "61440"}, // Port is overriden by env var "WWW_PORT"
security: {
authorizedIps:[
'127.0.0.1',
'localhost',
// Bitbucket IPs
// 131.103.20.165,
// 131.103.20.166
// Github's IPs
// '207.97.227.253',
// '50.57.128.197',
// '204.232.175.75',
// '108.171.174.178'
],
bitbucketIps: [
'131.103.20.165',
'131.103.20.166'
],
githubIps: [
'207.97.227.253',
'50.57.128.197',
'204.232.175.75',
'108.171.174.178'
],
authorizedSubnet:[
'204.232.175.64/27',
'192.30.252.0/22'
]
},
repository: {
branch: 'refs/heads/master',
},
action: {exec: "../node-cd.sh"}
};
var Private = {
server: {port: '61440'}, // Port is overriden by env var 'WWW_PORT'
security: {
authorizedIps: [
'127.0.0.1',
'localhost'
],
bitbucketIps: [
'131.103.20.160',
'131.103.20.27',
'131.103.20.165',
'165.254.145.0',
'165.254.145.26',
'104.192.143.0',
'104.192.143.24'
],
githubIps: [
'207.97.227.253',
'50.57.128.197',
'204.232.175.75',
'108.171.174.178'
],
githubAuthorizedSubnets: [
'204.232.175.64/27',
'192.30.252.0/22'
]
},
repository: {
branch: 'master'
},
action: {
exec: {
github: './github.sh',
bitbucket: './bitbucket.sh',
contentful: './contentful.sh'
}
}
}

module.exports = Private;
module.exports = Private
3 changes: 3 additions & 0 deletions contentful.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Updating Content"
echo "====================================="

Empty file added github.sh
Empty file.
23 changes: 0 additions & 23 deletions node-cd.template.sh

This file was deleted.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "node-cd",
"version": "2.0.0-rc",
"private": true,
"scripts": {
"start": "node src/index",
"test": "standard && tape test/**/*.js"
},
"dependencies": {
"express": "4.13.3",
"body-parser": "^1.6.3",
"morgan": "^1.5.0",
"netmask": "0.0.2",
"underscore": ""
},
"devDependencies": {
"eslint": "^1.10.1",
"eslint-config-standard": "^4.4.0",
"eslint-plugin-standard": "^1.3.1",
"standard": "^3.0.0",
"tape": "^4.2.2"
}
}
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var express = require('express')
var path = require('path')
var http = require('http')
var routes = require('./routes.js')
var githubController = require('./routes/github.js')
var bitbucketController = require('./routes/bitbucket.js')
var contentfulController = require('./routes/contentful.js')
var config = require('../config.js')
var app = express()
var morgan = require('morgan')
var bodyParser = require('body-parser')

app.set('port', process.env.WWW_PORT || config.server.port)
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(path.join(__dirname + 'public')))

app.get('/', routes.index.index)
app.get('/favicon.ico', routes.index.favicon)
app.post('/github', githubController.create(config).post)
app.post('/bitbucket', bitbucketController.create(config).post)
app.post('/contentful', contentfulController.create(config).post)

http.createServer(app).listen(app.get('port'), function () {
console.log('Node-cd server listening on port ' + app.get('port'))
})
33 changes: 0 additions & 33 deletions src/node_cd.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/package.json

This file was deleted.

23 changes: 11 additions & 12 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
var fs = require('fs')
var _ = require('underscore');

var controllers = []

var files = fs.readdirSync(__dirname + "/routes")
for (var i = files.length - 1; i >= 0; i--){
if(files[i].match(/\.js$/i)) {
controllers.push(files[i]);
}
var files = fs.readdirSync(__dirname + '/routes')
for (var i = files.length - 1; i >= 0; i--) {
if (files[i].match(/\.js$/i)) {
controllers.push(files[i])
}
}

for (i = controllers.length - 1; i >= 0; i--) {
var name = controllers[i].slice(0,controllers[i].lastIndexOf('.'))
exports[name] = require("./routes/" + controllers[i])
var name = controllers[i].slice(0, controllers[i].lastIndexOf('.'))
exports[name] = require('./routes/' + controllers[i])
}

var that = this;
var that = this

exports.__allRoutes = function(req, res){
res.json(that);
};
exports.__allRoutes = function (req, res) {
res.json(that)
}
55 changes: 55 additions & 0 deletions src/routes/bitbucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var config

function Bitbucket (conf) {
config = conf
}

function create (conf) {
return new Bitbucket(conf)
}

module.exports.create = create

Bitbucket.prototype.post = function (req, res) {
var authorizedIps = config.security.authorizedIps
var bitbucketIps = config.security.bitbucketIps
var commits = req.body.push.changes
var ipv4 = req.ip.replace('::ffff:', '')

if (!(authorizedIps.indexOf(ipv4) >= 0 || bitbucketIps.indexOf(ipv4) >= 0)) {
console.log('Unauthorized IP:', req.ip)
res.writeHead(403)
res.end()
return
}

if (commits.length <= 0) {
res.writeHead(204)
res.end()
return
}

var commitsFromBranch = commits.filter(function (commit) {
return commit.new.name === config.repository.branch ||
commit.new.name === 'refs/heads/master' ||
commit.new.name === 'refs/heads/develop'
})

if (commitsFromBranch.length > 0) {
console.log('Executing bash file...')
myExec(config.action.exec.bitbucket)
}

res.writeHead(200)
res.end()
}

var myExec = function (line) {
var exec = require('child_process').exec
var execCallback = function (error) {
if (error !== null) {
console.log('exec error: ' + error)
}
}
exec(line, execCallback)
}
Loading

0 comments on commit 609d19d

Please sign in to comment.