Laravelバージョンアップ対応時のgit worktree活用法
はじめに
Laravel のバージョンアップ対応を行う際、既存の保守対応と並行して作業を進める必要がある場面があります。そんな時に便利なのが git worktree を使った並行開発環境の構築です。
この記事では、git worktree と Docker を組み合わせて、既存環境に影響を与えることなくバージョンアップ対応を行うため、git worktreeを利用していきます。
環境概要
ディレクトリ構成
- 既存ソース:
~/project/src
- worktree用ソース:
~/project/worktree
PHP バージョン
- 既存環境: PHP 7.4、Laravel 8
- worktree環境: PHP 8.4、Laravel 12
アクセス先
- 既存環境: localhost:80
- worktree環境: localhost:8080
Docker 環境の設定
1. worktree用コンテナの Dockerfile の作成
まず、worktree 用の PHP コンテナ設定を作成します。
php-tree/Dockerfile
を作成し、PHP 8.4 環境を構築します。
2. Nginx 設定の追加
worktree 用の Nginx 設定ファイルを追加します。
nginx/conf.d/worktree.conf
server {
listen 8080 default_server;
server_name localhost;
root /var/www/worktree/public;
index index.php index.html index.htm;
charset utf-8;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
client_max_body_size 20m;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
location / {
try_files $uri $uri/ /index.php?$query_string;
add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "Fri, 01 Jan 1990 00:00:00 GMT";
}
location ~ \.php$ {
root /var/www/worktree/public;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-tree:9000;
fastcgi_index index.php;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffering off;
}
location /assets/ {
access_log off;
}
location ~ /\.ht {
deny all;
}
# setup https with ALB
if ($http_x_forwarded_proto = http) {
return 301 https://$host$request_uri;
}
# Deny some crawlers
if ($http_user_agent ~* (HTTrack|HTMLParser|libwww|nmap|nikto|wikto|sf|sqlmap|bsqlbf|w3af|acunetix|havij|appscan) ) {
return 444;
}
# Deny certain Referers (case insensitive)
if ($http_referer ~* (poker|sex|girl) ) {
return 444;
}
# Better website experience for IE users
add_header X-UA-Compatible "IE=Edge,chrome=1";
# Deny showing resource into iframe
add_header X-Frame-Options DENY;
# Disable autodetect Conntent-Type for IE users
add_header X-Content-Type-Options nosniff;
# Activates the built-XSS-defense for IE users
add_header X-XSS-Protection "1; mode=block;";
# Gzip Settings
gzip on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
3. compose.yml の編集
既存phpサービス
php:
build: ./containers/php
depends_on:
- postgres
- mailpit
working_dir: /var/www/laravel
volumes:
- ./src:/var/www/laravel:cached
networks:
default:
php-tree サービスの追加
php-tree:
build: ./containers/php-tree
depends_on:
- postgres
- mailpit
working_dir: /var/www/worktree
volumes:
- ./worktree:/var/www/worktree:cached
networks:
default:
nginx サービスの更新
nginx:
image: nginx
ports:
- "80:80"
- "8080:8080"
- "443:443"
- "4000:4000"
depends_on:
- php
volumes:
- ./containers/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./containers/nginx/conf.d:/etc/nginx/conf.d
- ./src:/var/www/laravel:cached
- ./worktree:/var/www/worktree:cached
networks:
default:
動作確認
設定完了後、以下の URL でそれぞれの環境にアクセスできることを確認します。
- 既存環境:
http://localhost:80
- worktree環境:
http://localhost:8080
メリット
1. 並行開発が可能
既存環境での保守対応と、新バージョンでの開発作業を同時に進められます。
2. 環境の分離
異なる PHP バージョンでの動作確認が容易に行えます。
3. データベース共有
今回はデータベースを分けていないため、同じデータでの動作確認が可能です。
4. 切り替えが簡単
ブラウザのタブを切り替えるだけで、異なる環境での確認ができます。
注意点
データベースの変更
データベーススキーマの変更が伴う場合は、マイグレーションの実行タイミングに注意が必要です。
完全に分離する場合はデータベースコンテナをもう1つ作成してください。
ポート番号の管理
複数の環境を立ち上げる場合、ポート番号の競合に注意しましょう。
リソース使用量
複数のコンテナが同時に動作するため、システムリソースの使用量が増加します。
まとめ
git worktree と Docker を組み合わせることで、Laravel のバージョンアップ対応を効率的に進めることができます。既存環境への影響を最小限に抑えながら、新しいバージョンでの開発・テストが可能となるため、安全かつ効率的なバージョンアップ作業が実現できます。
この手法は Laravel に限らず、他のフレームワークでも応用可能なので、ぜひ活用してみてください。
Discussion