Last active
December 25, 2021 09:59
-
-
Save chiqui3d/a9ea67c1bafcbc90bb71a867f92e5102 to your computer and use it in GitHub Desktop.
Gulpfile to deploy Symfony or Laravel projects with SSH
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
var gulp = require('gulp'); | |
var rsync = require('gulp-rsync'); | |
var GulpSSH = require('gulp-ssh') | |
var fs = require('fs'); | |
var shell = require('gulp-shell'); | |
var dotenv = require('dotenv'); | |
var parseDbUrl = require("parse-database-url"); | |
/** | |
* DEPLOY | |
*/ | |
var envProduction = dotenv.parse(fs.readFileSync('.env.production')); | |
var envLocal = dotenv.parse(fs.readFileSync('.env')); | |
var mysqlProduction = parseDbUrl(envProduction.DATABASE_URL); | |
var mysqlLocal = parseDbUrl(envLocal.DATABASE_URL); | |
var deployDir = '/home/crm/public_html'; | |
var gulpSSH = new GulpSSH({ | |
ignoreErrors: false, | |
sshConfig: config | |
}); | |
gulp.task('test', function(cb) { | |
return console.log(mysqlLocal); | |
}); | |
gulp.task('beforeLocalDeploy', () => { | |
return gulp.src('./', { | |
read: false | |
}) | |
.pipe(shell( | |
[ | |
'mysqldump -u ' + mysqlLocal.user + ' -p' + mysqlLocal.password + ' ' + mysqlLocal.database + ' > ' + mysqlLocal.database + '.sql', | |
'rm -fr ./var/cache/*', | |
'composer dump-autoload --optimize' | |
] | |
)); | |
}) | |
gulp.task('beforeRemoteDeploy', function() { | |
return gulpSSH | |
.shell([ | |
'cd public_html', | |
'rm -fr ' + deployDir + '/*' | |
], { | |
filePath: 'beforeRemoteDeploy.log' | |
}) | |
.pipe(gulp.dest('var/log')) | |
}); | |
gulp.task('up', function() { | |
return gulp.src(['./'], { | |
dot: true | |
}).pipe(rsync({ | |
hostname: 'crm', //This is a shortcut created in my ~/.ssh/config folder | |
destination: deployDir, | |
recursive: true, | |
dryrun: false, //simulate | |
progress: true, | |
exclude: ['public/dist', 'public/js/custom.js', 'public/css/custom.css', 'logs', '.gitignore', '.phpintel', '.vscode', '.history', '.git', 'package.json', 'assets', 'node_modules', '*.lock', '.bowerrc', '.gulpfile', '.env', '.env.dist', '/*.js', 'crm.code-workspace'], | |
clean: true | |
})); | |
}); | |
gulp.task('afterRemoteDeploy', function() { | |
return gulpSSH | |
.shell([ | |
'cd public_html', | |
'mv .env.production .env && rm -f .env.production', | |
'php bin/console cache:clear --env=prod --no-debug',// (optionaĺ) because before the application was deployed, the cache was cleaned. | |
'mysql -u ' + mysqlProduction.user + ' -p' + mysqlProduction.password + ' ' + mysqlProduction.database + ' < ' + mysqlLocal.database + '.sql', | |
'rm -f ' + mysqlLocal.database + '.sql', | |
'chmod 755 public && chmod 755 public/index.php', | |
], { | |
filePath: 'afterRemoteDeploy.log' | |
}) | |
.pipe(gulp.dest('var/log')) | |
}); | |
gulp.task('deploy', gulp.series('beforeLocalDeploy', 'beforeRemoteDeploy', 'up', 'afterRemoteDeploy')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes
mysqldump
command and then import it into the remote server with themysql
command.env
file and remote server data is taken from the.env.production
file..env
then inafterRemoteDeploy
the.env.production
name changed to.env