Created
October 6, 2017 20:00
-
-
Save marcelitocs/53c4a0b94d4c801adfd1cb742458d8a7 to your computer and use it in GitHub Desktop.
gulp.js task to deploy code to remote servers
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
/******************************************************************************* | |
* Description: | |
* | |
* Gulp file to push changes to remote servers (eg: staging/production) | |
* | |
* Usage: | |
* | |
* gulp deploy --target | |
* | |
* Examples: | |
* | |
* gulp deploy --production // push to production | |
* gulp deploy --staging // push to staging | |
* | |
******************************************************************************/ | |
var gulp = require('gulp'); | |
// gulp-util - https://www.npmjs.com/package/gulp-util | |
var gutil = require('gulp-util'); | |
// Minimist - https://www.npmjs.com/package/minimist | |
var argv = require('minimist')(process.argv); | |
// gulp-rsync - https://www.npmjs.com/package/gulp-rsync | |
var rsync = require('gulp-rsync'); | |
// gulp-prompt - https://www.npmjs.com/package/gulp-prompt | |
var prompt = require('gulp-prompt'); | |
// gulp-if - https://www.npmjs.com/package/gulp-if | |
var gulpif = require('gulp-if'); | |
gulp.task('deploy', function() { | |
// Dirs and Files to sync | |
rsyncPaths = [path.dist, 'lang', 'lib', 'templates', './*.php', './style.css' ]; | |
// Default options for rsync | |
rsyncConf = { | |
progress: true, | |
incremental: true, | |
relative: true, | |
emptyDirectories: true, | |
recursive: true, | |
clean: true, | |
exclude: [], | |
}; | |
// Staging | |
if (argv.staging) { | |
rsyncConf.hostname = ''; // hostname | |
rsyncConf.username = ''; // ssh username | |
rsyncConf.destination = ''; // path where uploaded files go | |
// Production | |
} else if (argv.production) { | |
rsyncConf.hostname = ''; // hostname | |
rsyncConf.username = ''; // ssh username | |
rsyncConf.destination = ''; // path where uploaded files go | |
// Missing/Invalid Target | |
} else { | |
throwError('deploy', gutil.colors.red('Missing or invalid target')); | |
} | |
// Use gulp-rsync to sync the files | |
return gulp.src(rsyncPaths) | |
.pipe(gulpif( | |
argv.production, | |
prompt.confirm({ | |
message: 'Heads Up! Are you SURE you want to push to PRODUCTION?', | |
default: false | |
}) | |
)) | |
.pipe(rsync(rsyncConf)); | |
}); | |
function throwError(taskName, msg) { | |
throw new gutil.PluginError({ | |
plugin: taskName, | |
message: msg | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment