Created
March 24, 2018 16:31
-
-
Save LukvonStrom/a3d3ee24fa27e22267072a9151fd1e43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
secret: 'wohooodeploy', | |
port: 8888, | |
serverRoot: '/home/www' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Node-deploy-hook | |
* -------------------------------------------- | |
* For original Copyright see LICENSE, | |
* adopted and modified. | |
* | |
*/ | |
const express = require('express'); | |
const http = require('http'); | |
const childprocess = require("child_process"); | |
const path = require("path"); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
const config = require('./config'); | |
const server = http.createServer(app).listen(config.port); | |
// Allow node to be run with proxy passing | |
app.set('trust proxy', 'loopback, linklocal, uniquelocal'); | |
app.use(bodyParser.json()); // for parsing application/json | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.all('/', (req, res) => res.json({hi: 'hi'})); | |
app.all(`/${config.secret}/deploy`, function(req, res){ | |
let projectDir, | |
remoteBranch = req.params.remote_branch || 'origin', | |
localBranch = req.params.local_branch || 'master', | |
deployJSON, | |
payload, | |
ok = true; | |
if(req.body) payload = req.body; | |
if(payload && payload.repository && payload.repository.name){ // POST request made by github service hook, use the repo name | |
projectDir = path.normalize(`${config.serverRoot}/${payload.repository.name}`); | |
} else if(req.query.project){ // GET request made thru nginx proxy, use the appended project GET param | |
projectDir = path.normalize(`${config.serverRoot}/${req.query.project}`); | |
} else { // Else assume it is this repo or installed here, and was hit directly | |
projectDir = __dirname; | |
//res.json(req.body); | |
res.end('Invalid request'); | |
ok = false; | |
} | |
if(ok) { | |
let commands = [ | |
`if [ ! -d `+projectDir+` ]; then git clone ${payload.repository.git_url} ${projectDir}; fi`, | |
`cd ${projectDir}`, | |
`git rev-parse --is-inside-work-tree`, | |
`git stash`, | |
`git pull ${remoteBranch} ${localBranch}` | |
]; | |
childprocess.exec(commands.join(" && ").replace(String.fromCharCode(92), ''), (err, stdout, stderr) => { | |
if (err) { | |
deployJSON = {error: true, message: err, std: stderr, st: stdout}; | |
} else { | |
deployJSON = {success: true, message: stdout.message}; | |
} | |
res.json(deployJSON); | |
}); | |
} | |
}); | |
console.log((new Date()).toString(), ":: Node-deploy-hook server listening on port::", config.port, "Secret:", config.secret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2013 Alex Ehrnschwender (http://alexehrnschwender.com/) | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment