Last active
February 13, 2024 14:31
-
-
Save plasticbrain/b98b5c3b97e7226353ce 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 | |
}); | |
} |
npm i ansi-colors
npm i plugin-error
replace var gutil = require('gulp-util'); with var colors = require('ansi-colors'); and var PluginError = require('plugin-error');
replace gutil.colors.red with colors.red and gutil.PluginError with PluginError
(:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any update for this gulp task ? Because https://www.npmjs.com/package/gulp-util is deprecated. Thanks