Skip to content

Instantly share code, notes, and snippets.

@Vadimhtml
Created December 19, 2017 10:56
Show Gist options
  • Save Vadimhtml/5d17cd382400265a733570a1d30861a4 to your computer and use it in GitHub Desktop.
Save Vadimhtml/5d17cd382400265a733570a1d30861a4 to your computer and use it in GitHub Desktop.
Gulp sftp deploy
var gulp = require('gulp');
var path = require('path');
var GulpSSH = require('gulp-ssh');
var shell = require('gulp-shell');
var moment = require('moment');
var config = {
host: 'ftp.domain.ru',
port: 22,
username: 'special',
privateKey: fs.readFileSync('~/.ssh/id_rsa'),
agent: process.env.SSH_AUTH_SOCK
};
var archiveName = 'deploy.tgz';
var timestamp = moment().format('YYYYMMDDHHmmssSSS');
var buildPath = './build';
var rootPath = '/uploads/deploy_test/';
var releasesPath = rootPath + 'releases/';
var symlinkPath = rootPath + 'current';
var releasePath = releasesPath + timestamp;
var gulpSSH = new GulpSSH({
ignoreErrors: false,
sshConfig: config
});
gulp.task('build', function () {
return true; // Тут сборка проекта
});
gulp.task('deploy:compress', ['build'], shell.task("tar -cf ./" + archiveName + " --directory=" + buildPath + " ."));
gulp.task('deploy:prepare', function() {
return gulpSSH.exec("mkdir -p " + releasesPath+ " && cd " + releasesPath + " && mkdir " + timestamp);
});
gulp.task('deploy:upload', ['deploy:prepare', 'deploy:compress'], function() {
return gulp.src(archiveName)
.pipe(gulpSSH.sftp('write', releasePath + '/' + archiveName))
});
gulp.task('deploy:uncompress', ['deploy:upload'], function() {
return gulpSSH.exec("cd " + releasePath + " && tar -xf " + archiveName);
});
gulp.task('deploy:symlink', ['deploy:uncompress'], function() {
return gulpSSH.exec("rm -f " + symlinkPath + " &&" +
" ln -s " + releasePath + " " + symlinkPath);
});
gulp.task('deploy:clean', ['deploy:symlink'], shell.task('rm ' + archiveName, {ignoreErrors: true}));
gulp.task('deploy', ['build',
'deploy:compress',
'deploy:prepare',
'deploy:upload',
'deploy:uncompress',
'deploy:symlink',
'deploy:clean']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment