Skip to content

Instantly share code, notes, and snippets.

@ivanproskuryakov
Forked from jbraithwaite/shipitfile.js
Created July 16, 2016 09:26
Show Gist options
  • Select an option

  • Save ivanproskuryakov/6b87589bc6a9c5e2757779e26e85b794 to your computer and use it in GitHub Desktop.

Select an option

Save ivanproskuryakov/6b87589bc6a9c5e2757779e26e85b794 to your computer and use it in GitHub Desktop.

Revisions

  1. @jbraithwaite jbraithwaite created this gist Aug 28, 2015.
    130 changes: 130 additions & 0 deletions shipitfile.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    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: 'git@github.com: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 );

    });
    };