Created
May 7, 2015 17:00
-
-
Save dperrera/e03e0959af43d68d017d to your computer and use it in GitHub Desktop.
Shipit File
This file contains hidden or 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
// ================================================== | |
// Shipit Commands | |
// ================================================== | |
// deploy | |
// pull-assets | |
// backup-local-db | |
// update-local-db | |
// sync-local | |
// backup-production-db | |
// update-production-db | |
// sync production | |
// ================================================== | |
// ================================================== | |
// Setup | |
// ================================================== | |
// Timestamp | |
var moment = require('moment'); | |
var ts = moment().format('YYYYMMDD[-]hhmmss'); | |
// Database (Same for local and production) | |
var dbName = 'XXXXX'; | |
// Remote Server | |
var remote = { | |
path: 'XXXXX', | |
user: 'XXXXX', | |
dbUser: 'XXXXX', | |
dbPwd: 'XXXXX', | |
dbDumpPath: 'XXXXX' | |
}; | |
// Local Server | |
var local = { | |
path: 'XXXXX', | |
host: '127.0.0.1', | |
mysqlPort: '33060', | |
dbDumpPath: 'XXXXX', | |
dbUser: 'homestead', | |
dbPwd: 'secret', | |
}; | |
// Function for building prettier statements | |
function command() { | |
var args = []; | |
for (var i = 0; i < arguments.length; i++) { | |
args.push(arguments[i]); | |
} | |
return args.join(' '); | |
} | |
// ================================================== | |
// Shipit | |
// ================================================== | |
module.exports = function (shipit) { | |
require('shipit-deploy')(shipit); | |
shipit.initConfig({ | |
default: { | |
workspace: 'tmp', | |
deployTo: 'XXXXX', | |
repositoryUrl: 'XXXXX', | |
ignores: ['.git', 'gulp'], | |
keepReleases: 2, | |
shallowClone: true, | |
}, | |
production: { | |
servers: 'XXXXX', | |
}, | |
local: { | |
servers: 'localhost' | |
} | |
}); | |
// ======================================== | |
// LOCAL COMMANDS | |
// ======================================== | |
// Backup Assets from Production | |
// ======================================== | |
shipit.task('pull-assets', function() { | |
return shipit.local( | |
command( | |
'rsync -aP', | |
remote.user + ':' + remote.path + 'XXXXX', | |
local.path + 'XXXXX.' | |
) | |
); | |
}); | |
// ======================================== | |
// Backup Local Database | |
// ======================================== | |
shipit.task('backup-local-db', function() { | |
return shipit.local( | |
// Make a local copy with a timestamp | |
command( | |
'mysqldump', | |
'--user=' + local.dbUser, | |
'--password=' + local.dbPwd, | |
'--host=' + local.host, | |
'--port=' + local.mysqlPort, | |
dbName, | |
'> ' + local.dbDumpPath + 'local/' + dbName + '-' + ts + '.sql' | |
) | |
) | |
.then( | |
shipit.local( | |
// Make a timestamp copy on production server | |
command( | |
'scp', | |
local.dbDumpPath + 'local/' + dbName + '-' + ts + '.sql', | |
remote.user + ':' + remote.dbDumpPath + 'local/' | |
) | |
) | |
) | |
.then( | |
shipit.local( | |
// Set file to 'db-local-current.sql' on production server | |
command( | |
'scp', | |
local.dbDumpPath + 'local/' + dbName + '-' + ts + '.sql', | |
remote.user + ':' + remote.dbDumpPath + 'db-local-current.sql' | |
) | |
) | |
); | |
}); | |
// ======================================== | |
// Update Local Database | |
// ======================================== | |
shipit.task('update-local-db', function() { | |
return shipit.local( | |
command( | |
'mysql', | |
'--user=' + local.dbUser, | |
'--password=' + local.dbPwd, | |
'--host=' + local.host, | |
'--port=' + local.mysqlPort, | |
'-D ' + dbName, | |
'< ' + local.dbDumpPath + 'db-production-current.sql' | |
) | |
); | |
}); | |
// ======================================== | |
// Sync Local Environment | |
// ======================================== | |
shipit.task('sync-local', ['backup-production-db', 'update-local-db', 'pull-assets']); | |
// ======================================== | |
// PRODUCTION | |
// ======================================== | |
// Backup Production Database | |
// ======================================== | |
shipit.task('backup-production-db', function() { | |
return shipit.remote( | |
// Make a copy on production server with a timestamp | |
command( | |
'mysqldump', | |
'--user=' + remote.dbUser, | |
'--password=' + remote.dbPwd, | |
dbName, | |
'> ' + remote.dbDumpPath + 'production/' + dbName + '-' + ts + '.sql' | |
) | |
) | |
.then( | |
shipit.local( | |
// Make a local copy with a timestamp | |
command( | |
'scp', | |
remote.user + ':' + remote.dbDumpPath + 'production/' + dbName + '-' + ts + '.sql', | |
local.dbDumpPath + 'production/' | |
) | |
) | |
) | |
.then( | |
shipit.local( | |
// Set file to 'db-production-current.sql' on local server | |
command( | |
'scp', | |
remote.user + ':' + remote.dbDumpPath + 'production/' + dbName + '-' + ts + '.sql', | |
local.dbDumpPath + 'db-production-current.sql' | |
) | |
) | |
); | |
}); | |
// ======================================== | |
// Update Production Database | |
// ======================================== | |
shipit.task('update-production-db', function() { | |
return shipit.remote( | |
command( | |
'mysql', | |
'--user=' + remote.dbUser, | |
'--password=' + remote.dbPwd, | |
'-D ' + dbName, | |
'< ' + remote.dbDumpPath + 'db-local-current.sql' | |
) | |
); | |
}); | |
// ======================================== | |
// Sync Production Environment | |
// ======================================== | |
shipit.task('sync-production', ['backup-local-db', 'update-production-db']); | |
}; // End of shipit |
Looks swell, though. It's pretty straightforward config.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd say to externalize the config bits (have like a
var auth = require('./auth.json') and enumerate your credentials & config needs there. This way you use
auth.username,
config.endpoint` like you are now, but managed nicer-ly.Provide an
auth.sample.json
w/the XXX'd parts and don't forget to gitignoreauth.json
,config.json
! My $0.02!