totallydave/auto-deploy

Zend Framework 2 的自动部署模块

0.0.11 2016-01-22 10:05 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:03:57 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 totallydave/auto-deploy dev-master

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

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

ZF2 配置

创建 auto_deploy.php 并在 config/autoload 中放置以下内容

cp vendor/totallydave/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
         ],

         ...

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

        ...

         'db' => [
             'type' => 'mysql',

              // 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]'
             ]],
         ],

         ...
     ],

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

 ...

在 application.config.php 中将 'AutoDeploy' 添加到注册的模块中

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

        'AutoDeploy'

        ...
    ),
    // other content
);

通用配置(非 ZF2)

创建 auto_deploy.php 并将其放置在 web 根目录之外的某个位置,以下示例中,我将使用 'config/auto_deploy.php',相对于项目根目录

cp vendor/totallydave/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]'
             ),

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

             'db' => array(
                 'type' => 'mysql',

                 '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]'
                  )),
             ),
         ),

         // 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 服务使 composer 可用
  • 必须安装 mysql-server 以使用 mysql 数据库服务
  • 必须安装 git 以使用 git vcs 服务

注意事项

  • auto_deploy.ipAddress:必须将 VCS 服务器 IP 地址添加到 config [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

  • 添加其他失败情况的回滚
  • 编写 db 的回滚
  • 允许每种类型中存在多个服务
  • 编写单元测试