Skip to content

Instantly share code, notes, and snippets.

@desaiuditd
Created March 20, 2015 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desaiuditd/51b3c08fc20e7df8a0e8 to your computer and use it in GitHub Desktop.
Save desaiuditd/51b3c08fc20e7df8a0e8 to your computer and use it in GitHub Desktop.
Gulp Git Deploy
{
"staging": {
"destination": "/var/www/staging.example.com/htdocs/",
"hostname": "staging.example.com",
"username": "www-data",
"port": 22,
"sshLocalPath": "/home/udit/.ssh/id_rsa"
},
"production": {
"destination": "/var/www/example.com/htdocs/",
"hostname": "example.com",
"username": "www-data",
"port": 22,
"sshLocalPath": "/home/udit/.ssh/id_rsa"
}
}
/**
* Created by udit on 19/3/15.
*/
var argv = require( 'yargs' ).argv;
var gulp = require( 'gulp' );
var fs = require( 'fs.extra' );
var gulpSSH = require( 'gulp-ssh' );
var chalk = require( 'chalk' );
var deployJSON = require( './deploy.json' );
var deployConfig;
var sshConnect;
gulp.task( 'setupSSH', function() {
if ( argv.staging ) {
// Staging
deployConfig = deployJSON.staging;
} else if( argv.production ) {
//Production
deployConfig = deployJSON.production;
} else {
// Custom Environment
console.log( chalk.bold.red( '"staging" or "production" environment is not passed. If your custom environment is configured then you need to provide its support. Comming Soon !' ) );
process.exit();
}
// Setup SSH object for remote commands
sshConnect = gulpSSH( {
ignoreErrors: false,
sshConfig: {
host: deployConfig.hostname,
port: deployConfig.port,
username: deployConfig.username,
privateKey: fs.readFileSync( deployConfig.sshLocalPath )
}
} );
} );
gulp.task( 'pull', [ 'setupSSH' ], function() {
var gitPullCommand = 'cd ' + deployConfig.destination + ' && git reset --hard HEAD && git pull';
return sshConnect.shell( [ gitPullCommand ] )
.pipe( gulp.dest('./') )
.on( 'end', function() { console.log( chalk.green( 'Pull Complete !' ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log').toString() ); } );
} );
gulp.task( 'checkout', [ 'setupSSH' ], function() {
if ( argv.revision ) {
var gitCheckoutCommand = 'cd ' + deployConfig.destination + ' && git reset --hard HEAD && git fetch --all && git checkout ' + argv.revision;
return sshConnect.shell( [ gitCheckoutCommand ] )
.pipe( gulp.dest('./') )
.on( 'end', function() { console.log( chalk.green( 'Checkout Complete !' ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log').toString() ); } );
} else {
console.log( chalk.bold.red( 'You need to provide revision to be able to checkout at particular version ! USE : "gulp checkout --revision=master" or gulp checkout --revision=5s3532' ) );
process.exit();
}
} );
gulp.task( 'status', [ 'setupSSH' ], function() {
var gitStatusCommand = 'cd ' + deployConfig.destination + ' && git status';
return sshConnect.shell( [ gitStatusCommand ] )
.pipe( gulp.dest('./') )
.on( 'end', function() { console.log( chalk.green( "Status Complete !" ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } )
} );
{
"devDependencies": {
"gulp": "^3.8.10",
"gulp-ssh": ">=0.3.3",
"yargs": ">=3.5.4",
"fs.extra": ">=1.3.2",
"chalk": ">=1.0.0"
},
"engines": {
"node": ">=0.10.0"
},
"private": true,
"scripts": {
"test": "gulp && git status | grep 'working directory clean' >/dev/null || (echo 'Please commit all changes generated by building'; exit 1)"
},
"dependencies": {
"gulp-install": "^0.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment