Memo

メモ > サーバ > 各論: Deployer > プロジェクトを作成してリポジトリをデプロイ

■プロジェクトを作成してリポジトリをデプロイ
※デプロイ先サーバにGitのインストールが必要 ■デプロイ先ディレクトリを作成 あらかじめ、デプロイ先サーバにディレクトリを作成しておく
$ sudo su - # mkdir /var/www/deploy_test # chown vagrant. /var/www/deploy_test
■プロジェクトを作成 デプロイ元サーバでDeployerのプロジェクトを作成する (「deploy_init」という名前にしているが、実際は案件名をもとにしたディレクトリ名にするといい)
$ cd $ mkdir deploy_init $ cd deploy_init $ dep init … プロジェクトを作成 Welcome to the Deployer config generator This utility will walk you through creating a deploy.php file. It only covers the most common items, and tries to guess sensible defaults. Press ^C at any time to quit. 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 > 0 … 今回は「0」を入力 Repository []: > git@bitbucket.org:refirio/test.git … デプロイしたいリポジトリを入力 Contribute to the Deployer Development In order to help development and improve Deployer features in, Deployer has a setting for collection of usage data. 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 separate process. Do you confirm? (yes/no) [yes]: > yes … 「yes」を入力 Successfully created: /home/vagrant/deploy_init/deploy.php … 上記入力内容をもとに、処理内容を記述したファイルが作成される $ vi file.yml … 接続先を定義
develop: hostname: 192.168.33.11 user: vagrant deploy_path: /var/www/deploy_test
$ vi deploy.php … 作成されたファイルを編集
<?php namespace Deployer; require 'recipe/common.php'; // Configuration set('repository', 'git@bitbucket.org:refirio/test.git'); … デプロイしたいリポジトリが設定済みになっている set('git_tty', true); // [Optional] Allocate tty for git on first deployment set('shared_files', []); set('shared_dirs', []); set('writable_dirs', []); // Hosts inventory('file.yml'); … 接続設定を読み込み /* … 元の接続設定はコメントアウトする host('project.com') ->stage('production') ->set('deploy_path', '/var/www/project.com'); host('beta.project.com') ->stage('beta') ->set('deploy_path', '/var/www/project.com'); */ // Tasks desc('Restart PHP-FPM service'); task('php-fpm:restart', function () { // The user must have rights for restart service // /etc/sudoers: username ALL=NOPASSWD:/bin/systemctl restart php-fpm.service run('sudo systemctl restart php-fpm.service'); }); after('deploy:symlink', 'php-fpm:restart'); desc('Deploy your project'); task('deploy', [ 'deploy:prepare', 'deploy:lock', 'deploy:release', 'deploy:update_code', 'deploy:shared', 'deploy:writable', // 'deploy:vendors', … プロジェクトにComposerが含まれないならコメントアウトする 'deploy:clear_paths', 'deploy:symlink', 'deploy:unlock', 'cleanup', 'success' ]); // [Optional] if deploy fails automatically unlock. after('deploy:failed', 'deploy:unlock');
$ dep deploy develop … この時点で実行しても、リポジトリにアクセスできずにエラーになる Executing task deploy:prepare Executing task deploy:lock Executing task deploy:release Executing task deploy:update_code The authenticity of host 'bitbucket.org (18.205.93.0)' can't be established. RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'bitbucket.org,18.205.93.0' (RSA) to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. Connection to 192.168.33.11 closed. Executing task deploy:failed Executing task deploy:unlock [Symfony\Component\Process\Exception\ProcessFailedException] The command "ssh -A -tt vagrant@192.168.33.11 '/usr/bin/git clone --depth 1 --recursive -q git@bitbucket.org:refirio/test.git /var/www/deploy_test/releases/1 2>&1'" failed. Exit Code: 128(Invalid exit argument) Working directory: /home/vagrant/deploy_init Output: ================ Error Output: ================ deploy [-p|--parallel] [-l|--limit LIMIT] [--no-hooks] [--log LOG] [--roles ROLES] [--hosts HOSTS] [-o|--option OPTION] [--] [<stage>] $ ssh-keygen -t rsa … デプロイ先サーバ(デプロイ元サーバではない)で鍵を作成し、リポジトリ(GitHubやBitbucket)に登録する $ vi ~/.ssh/id_rsa.pub $ dep deploy develop … 今度はデプロイできる Executing task deploy:prepare Executing task deploy:lock Executing task deploy:release Executing task deploy:update_code Connection to 192.168.33.11 closed. Ok Executing task deploy:shared Executing task deploy:writable Executing task deploy:clear_paths Executing task deploy:symlink Executing task php-fpm:restart Executing task deploy:unlock Executing task cleanup Executing task success Successfully deployed!
■デプロイの確認 対象サーバで確認すると、以下のようにファイルが配置されている
# pwd /var/www/deploy_test # ll total 0 lrwxrwxrwx 1 vagrant vagrant 10 Nov 16 14:54 current -> releases/1 … releasesの中で一番新しいものへのシンボリックリンク drwxrwxr-x 3 vagrant vagrant 14 Nov 16 14:54 releases … デプロイ履歴 drwxrwxr-x 2 vagrant vagrant 6 Nov 16 14:54 shared … releases間で共通して使われるディレクトリ # ll current/ … 公開したい内容が確認できる total 8 drwxr-xr-x 2 vagrant vagrant 6 Nov 16 15:11 css -rw-r--r-- 1 vagrant vagrant 10 Nov 16 15:11 index.php drwxr-xr-x 2 vagrant vagrant 6 Nov 16 15:11 js
■デプロイ内容の公開 一例だがWebサーバを以下のように設定することで、デプロイ内容を公開できる
# vi /etc/nginx/nginx.conf
#root /var/www/html; root /var/www/deploy_test/current; #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME /var/www/deploy_test/current$fastcgi_script_name;
# service nginx restart
■引き続き 以下を検証しておきたい ・nginxユーザでデプロイ ・ログファイルなど、アプリケーションのバージョンに関わらず利用するものは shared_dirs や shared_files で指定するみたい ・ロールバック ・シンボリックリンクの手動での張り替え ・一切の変更がなくても別ディレクトリに丸ごとデプロイされるのは仕様? 各タスクの意味は以下に記載されている DeployerによるPHPデプロイ : エキサイト公式 エンジニアブログ https://blog.excite.co.jp/exdev/27205935/ ソースコードは以下にあるので、実際の処理内容を確認できる deployphp/deployer: A deployment tool written in PHP with support for popular frameworks out of the box https://github.com/deployphp/deployer このツールに関係なく、シンボルリンクで公開ディレクトリ切り替えを試したい publicフォルダにシンボリックリンクを貼る - Laravel学習帳 https://laraweb.net/environment/1332/

Advertisement