前回は PHPUnit での単体テストの作成と実行方法を説明しました。
今回は 作成したコマンドを本番環境にデプロイし実行させます。
それには、やり方はいくつかあると思います。
Contents
デプロイの方法
1. シンプルな方法
bulk-convert-images-cli
をフォルダごと 本番サーバーにアップし実行する。
2. CD (Continuous Delivery) を意識した方法
作成した bulk-convert-images-cli
を composerパッケージ化し github に push して 本番サーバで composer install し 実行する。
今回は (2) の方法でやってみます。
composer パッケージの作成
プロジェクトフォルダの中のcliパッケージに入ります。
1 2 |
$ cd bulk-convert-images/bulk-convert-images-cli |
composer init
composer init してパッケージ定義をインタラクティブに作成します。
1 2 |
$ composer init |
以下の質問に答えていくことで、composer.json を生成することができます。
パッケージ名は?
1 2 |
Package name (<vendor>/<name>) [yokada/bulk-convert-images-cli]: |
パッケージの説明文
1 2 |
Description []: WordPress media images you registered can be convert this cli command using wp-cli. |
作成者
1 2 |
Author [yokada <yokada.moo.jp@gmail.com>, n to skip]: |
最小限の安定性
1 2 |
Minimum Stability []: dev |
パッケージタイプ
1 2 |
Package Type (e.g. library, project, metapackage, composer-plugin) []: command |
ライセンス
1 2 |
License []: MIT |
依存性定義
1 2 3 4 5 6 7 8 9 10 11 |
Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? Search for a package: wp-cli/wp-cli Enter the version constraint to require (or leave blank to use the latest version): ^2.1 Search for a package: wp-cli/wp-cli-bundle Enter the version constraint to require (or leave blank to use the latest version): ^2.1 Search for a package: |
これで依存性定義は終了なので、エンターを押して次へ。
devの依存性定義(なし)
1 2 |
Would you like to define your dev dependencies (require-dev) interactively [yes]? no |
devの依存性定義はないので、エンターを押して次へ。
最終成果物
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "name": "yokada/bulk-convert-images-cli", "description": "WordPress media images you registered can be convert this cli command using wp-cli.", "type": "command", "require": { "wp-cli/wp-cli": "^2.1", "wp-cli/wp-cli-bundle": "^2.1" }, "license": "MIT", "authors": [ { "name": "yokada", "email": "yokada.moo.jp@gmail.com" } ], "minimum-stability": "dev" } Do you confirm generation [yes]? yes |
OKなので エンター押して終了させる。
すると、 bulk-convert-images/bulk-convert-images-cli/composer.json
ファイルが作成されているかと思います。
composer install してコマンドを実行できるか試してみる
現在のディレクトリ (bulk-convert-images/bulk-convert-images-cli)で composer install して 作成した composer.json がうまく動くか試します。
1 2 |
$ composer install |
以下のようになります。
1 2 3 4 5 6 7 8 9 10 11 |
Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 53 installs, 0 updates, 0 removals - Installing wp-cli/php-cli-tools (v0.11.11): Loading from cache - Installing wp-cli/mustangostang-spyc (0.6.3): Loading from cache ... snip ... wp-cli/wp-cli-bundle suggests installing psy/psysh (Enhanced `wp shell` functionality) Package ramsey/array_column is abandoned, you should avoid using it. No replacement was suggested. Writing lock file Generating autoload files |
インストールがうまくいけば、 bulk-convert-images/bulk-convert-images-cli/vendor
というフォルダと
bulk-convert-images/bulk-convert-images-cli/composer.lock
というファイルができます。
ちなみに、 bulk-convert-images/vendor
フォルダは、 wordplate の開発環境の依存ライブラリなので、混同しないようにしましょう。
wpコマンドを実行してみる。
1 2 |
$ vendor/bin/wp |
以下のように 作成したコマンドが表示されていればOK。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp <command> SUBCOMMANDS bulk-convert-images メディア画像を指定形式に変換します cache Adds, removes, fetches, and flushes the WP Object Cache object. cap Adds, removes, and lists capabilities of a user role. cli Review current WP-CLI info, check for updates, or see defined aliases. ... |
bulk-convert-images が実行できるか試す
現在のディレクトリで bulk-convert-images を実行してみます。
1 2 3 4 5 6 7 8 9 10 |
➜ vendor/bin/wp bulk-convert-images convert --to=gif Found 100 images to regenerate. 1/100 Regenerated thumbnails for "88" (ID 91). 2/100 Regenerated thumbnails for "89" (ID 92). 3/100 Regenerated thumbnails for "87" (ID 90). 4/100 Regenerated thumbnails for "86" (ID 89). 5/100 Regenerated thumbnails for "85" (ID 88). 6/100 Regenerated thumbnails for "84" (ID 87). ... |
OK。
次に、作成した composer パッケージ を github に push します。
github に push
githubでリポジトリを新規作成
githubにブラウザでアクセスし 以下の New ボタンで作成できます。
リポジトリ名は bulk-convert-images-cli
とします。ほかは適宜いれて、createボタンをポチします。
これでリポジトリのURLが割り当てられます。
git@github.com:yokada/bulk-convert-images-cli.git
がgitリポジトリのURLです。
git init します
ターミナルに戻って以下を実行します。
pushしたいのは、自分で作った composerパッケージなので プロジェクトフォルダのcliパッケージに入ります。
1 2 |
$ cd bulk-convert-images/bulk-convert-images-cli |
1 2 |
$ git init |
初期化成功:
1 2 |
Initialized empty Git repository in /Users/wpken/bulk-convert-images/bulk-convert-images-cli/.git/ |
.gitignore の作成
不要なファイルはgitリポジトリに含めたくないので、それを git コマンドに伝えるため、 .gitignore ファイルを作成します。
1 2 |
$ vi .gitignore |
以下を記述し保存します。
1 2 3 4 5 |
test/helper/images .DS_Store vendor |
git add でファイルをステージングします。
git add の対象となるファイルを確認します。
1 2 |
$ git status -uall |
1 2 3 4 5 6 7 8 |
.gitignore composer.json composer.lock src/cli.php test/cli-test.php test/helper/gen-dummy-images.sh test/helper/init.sh |
↑ いらないファイルが除外されていることを確認します。
そして git add – git commitします。
1 2 |
$ git add . |
1 2 |
$ git commit -m 'my first commit.' |
push先のリモートリポジトリを定義します
1 2 |
$ git remote add origin git@github.com:yokada/bulk-convert-images-cli.git |
remote が定義されたか確認。
1 2 3 4 |
$ git remote -v origin git@github.com:yokada/bulk-convert-images-cli.git (fetch) origin git@github.com:yokada/bulk-convert-images-cli.git (push) |
ok
git push
1 2 |
$ git push -u origin master |
実行結果:
1 2 3 4 5 6 7 8 9 |
Counting objects: 12, done. Delta compression using up to 12 threads. Compressing objects: 100% (10/10), done. Writing objects: 100% (12/12), 15.77 KiB | 5.26 MiB/s, done. Total 12 (delta 0), reused 0 (delta 0) To github.com:yokada/bulk-convert-images-cli.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'. |
これで、 composer パッケージを github にpushすることができました。
本番サーバに composer パッケージを デプロイ
このステップは CircleCI
などのデプロイサービスを利用することで自動化することができます。
その方法は別記事で説明するとして今回は、シンプルに本番サーバからcomposerを使って上で作ったパッケージをgithubからpullします。
本番サーバーにsshでログイン
1 2 |
$ ssh {あなたの本番サーバアカウント}@{あなたのIPアドレス} |
インストールするためのフォルダの準備
1 2 |
$ mkdir bulk-convert-images-cli |
1 2 |
$ cd bulk-convert-images-cli |
インストール側の composer.json の作成
ソースをインストールするための composer.json を bulk-convert-images-cli フォルダの中に作成し保存します。
1 2 |
$ vi composer.json |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "require": { "yokada/bulk-convert-images-cli": "@dev" }, "repositories": [ { "type": "vcs", "url": "git@github.com:yokada/bulk-convert-images-cli.git" } ] } |
composer install
1 2 |
$ composer install |
1 2 3 4 5 6 7 8 9 |
Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 53 installs, 0 updates, 0 removals - Installing wp-cli/php-cli-tools (v0.11.11): Downloading (100%) - Installing wp-cli/mustangostang-spyc (0.6.3): Downloading (100%) - Installing symfony/finder (v3.4.22): Downloading (100%) ... - Installing yokada/bulk-convert-images-cli (dev-master c5ea87d): Cloning c5ea87d4ea ... |
wp-cliの設定ファイル (wp-cli.yml) の作成
bulk-convert-images-cli/wp-cli.yml
1 2 |
$ vi wp-cli.yml |
自分の場合は以下のような設定となりました。
1 2 3 4 |
path: "/opt/bitnami/apps/wordpress/htdocs" require: - vendor/yokada/bulk-convert-images-cli/src/cli.php |
- path は 一般的には wp-config.php の存在するディレクトリを指定します。
- require は wpコマンドとしてロードしたPHPスクリプトを指定します。
実行
以下のように実行します。
1 2 |
$ wp bulk-convert-images convert -to=jpg |
結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Warning: Not allowed file specified, postId: 27, postMimeType: image/jpeg, origExt: jpg Found 8 images to regenerate. 1/8 Regenerated thumbnails for "test" (ID 489). 2/8 Regenerated thumbnails for "wpken.net" (ID 458). 3/8 Regenerated thumbnails for "test" (ID 454). 4/8 Regenerated thumbnails for "tomato" (ID 453). 5/8 Regenerated thumbnails for "test" (ID 452). 6/8 Regenerated thumbnails for "vvv2" (ID 352). 7/8 Regenerated thumbnails for "wand" (ID 190). 8/8 Regenerated thumbnails for "photo-1470408318404-1fea210fa893" (ID 27). Success: Regenerated 8 of 8 images. Success: convert succeeded. |
1件警告でました。mimeType: image/jpeg
で拡張子が jpg
のケースが存在するようです。
どういう経緯でこのような mimeType となるのか調べる。
コードの更新の流れ
- ローカルでコードの
git add commit push
- 本番サーバーで
composer update
これだけでokです。
ToDos
- デプロイの自動化: githubのmasterブランチにpushしたら circleci が起動して、自動でテストとデプロイが走るようにする。
- wp-cli.yml を環境に合わせものを用意する。
- 単体テストを追加する。
- 機能追加。
以上です。
No comment yet, add your voice below!