-
-
Save lpaulmp/12a3479e8dc88a696de0bd202167b863 to your computer and use it in GitHub Desktop.
Shipit deploy with Slack integration
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
var pack = require('./package.json'); | |
var request = require('request'); | |
var name = pack.name; | |
module.exports = function (shipit) { | |
require('shipit-deploy')(shipit); | |
var deployTo = '/var/www/FOLDER_TO_DEPLOY_TO'; | |
var deployToCurrent = deployTo + '/current'; | |
var slackWebhookURL = 'SLACK_HOOK_URL'; | |
var nodeModules = deployTo + '/node_modules'; | |
var productionConfig = deployTo + '/production.json'; | |
shipit.initConfig({ | |
default: { | |
workspace: '/tmp/' + name, | |
deployTo: deployTo, | |
repositoryUrl: '[email protected]:YOUR_REPO_HERE.git', | |
ignores: ['.git', 'node_modules'], | |
keepReleases: 5, | |
shallowClone: true | |
}, | |
production: { | |
servers: 'USER@SERVER' | |
} | |
}); | |
// Listen to the on published event. | |
// This happens right after the symlink for current is established | |
shipit.on('published', function(){ | |
shipit.start('post-publish'); | |
}); | |
// First run | |
// ================================================================ | |
// shipit.on('deploy', function(){ | |
// shipit.start('init:remote'); | |
// }); | |
// shipit.task('post-publish', ['symbolic-links', 'npm-install', 'pm2-start', 'slack']); | |
// ================================================================ | |
// Subsequent runs | |
// ================================================================ | |
shipit.task('post-publish', ['symbolic-links', 'npm-install', 'pm2-restart', 'slack']); | |
// ================================================================ | |
// Synbolic links | |
// ---------------------------------------------------------------- | |
shipit.blTask('symbolic-links', ['symbolic-link-config', 'symbolic-link-nodemodules'], function(){ | |
return; | |
}); | |
shipit.blTask('symbolic-link-config', function(){ | |
return shipit.remote('ln -Ffs ' + productionConfig + ' ' + deployToCurrent + '/config/'); | |
}); | |
shipit.blTask('symbolic-link-nodemodules', function(){ | |
return shipit.remote('ln -Ffs ' + nodeModules + ' ' + deployToCurrent + '/'); | |
}); | |
// npm install | |
// ---------------------------------------------------------------- | |
shipit.blTask('npm-install', function(){ | |
return shipit.remote('(cd ' + deployToCurrent + ' && NODE_ENV=development npm install && NODE_ENV=development npm prune && NODE_ENV=production npm run compile)'); | |
}); | |
// pm2 restart | |
// ---------------------------------------------------------------- | |
shipit.blTask('pm2-restart', function(){ | |
return shipit.remote('pm2 restart ' + name + ' && sleep 2 && pm2 info '+ name +''); | |
}); | |
shipit.blTask('pm2-start', function(){ | |
return shipit.remote('pm2 start '+ deployToCurrent +'/config/pm2-production.json && sleep 2 && pm2 ls && pm2 save'); | |
}); | |
// npm install | |
// ---------------------------------------------------------------- | |
shipit.blTask('npm-uninstall', function(){ | |
return shipit.remote('rm -R /var/www/FOLDER_TO_DEPLOY_TO/node_modules/*'); | |
}); | |
// Slack test | |
// ---------------------------------------------------------------- | |
shipit.blTask('slack', function(cb){ | |
var workspace = shipit.config.workspace; | |
if (!slackWebhookURL) { | |
return cb(); | |
} | |
shipit.local('git rev-parse HEAD', {cwd: workspace}).then(function(res) { | |
var githubLink = pack.bugs.url.replace('/issues',`/commit/${res.stdout}`).trim(); | |
request({ | |
method: 'POST', | |
uri: slackWebhookURL, | |
json: true, | |
body: { | |
username: 'Shipit', | |
// APP_NAME v0.0.1 - deployed to production | |
// #GIT_HASH | |
// View on Github | |
text: `${name} v${pack.version} - Deployed to ${shipit.environment} \n#${res.stdout}\n<${githubLink}|View on GitHub>` | |
} | |
}, | |
function (error, response, body) { | |
if (error) { | |
return console.error('upload failed:', error); | |
} | |
console.log('Upload successful! Server responded with:', body); | |
return cb(); | |
}); | |
}); | |
}); | |
shipit.blTask('init:remote', function(){ | |
shipit.remote('mkdir -p ' + deployTo); | |
shipit.remote('mkdir ' + nodeModules); | |
shipit.remoteCopy('./config/production.json', deployTo ); | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment