totally / auto-deploy

Zend Framework 2 的自动部署模块

0.0.23 2017-05-30 16:37 UTC

README

AutoDeploy 是一个框架无关的自动部署应用程序,它作为 ZF2 模块提供支持,通过利用 VCS 提供商的 webhooks 提供自动部署代码的服务。

AutoDeploy 仍在开发中

可用服务

  • Vcs (Git)
  • Dm - 依赖管理器 (Composer)
  • Db - 数据库迁移(执行 MySql 迁移文件)

注意

  • Dm 和 Db 服务依赖于 Vcs 服务
  • Db 服务将最后运行
  • Db 迁移文件必须以 '_auto_deploy_' 开头,这允许它们在自动部署中被忽略
  • Db 服务上的错误将回滚其他服务,因此确保首先运行 Vcs 是重要的(@todo 强制执行)
  • 将向更新中的任何提交者发送电子邮件日志
  • 如果启用日志记录,则日志将被写入文件系统

安装

composer require totally/auto-deploy dev-master

确保项目中 composer.json 文件中的 'name' 键设置为仓库名称

{
    "name": "myrepoprojectname",
    ...
}

将以下内容添加到您的 index.php 或 web 根 php 文件顶部

define('APPLICATION_ROOT', realpath(dirname(__DIR__)));

ZF2 配置

通过运行以下命令创建 auto_deploy.php 并放置在 config/autoload 中

cp vendor/totally/auto-deploy/config/module.config.php config/autoload/auto_deploy.php

根据需要更新默认配置(以下是最小化运行所需的内容)

 ...

 'application' => [
     'email' => [
         'allowedDevEmailDomains' => [
             [YOUR EMAIL DOMAIN] // use regex pattern e.g. 'github\.com' for foo@github.com
         ],
     ]
 ],

  ...

 'auto_deploy' => [
     'services' => [
         'vcs' => [
             'originUrl' => '[GIT REPOSITORY URL]', // This is the git branch that you wish to auto deploy
             'runAs' => '', // user to execute vcs commands
         ],

         ...

         'dm' => [
             'runAs' => '', // user to execute dm commands
             'name' => '[COMPOSER PACKAGE NAME]' // This is required to identify the correct composer.json file
         ],

        ...

         'db' => [
             'type' => 'mysql',
             'runAs' => '', // user to execute db commands

              // This is where the db migration files are kept relative to the vcs root
              // migration files will only be applied if they start with '_auto_deploy_' - this is
              // to allow you the flexibility to pick and choose when auto db migration is applied
              // as it is not advised to use this feature for any heavy lifting
             'migrationDir' => '[MIGRATION FILE DIRECTORY]',

             // This is where the backup taken prior to a db migration are kept relative to the vcs root
             // make sure this directory is excluded from version control
             'backupDir' => '[MIGRATION BACKUP DIRECTORY]',

             // the below is required to perform db migrations - this is the target db
             'connection' => [
                 'hostname' => '[DB HOST]',
                 'username' => '[DB USERNAME]',
                 'password' => '[DB PASSWORD]',
                 'database' => '[DB NAME]'
             ],
             // the below is required to perform db migrations - multiple databases can be backed up if required
             'backup_connections' => [[
                 'hostname' => '[DB HOST]',
                 'username' => '[DB USERNAME]',
                 'password' => '[DB PASSWORD]',
                 'database' => '[DB NAME]',
                 'skipTables' => [ARRAY OF TABLE NAMES TO SKIP],
             ]],
         ],

         ...
     ],

      // This is a list of white-listed IP addresses for the modules internal firewall
     'ipAddresses' => [
          [GIT SERVER PROVIDER IP]
     ],
  ]

 ...

将 'AutoDeploy' 添加到 application.config.php 中的应用程序注册模块中

return array(
    'modules' => array(
        // other modules
        ...

        'AutoDeploy'

        ...
    ),
    // other content
);

通用配置(非 ZF2)

在您的 web 根之外创建 auto_deploy.php 文件,以下示例中我将使用 'config/auto_deploy.php' 相对于项目根目录

cp vendor/totally/auto-deploy/config/generic.config.php config/auto_deploy.php

替换以下方括号内的值 - 例如 [GIT BRANCH]

<?php
 return array(
     'application' => array(
        'email' => array(
            'allowedDevEmailDomains' => array(
                [YOUR EMAIL DOMAIN] // use regex pattern e.g. 'github\.com' for foo@github.com
            ),
            'fromName' => 'AutoDeploy',
            'fromEmail' => 'test@test.com',
            'replyTo' => 'test@test.com',
            'developerEmail' => 'test@test.com',
            'siteUrl' => '',
            'siteName' => '',
            'adminUrl' => '',
            'adminName' => ''
        )
    ),

     'auto_deploy' => array(
         /**
          * @todo allow for multiple of each service type
          */
         'services' => array(
             'vcs' => array(
                 'type' => 'git',
                 'branch' => '[GIT BRANCH]', // This is the git branch that you wish to auto deploy
                 'originUrl' => '[GIT REPOSITORY URL]',
                 'runAs' => '' // optional unix user to run git commands as
             ),

             'dm' => array(
                 'type' => 'composer',
                 'runAs' => '', // user to execute dm commands
                 'name' => '[COMPOSER PACKAGE NAME]' // This is required to identify the correct composer.json file
             ),

             'db' => array(
                 'type' => 'mysql',
                 'runAs' => '', // user to execute db commands
                 'migrationDir' => '[MIGRATION FILE DIRECTORY]',

                 // This is where the backup taken prior to a db migration are kept relative to the vcs root
                 // make sure this directory is excluded from version control
                 'backupDir' => '[MIGRATION BACKUP DIRECTORY]',

                 // the below is required to perform db migrations - this is the target db
                 'connection' => array(
                      'hostname' => '[DB HOST]',
                      'username' => '[DB USERNAME]',
                      'password' => '[DB PASSWORD]',
                      'database' => '[DB NAME]'
                  ),
                  // the below is required to perform db migrations - multiple databases can be backed up if required
                  'backup_connections' => array(array(
                      'hostname' => '[DB HOST]',
                      'username' => '[DB USERNAME]',
                      'password' => '[DB PASSWORD]',
                      'database' => '[DB NAME]',
                      'skipTables' => [ARRAY OF TABLE NAMES TO SKIP],
                  )),
             ),
         ),

         // This is a list of white-listed IP addresses for the modules internal firewall
         'ipAddresses' => array(
             [GIT SERVER PROVIDER IP]
         ),

         'log' => array(
             'enabled' => true,
             'logger' => 'Zend_Log', // default use the Zend Log
             'logDir' => 'var/log', // directory that the log file lives in
             'logFile' => 'application.log', // log file name
             'logTitle' => 'AutoDeploy', // log entry title
             'mail' => true,
             'mailerClass' => 'AutoDeploy\Application\SystemEmail', // default use the Zend Logger (must be implement AutoDeploy\Application\SystemEmailInterface)
         )
     ),
 );

创建并排除 vcs 中的日志目录 'var/log'

在定义 'APPLICATION_ROOT' 之后在您的 web 根 php 文件顶部放置以下内容,以便在请求 '/auto-deploy/' 之前由 AutoDeploy 处理

确保 '../vendor/autoload.php' 和 'config/auto_deploy.php' 的路径与您的应用程序结构正确


// auto load composer packages - assuming this is the path to your vendor dir
if (file_exists('../vendor/autoload.php')) {
	require_once '../vendor/autoload.php';
}

if ($_SERVER['REQUEST_URI'] === '/auto-deploy/') {
    // Run the auto deploy application!
    $autoDeploy = new \AutoDeploy\AutoDeploy(require 'config/auto_deploy.php');
    $autoDeploy->run();
    exit;
}

通用安装继续(包括 ZF2)

在您选择的 VCS 提供商中配置 webhook,以在推送事件上调用 [YOUR APPLICATION URL]/auto-deploy/

先决条件

  • 必须通过使用命令 'composer' 来使用全局 composer 服务
  • 必须安装 mysql-server 以使用 mysql db 服务
  • 必须安装 git 以使用 git vcs 服务

注意事项

  • auto_deploy.ipAddress : VCS 服务器 IP 地址必须添加到配置 [GIT SERVER PROVIDER IP]
  • application.email : 正确配置此设置,否则您的邮件可能会进入垃圾邮件
  • application.email.allowedDevEmailDomains : 此设置依赖于 php 环境变量 'env'
  • auto_deploy.services.db : 创建并排除 vcs 中的备份目录
  • auto_deploy.services.db : 迁移依赖于 vcs
  • auto_deploy.services.db : 迁移文件必须以 '_auto_deploy_' 开头
  • auto_deploy.services.db : sql 更新文件应包含正在更新的数据库
  • auto_deploy.log : 创建并排除 vcs 中的日志目录

@todo

  • 添加其他失败的回滚
  • 为数据库编写回滚
  • 在每个类型中允许多个服务
  • 编写单元测试