alwex/phpdeploy

该包的最新版本(v1.5.9)没有可用的许可证信息。

全功能的PHP部署工具,用于自动化在多个主机/环境上的代码部署

v1.5.9 2015-03-17 13:32 UTC

README

Packagist

php-deploy 是一款全功能的PHP部署工具,旨在允许执行非常常见或非常特定的任务。其主要理念是将命令按描述在简单的配置文件中串联起来,命令易于扩展和高度定制。

使用 composer 安装

如果您使用 composer,可以轻松地在项目中使用它。主要二进制文件将位于您的二进制路径 bin/pdeploy,并准备好使用。

composer require alwex/phpdeploy

初始化项目

要使用 php-deploy,您必须初始化项目。一些目录和基本配置文件将创建在项目根目录下的 .php-deploy 目录中。

bin/pdeploy config:init myproject

  • .php-deploy/config.ini -> 描述全局项目配置的主要配置文件
  • .php-deploy/environments -> 环境配置文件放在这里
  • .php-deploy/Command -> 您可以将自定义命令放在这里

您可能需要修改 config.ini 文件

添加一些环境

要添加环境,只需输入

bin/pdeploy config:addenv production

环境文件示例

# user used for all remote commands
login=mySshLogin

# directory where the project is located
# relatively to the path where you execute
# bin/pdeploy
fromDirectory = ./
# remote or local path where to deploy
# the application
toDirectory = /var/www
# list of hosts where to deploy the app
hosts[] = 'localhost'
hosts[] = 'web1'
hosts[] = 'web2'
# ...
# name of the symlink allowing
# multiple apps in the same path
symlink = current-php-deploy

[deploy]
# PRE TASK
# executed before task
# usually vcs export and build
# of the package to deploy
preTask[] = Deploy\Command\GitExport
preTask[] = Deploy\Command\ComposerInstall
preTask[] = Deploy\Command\TarGz
# deployment stage executed against
# each hosts
onTask[] = Deploy\Command\Scp
onTask[] = Deploy\Command\UnTarGz
# post deployment stage executed
# on each hosts, usually cache clear,
# apache reload and symlink generation
# after this stage, deployment is done!
postTask[] = Deploy\Command\Symlink
# after tasks executed one time only
afterTask[] = ExampleCommand # custom comand

[mytask]
# PRE TASK
preTask[] = Deploy\Command\Symlink
# ON TASK
onTask[] = Deploy\Command\Symlink
# POST TASK
postTask[] = Deploy\Command\Symlink
# AFTER TASK
afterTask[] = Deploy\Command\Symlink

# ... and so on as many tasks as necessary

执行任务

一旦在环境 ini 文件中定义了任务,您可以使用以下命令简单运行它们

bin/pdeploy task:run --release=0.0.1 --env=production mytask

创建自定义命令

要添加自定义命令,只需将其作为 PHP 类添加到 .php-deploy/Command 文件夹中,您可以从复制 ExampleCommand.php 开始。只需将此自定义命令添加到任务中,例如

afterTask[] = ExampleCommand

ExampleCommand 内容

class ExampleCommand extends \Deploy\Command\AbstractCommand {

    /**
     * optionally you may check if all the
     * requirement are met before running
     * the command
     *
     * @throw \RuntimeException
     */
    public function beforeRun() {

        if (file_exists("/tmp/hello.txt")) {
            throw new \RuntimeException("hello file already exists");
        }
    }

    /**
     * execute command and php tasks
     * return the execution status as an integer
     *
     * @return void
     */
    public function run()
    {
        $command = "echo hello > /tmp/hello.txt";
        $this->shellExec($command);
    }

    /**
     * optionally you may check if the command has been
     * correctly done
     *
     * @throw \RuntimeException
     */
    public function afterRun() {

        $expectedValue = 'hello';
        $fileContent = file_get_contents("/tmp/hello.txt");

        if ($fileContent != $expectedValue) {
            throw new \RuntimeException("hello file does not contain expected value '$expectedValue', found '$fileContent'");
        }
    }
}