webtown/deployer-recipes

一些Deployer配方。

2.0.0 2017-08-14 11:55 UTC

README

添加composer

    "webtown/deployer-recipes": "~2.0.0"

本版本使用Deployer 5.x

包命令

初始化

您或您的组织可以注册自定义 deploy.php 模板文件。该模板还可以创建其他文件!该命令找到所有具有 webtown_deployer.template 标签的模板服务。

  1. 创建您自己的 DeployerTemplatesBundle
  2. 在资源目录中创建一个容器目录: Resources/Template/mySymfonyDeployerTemplateStructure
  3. 构建您的附加文件结构
Resources
  '- Template
       '- mySymfonyDeployerTemplateStructure
            |- app
            |    '- config
            |         '- Deployer
            |              |- server1_key.pub
            |              '- servers.yml
            |
            '- deploy.php
  1. MySymfonyDeployerTemplate 模板类创建到 /Template 目录
<?php
/**
 * Created by IntelliJ IDEA.
 * User: chris
 * Date: 2017.02.17.
 * Time: 16:37
 */

namespace Tests\Webtown\DeployerRecipesBundle\Template;

use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Filesystem\Filesystem;
use Webtown\DeployerRecipesBundle\Template\AbstractDirectoryTwigTemplate;

class MySymfonyDeployerTemplate extends AbstractDirectoryTwigTemplate
{
    // Vars for templates
    protected $type;
    protected $info;
    protected $username;
    protected $host;

    public function getDirectory()
    {
        return implode(DIRECTORY_SEPARATOR, [
            __DIR__,
            '..',
            'Resources',
            'Template',
            'mySymfonyDeployerTemplateStructure',
        ]);
    }
    
    public function build(InputInterface $input, OutputInterface $output, Command $command)
    {
        // Ask informations
        $helper = $command->getHelper('question');
        
        $typeQuestion = new Question('Please set type', false);
        $this->type = $helper->ask($input, $output, $typeQuestion);
        
        $infoQuestion = new Question('Please set info', false);
        $this->info = $helper->ask($input, $output, $infoQuestion);
        
        $usernameQuestion = new ChoiceQuestion(
            'Please select username',
            ['root', 'admin', 'barfoo'],
            0
        );
        $this->username = $helper->ask($input, $output, $usernameQuestion);
        
        $hostQuestion = new Question('Please set host', 'company.com');
        $this->host = $helper->ask($input, $output, $hostQuestion);
    
        parent::build($input, $output, $command);
    }
    
    public function getTemplateParameters()
    {
        return [
            'name'      => 'Test',
            'type'      => $this->type,
            'info'      => $this->info,
            'username'  => $this->username,
            'host'      => $this->host,
        ];
    }

    public function getName()
    {
        return 'My Symfony Deployer Template';
    }
}

配方

Symfony扩展

添加新任务

  • database:raw-create:如果不存在,则尝试创建数据库(从命令行,无需doctrine!)您可以使用 enable_mysql_database_create 启用它。
  • deploy:init-parameters-yml:询问参数。
  • database:migrate:rollback:迁移回滚。您必须将 use_database_migration_strategy 设置为 true!
  • deploy:force-cache-clean:强制清除缓存。私有!
<?php

namespace Deployer;

// include base
require 'vendor/deployer/deployer/recipe/symfony3.php';
// include extension
require 'vendor/webtown/deployer-recipes/recipes/symfony.php';

const SERVER_CONFIGURATION_FILE_PATH = 'app/config/deploy/servers.yml';
if (!file_exists(SERVER_CONFIGURATION_FILE_PATH)) {
    throw new \Exception(sprintf('Nem lett még létrehozva a szerver konfigurációs fájlod a `%s` helyen!', SERVER_CONFIGURATION_FILE_PATH));
}
serverList(SERVER_CONFIGURATION_FILE_PATH);

set('repository', 'git@github.com:webtown-php/project.git');
set('enable_mysql_database_create', true);

Kunstmaan

Kunstmaan特定设置。在 symfony.phpsymfony3.php 配方后包含 vendor/webtown/deployer-recipes/recipes/kunstmaan.php 文件

<?php

namespace Deployer;

