sulu / sulu-demo

Sulu Demo Project
https://sulu.rocks
86 stars 44 forks source link

Deployer configuration #19

Open wachterjohannes opened 7 years ago

alexander-schranz commented 7 years ago

Here an example with doctrine migrations, nanbando backup, multiple prod server and one stage

<?php

namespace Deployer;

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;

require 'vendor/deployer/deployer/recipe/symfony3.php';
require 'vendor/nanbando/deployer-recipe/recipe.php';

// options

option('force', null, InputOption::VALUE_NONE, 'Force deploy (skip tag check)');

// Configuration

set('ssh_type', 'native');
set('ssh_multiplexing', true);

set('repository', 'git@github.com:sulu/sulu-demo.git');

set('shared_dirs', ['var/indexes', 'var/sessions', 'var/sitemaps', 'var/uploads', 'web/uploads']);

set('writable_dirs',
    // FIXME var/uploads is network share and setfacl can't use there
    ['var/cache', 'var/indexes', 'var/logs', 'var/sessions', 'web/uploads']
);

set('http_user', 'www-data');
set('http_group', 'www-data');

set('composer_options', '{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader --apcu-autoloader');

# backup over nanbando
set('bin/nanbando', '/usr/local/bin/nanbando');
set('nanbando_push', false);
set('nanbando_backup_options', '{{release_name}} --process database');

set('tasks', true);

// Servers

//stage
host('stage.example.org')
    ->user('deployer')
    ->forwardAgent()
    ->set('deploy_path', '/var/www/stage.example.org')
    ->set('branch', function () {
        return input()->getOption('branch') ?: 'develop';
    })
    ->set('nanbando', false)
    ->stage('stage');

// web01 prod
host('node01.example.org')
    ->user('deployer')
    ->forwardAgent()
    ->set('deploy_path', '/var/www/example.org')
    ->set('branch', function () {
        return input()->getOption('branch') ?: 'master';
    })
    ->stage('prod');

// web02 prod
host('node02.example.org')
    ->user('deployer')
    ->forwardAgent()
    ->set('deploy_path', '/var/www/example.org')
    ->set('branch', function () {
        return input()->getOption('branch') ?: 'master';
    })
    ->stage('prod');

// Tasks
desc('Check requirements');
task('deploy:check', function () {
    if (!input()->hasArgument('stage') || !input()->hasOption('tag') || !input()->hasOption('force')) {
        return;
    }

    $stage = input()->getArgument('stage');
    if (false === strpos($stage, ':prod') || input()->getOption('tag')) {
        return;
    }

    if (input()->getOption('tag')) {
        return;
    }

    if (input()->getOption('force')) {
        $question = new ConfirmationQuestion('Do you really want to deploy to "prod" without tag? [y, N] ', false);

        $helper = new QuestionHelper();
        $result = $helper->ask(input(), output(), $question);

        if (!$result) {
            throw new \Exception('You have aborted the deploy.');
        }

        return;
    }

    throw new \Exception('Tag options is required for stage "prod".');
});
before('deploy', 'deploy:check');

desc('Restart PHP-FPM service');
task('php-fpm:restart', function () {
    // The user must have rights for restart service
    run('sudo service php7.0-fpm restart');
});
after('deploy:symlink', 'php-fpm:restart');

desc('Reload nginx configuration');
task('nginx:reload', function () {
    // The user must have rights for reload nginx configuration
    run('sudo service nginx reload');
});
after('deploy:symlink', 'nginx:reload');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.
before('deploy:symlink', 'database:migrate');

desc('Schedule schedule-tasks');
task('task:schedule:system-tasks', function () {
    if (!get('tasks')) {
        return;
    }

    run('{{bin/php}} {{bin/console}} task:schedule:system-tasks {{console_options}}');
});
after('database:migrate', 'task:schedule:system-tasks');
wachterjohannes commented 7 years ago

@alexander-schranz we should add some examples directly to the repository:

alexander-schranz commented 6 years ago

In some projects it maybe also make sense to add bin/adminconsole doctrine:cache:clear-metadata to it where the metadata cache is for example in redis