DeployerでLaravelをデプロイ

こんにちは!エンジニアの神山です!

Deployerを触る機会がありましたので、その内容を書きます!

Deployerとは

一言でいうと、コードのリリースをしてくれるツールになります。

導入も簡単ですぐに利用することができます。

デプロイの流れ

Gitクローンした後は、マイグレーションやLaravelのキャッシュクリアをしてくれます。

デプロイの準備(開発PC編)

  1. PCのDeployerをインストール
$ curl -LO <https://deployer.org/deployer.phar>
$ mv deployer.phar /usr/local/bin/dep
$ chmod +x /usr/local/bin/dep
  1. デプロイするプロジェクトにデプロイ設定
$ cd hogehoge-project
$ dep init
質問1
----------------------------------------------------
 Please select your project type [Common]:
  [0 ] Common
  [1 ] Laravel
  [2 ] Symfony
  [3 ] Yii
  [4 ] Yii2 Basic App
  [5 ] Yii2 Advanced App
  [6 ] Zend Framework
  [7 ] CakePHP
  [8 ] CodeIgniter
  [9 ] Drupal
  [10] TYPO3

対象のフレームワークを選択

質問2
----------------------------------------------------
 Repository []:
 >
「Github」や「GitLab」のSSHのRepostioryを入れる

質問3
----------------------------------------------------
 Contribute to the Deployer Development

 In order to help development and improve the features in Deployer,
 Deployer has a setting for usage data collection. This function
 collects anonymous usage data and sends it to Deployer. The data is
 used in Deployer development to get reliable statistics on which
 features are used (or not used). The information is not traceable
 to any individual or organization. Participation is voluntary,
 and you can change your mind at any time.

 Anonymous usage data contains Deployer version, php version, os type,
 name of the command being executed and whether it was successful or not,
 exception class name, count of hosts and anonymized project hash.

 If you would like to allow us to gather this information and help
 us develop a better tool, please add the code below.

     set('allow_anonymous_stats', true);

 This function will not affect the performance of Deployer as
 the data is insignificant and transmitted in a separate process.

 Do you confirm? (yes/no) [yes]:
 >

noにする

deployer.phpファイルが生成されていればOKです!

  1. deployer.phpファイルの修正


// Project repository
set('repository', 'Gitのリポジトリ');

// composerのオプション
set('composer_options', 'install --verbose --prefer-dist --no-progress --no-interaction --optimize-autoloader');

// 世代管理の件数
set('keep_releases', 3);

// Shared files/dirs between deploys
add('shared_files', ['.env']);
add('shared_dirs', ['vendor']);

// Writable dirs by web server
add('writable_dirs', ['bootstrap/cache', 'storage']);

// Hosts
// ※.ssh/config等で設定
host('本番環境のデプロイ先サーバのhost')
    ->stage('production')
    ->set('deploy_path', '~')
    ->set('branch', 'master');

host('テスト環境のデプロイ先サーバのhost')
    ->stage('develop')
    ->set('deploy_path', '~')
    ->set('branch', 'develop');

// 事前に.dev.productionなどを(.env.ステージ名の)ファイルを準備しておく
before('deploy:shared', 'upload-env');
task('upload-env', function () {
    $stage = get('stage');
    $src = ".env.${stage}";
    $deployPath = get('deploy_path');
    $sharedPath = "${deployPath}/shared";
    upload(__DIR__ . "/" . $src, "${sharedPath}/.env");
});

デプロイの準備(サーバ編)

  1. キーの生成
$ ssh-keygen -t rsa -f gitlab_rsa
  1. .ssh/configの設定
$ vim config
Host gitlab gitlab.com
  HostName gitlab.com
  IdentityFile ~/.ssh/gitlab_rsa
  User git

デプロイの準備(Gitリポジトリ編)

  1. 対象のリポジトリにサーバの公開鍵を登録

※画像はGitLabのデプロイキー登録画面

デプロイ実行

  1. コマンドラインでプロジェクトまで移動
  2. デプロイコマンド実行
$ dep deploy production

※productionはステージ名

まとめ

世代管理できるので、すぐにリリース前の状態に戻せるので便利かなーと。

あと、PHPで設定ファイルを書けるのが嬉しいですね。

今後やりたいこと

Gitにプッシュしたら、Deployer実行みたなおしゃれな感じにしたいのと、

php-fpmの自動再起動とか入れたりしたい。

あと、デプロイ完了をチャットツールに通知させるとかしてみたい。

 

Related article

おすすめ関連記事