Skip to content

Instantly share code, notes, and snippets.

@antoine1003
Created April 14, 2020 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoine1003/c614415a38304c8ac71eb95950764687 to your computer and use it in GitHub Desktop.
Save antoine1003/c614415a38304c8ac71eb95950764687 to your computer and use it in GitHub Desktop.
Fichier de configuration du déploiement du site Symfony 4.4 avec deployer.org
<?php
namespace Deployer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Dotenv\Dotenv;
require_once __DIR__.'/vendor/autoload.php';
require 'recipe/symfony4.php';
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/.env');
/*
* Dans mon .env j'ai ces variables qui sont utilisées ici :
* DEPLOYER_REPO_URL=git@gitlab.com:antoine1003/adautry.fr.git <- Accès à ton dépôt git
* DEPLOYER_REPO_HOSTNAME=u142210187@access820068188.webspace-data.io <- User/host du ftp (j'ai aussi mis une clé SSH pour éviter qu'il me demande mon mdp à chaque fois)
*/
set('application', 'adautry');
set( 'default_stage', 'test' );
set('keep_releases', 3); // Il gardera les trois dernières versions
set('ssh_multiplexing', false);
set('repository', $_ENV['DEPLOYER_REPO_URL']);
set('bin/php', '/usr/bin/php7.3-cli'); // Indique le chemin vers ton php-cli sur la machine distante
set('http_user', 'u142210187');
set('writable_mode', 'chmod');
set('writable_dirs', ['var']);
set('bin_dir', 'bin');
set('console_options', '');
set('clear_paths', ['var/cache']);
set('release_name', function () {
return (string) run('date +"%Y-%m-%d_%H-%M-%S"'); // Moi j'ai choisi de nommer les dossier de version comme ça : 2020-02-12_12-40-12
});
set('console_options', '--no-interaction --env={{symfony_env}}'); // Important pour utiliser les bonnes valeurs lors de la migration de la base
add('shared_files', ['.env']);
add('shared_dirs', ['public/uploads']);
// --- TEST ---
// Se lance quand je tape ./vendor/bin/dep deploy
host('test.adautry.fr')
->stage('test')
->set('symfony_env', 'test')
->set('branch', 'master')
->hostname($_ENV['DEPLOYER_REPO_HOSTNAME'])
->set('deploy_path', '/kunden/homepages/12/d820145675/htdocs/main/test');
// --- PROD ---
// Se lance quand je tape ./vendor/bin/dep deploy prod
host('www.adautry.fr')
->stage('prod')
->set('symfony_env', 'prod')
->set('branch', 'master')
->hostname($_ENV['DEPLOYER_REPO_HOSTNAME'])
->set('deploy_path', '/kunden/homepages/12/d820145675/htdocs/main/prod');
task( 'confirm', function () {
if ( ! askConfirmation( 'Are you sure you want to deploy to production?' ) ) {
write( 'Ok, quitting.' );
die;
}
} )->onStage( 'prod' ); // Demande de confirmation quand le déploiement est fait en prod
task('deploy:vendors', function () {
if (!commandExist('unzip')) {
writeln('To speed up composer installation setup "unzip" command with PHP zip extension https://goo.gl/sxzFcD');
}
$options = '';
if (get('symfony_env') == 'prod') {
$options .= '--no-dev';
}
$runCommand = sprintf('cd {{release_path}} && {{bin/composer}} install --verbose --prefer-dist --no-progress --no-interaction --optimize-autoloader %s', $options);
run($runCommand);
});
task('deploy:cache:clear', function () {
$optionsBegin = '';
$optionsEnd = '';
if (get('symfony_env') == 'prod') {
$optionsBegin .= 'APP_ENV=prod APP_DEBUG=0';
} else {
$optionsEnd .= '--no-warmup';
}
run("$optionsBegin {{bin/console}} cache:clear $optionsEnd");
})->desc('Clear cache');
task('deploy:assets:install', function () {
run('{{bin/console}} assets:install {{console_options}} --symlink');
})->desc('Install bundle assets');
/*
* Migrate database
*/
task('database:migrate', function () {
$options = '{{console_options}} --allow-no-migration';
if (get('migrations_config') !== '') {
$options = sprintf('%s --configuration={{release_path}}/{{migrations_config}}', $options);
}
run(sprintf('{{bin/php}} {{release_path}}/bin/console doctrine:migrations:migrate %s', $options));
})->desc('Migrate database');
task('deploy:done', function () {
write('Le déploiement en test a bien été effectué sous le nom {{release_name}}!');
});
/*
* Si il y a un bug alors on supprime le dossier qui a été créé
*/
task('deploy:failed', function () {
$fullDeployPath = '{{release_path}}';
// On check si le dossier existe, si c'est le cas on le supprime
// run("if [ -d $fullDeployPath ]; then rm -rf $fullDeployPath ; fi");
});
task('deploy', [
'confirm',
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:clear_paths',
'deploy:shared',
'deploy:vendors',
'deploy:cache:clear',
'deploy:cache:warmup',
'deploy:writable',
'deploy:assets:install',
'deploy:symlink',
'database:migrate',
'deploy:unlock',
'cleanup',
])->desc('Deploy your project');
after('deploy', 'deploy:done');
after('deploy:failed', 'deploy:unlock');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment