Created
March 20, 2015 17:54
-
-
Save desaiuditd/51b3c08fc20e7df8a0e8 to your computer and use it in GitHub Desktop.
Gulp Git Deploy
This file contains 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
{ | |
"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" | |
} | |
} |
This file contains 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
/** | |
* 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() ); } ) | |
} ); |
This file contains 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
{ | |
"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