// include base
require 'vendor/deployer/deployer/recipe/symfony3.php';
require 'vendor/webtown/deployer-recipes/recipes/common.php';
// include extension (it contains WT symfony.php)
require 'vendor/webtown/deployer-recipes/recipes/kunstmaan.php';

const SERVER_CONFIGURATION_FILE_PATH = 'app/config/deploy/servers.yml';
if (!file_exists(SERVER_CONFIGURATION_FILE_PATH)) {
    throw new \Exception(sprintf('Nem lett még létrehozva a szerver konfigurációs fájlod a `%s` helyen!', SERVER_CONFIGURATION_FILE_PATH));
}
serverList(SERVER_CONFIGURATION_FILE_PATH);

set('repository', 'git@github.com:webtown-php/project.git');

制作配置文件

prod:
    stage:          prod
    host:           host.com
    user:           user
    # if you are using key
    forward_agent:  true
    # if you are using different php version
    'bin/php':      /usr/local/php/php5.6/bin/php
    'bin/composer': /usr/local/php/php5.6/bin/php /usr/local/bin/composer
    deploy_path:    /var/www/project

然后

$ bin/dep deploy prod [-vvv]

回滚

$ bin/dep rollback prod [-vvv]

加载固定数据

这将删除整个数据库并重新构建它!

您可以使用 load_fixtures 参数在每个服务器上启用此功能!默认值为 false

<?php

namespace Deployer;

// include base
require 'vendor/deployer/deployer/recipe/symfony3.php';
// include extension
require 'vendor/webtown/deployer-recipes/recipes/common.php';
require 'vendor/webtown/deployer-recipes/recipes/symfony.php';
require 'vendor/webtown/deployer-recipes/recipes/load-fixtures.php';

const SERVER_CONFIGURATION_FILE_PATH = 'app/config/deploy/servers.yml';
if (!file_exists(SERVER_CONFIGURATION_FILE_PATH)) {
    throw new \Exception(sprintf('Nem lett még létrehozva a szerver konfigurációs fájlod a `%s` helyen!', SERVER_CONFIGURATION_FILE_PATH));
}
serverList(SERVER_CONFIGURATION_FILE_PATH);

set('repository', 'git@github.com:webtown-php/project.git');

维护

添加维护功能,您可以锁定或解锁网站。您需要一个维护模板html。默认路径在 maintenance_template 参数中定义。默认值是 maintenance.html.tpl 或在Symfony项目中位于 app/Resources/views/maintenance.html 的文件。

  1. 创建一个 maintenance.html.tplapp/Resources/views/maintenance.html。此文件将在维护期间供用户查看。
  2. 配置Web服务器。如果文档根中存在 maintenance.html,则强制以503状态显示此文件!
    • Apache示例
      ErrorDocument 503 /maintenance.html
      RewriteEngine On
      RewriteCond %{REQUEST_URI} !\.(css|js|gif|jpg|png)$
      RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
      RewriteCond %{SCRIPT_FILENAME} !maintenance.html
      RewriteRule ^.*$  -  [redirect=503,last]
      
    • Nginx示例
      if (-f \$document_root/maintenance.html) {
        return 503;
      }
      error_page 503 @maintenance;
      location @maintenance {
        rewrite  ^(.*)$  /maintenance.html last;
        break;
      }
      
  3. 注册任务(使用 beforeafter

deploy.php 文件

<?php

namespace Deployer;

// include base
require 'vendor/deployer/deployer/recipe/symfony3.php';
// include extension
require 'vendor/webtown/deployer-recipes/recipes/common.php';
require 'vendor/webtown/deployer-recipes/recipes/maintenance.php'; // <-- first!!!!
require 'vendor/webtown/deployer-recipes/recipes/symfony.php';     // <-- second!!!

// ...

// !!!! Register tasks
before('database:migrate', 'maintenance:lock');
// You don't have to unlock after deploy, because there is a new, clean `web` path without maintenance.html!
after('rollback', 'maintenance:unlock');

控制台命令

$ bin/dep rollback prod --keep-maintenance-file
// ...

$ bin/dep maintenance:lock prod 
✔ Executing task maintenance:lock
$ bin/dep maintenance:unlock prod
✔ Executing task maintenance:unlock