はじめに
- PHP製のデプロイツールである Deployer が7系になって書き方が結構変わりそうなので触っておく
- Deployまでやろうと思ったが調査等に時間かけすぎたので一旦アウトプット
- 本当にさわりだけ、host周りの移行が出来たらまた書く
環境
- PHP 8.0
- Deployer 7.0.0-beta.11
初期化
- dep initで生成されるファイルがphpかyamlか選べるようになっている
- deploy.yamlとdeploy.php両方ある場合はphpのみ読み込まれる
dep initの実行
$ dep init
╔╦╗┌─┐┌─┐┬ ┌─┐┬ ┬┌─┐┬─┐
║║├┤ ├─┘│ │ │└┬┘├┤ ├┬┘
═╩╝└─┘┴ ┴─┘└─┘ ┴ └─┘┴└─
███████████████████████████████████████████████
Welcome to the Deployer config generator.
Press ^C at any time to quit.
Select recipe language [php]:
[0] php
[1] yaml
> 1
Select project template [common]:
[0 ] cakephp
[1 ] codeigniter
[2 ] common
[3 ] composer
[4 ] drupal7
[5 ] drupal8
[6 ] flow_framework
[7 ] fuelphp
[8 ] joomla
[9 ] laravel
[10] magento
[11] magento2
[12] prestashop
[13] provision
[14] shopware
[15] silverstripe
[16] sulu
[17] symfony
[18] typo3
[19] wordpress
[20] yii
[21] zend_framework
> 2
Repository []:
>
Project name [src]:
> DeployerSample
Hosts (comma separated) []:
> example
Successfully created deploy.yaml
生成されたyaml
deploy.yaml
import:
- recipe/common.php
- recipe/provision.php
config:
application: 'DeployerSample'
repository: ''
shared_files:
- .env
shared_dirs:
- uploads
writable_dirs:
- uploads
hosts:
example:
deploy_path: '~/{{application}}'
tasks:
build:
script:
- 'cd {{release_path}} && npm run build'
after:
deploy:failed: deploy:unlock
生成されたphp
最初の選択をphpにして実行
deploy.php
<?php
namespace Deployer;
require 'recipe/common.php';
require 'recipe/provision.php';
// Config
set('application', 'DeployerSample');
set('deploy_path', '~/{{application}}');
set('repository', '');
add('shared_files', []);
add('shared_dirs', []);
add('writable_dirs', []);
// Hosts
host('example');
// Tasks
task('build', function () {
cd('{{release_path}}');
run('npm run build');
});
after('deploy:failed', 'deploy:unlock');
外部ファイルのimport
ファイルツリー
/path/to/project
├─deployer
│ ├─hoge.php
│ └─hoge.yaml
└─deploy.yaml or deploy.php
deployer/hoge.php
<?php
namespace Deployer;
task('hoge:php', fn() => writeln('PHP'));
deployer/hoge.yaml
tasks:
hoge:yaml:
script: "touch ./touch-hoge-yaml"
yamlの場合
- 今回はテスト用に諸々省いたdeploy.yamlを使う
- 普通にimportの中にファイルを指定するだけで外部ファイルのタスクを取り扱えるようになる
- 取り敢えずローカルで実行
deploy.yaml
import:
- deployer/hoge.php
- deployer/hoge.yaml
hosts:
example:
local: true
deployer/hoge.phpのタスクを実行
$ dep hoge:php example
task hoge:php
[example] PHP
deployer/hoge.yamlのタスクを実行
- 現状アウトプットを表示しない?ようなのでtouchして確認
- やり方あればコメントで教えて下さいm(_ _)m
- scriptを実行すると sh
COMMAND
が実行される - Importer#tasksのrunの戻り値を取って funcions.phpのwriteln等に渡せば出力出来る
$ dep hoge:yaml example
task hoge:yaml
$ ls touch-hoge-yaml
touch-hoge-yaml
phpの場合
- yaml同様簡易化したファイル
- 結果はyamlと変わらないので割愛
deploy.php
<?php
namespace Deployer;
import('deployer/hoge.php');
import('deployer/hoge.yaml');
localhost('example');
memo
- yamlで定義されたtasks配下はImporter#tasks内で組み立てられる
- yamlのタスクに紐づくキーがscript, upload, download以外の場合、結局 src/functions.phpのtask内でエラーになる。