Skip to content

Instantly share code, notes, and snippets.

@chiqui3d
Last active December 25, 2021 09:59
Show Gist options
  • Save chiqui3d/a9ea67c1bafcbc90bb71a867f92e5102 to your computer and use it in GitHub Desktop.
Save chiqui3d/a9ea67c1bafcbc90bb71a867f92e5102 to your computer and use it in GitHub Desktop.
Gulpfile to deploy Symfony or Laravel projects with SSH
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'));
@chiqui3d
Copy link
Author

chiqui3d commented Jun 18, 2018

Notes

  1. The Gulpfile generate a local sql file with the mysqldump command and then import it into the remote server with the mysql command
  2. The MYSQL Local connection data is taken from the .env file and remote server data is taken from the .env.production file.
  3. Symfony in production only reads .env then in afterRemoteDeploy the .env.production name changed to .env
  4. Tutorial on how to create an ssh shortcut https://scotch.io/tutorials/how-to-create-an-ssh-shortcut
  5. Apache or nginx configuration for symfony https://symfony.com/doc/current/setup/web_server_configuration.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment