CI/CDプロセスの基本構成
Rustの高速WebフレームワークであるActix Webにおいて、CI/CDプロセスを効率的に構築するための手順を解説します。コードの変更から本番環境への自動デプロイまでを一貫して自動化することで、開発効率と品質の向上を実現します。
環境要件の確認
CI/CD設定を始める前に、以下の条件を満たしていることを確認してください:
- プロジェクトがGitによるバージョン管理を採用している
- Cargo.tomlが正しく設定されている
- 基本的なユニットテストが実装されている
必須ツールの準備
CI/CDフローで必要なツールは以下の通りです:
- Rustコンパイラ(rustc)
- Cargo(Rustパッケージマネージャ)
- Git(バージョン管理システム)
テストの自動化
コード品質を保証するため、CIプロセス内での自動テストは必須です。
ユニットテストの実行
Actix Webプロジェクトでは、testsディレクトリ内にテストファイルが配置されます。CI設定で以下のコマンドを追加します:
cargo test --all-features
統合テストとパフォーマンステスト
より複雑なシナリオでは、統合テストとベンチマークを実行します:
cargo test --test integration
cargo bench
ビルド最適化
ビルド時間を短縮するため、依存関係のキャッシュと最適化されたビルドを設定します。
依存関係のキャッシュ
CI環境でCargoの依存関係をキャッシュすることで、毎回のダウンロードを回避します。GitHub Actionsでの例:
- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
リリースビルドの生成
最適化されたバイナリを生成するため、--releaseフラグを使用します:
cargo build --release
デプロイ戦略の設定
環境ごとに適切なデプロイフローを構築します。
開発環境への自動デプロイ
developブランチへのプッシュ時に自動デプロイを実行します:
deploy_dev:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to dev
run: ./scripts/deploy-dev.sh
if: github.ref == 'refs/heads/develop'
本番環境の手動デプロイ
本番環境のデプロイは手動トリガーを推奨します:
deploy_prod:
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v3
with:
name: app-binary
- name: Deploy to production
run: ./scripts/deploy-prod.sh
when: manual
if: github.ref == 'refs/heads/main'
GitHub Actionsの完全設定例
プロジェクトの根元に.github/workflows/ci-cd.ymlを配置します:
name: Actix Web CI/CD Pipeline
on:
push:
branches: [ main, dev ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Run tests
run: cargo test --all-features
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Build release
run: cargo build --release
- name: Archive build
uses: actions/upload-artifact@v3
with:
name: app-binary
path: target/release/app
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: app-binary
path: ./bin
- name: Deploy to production
run: |
chmod +x ./bin/deploy.sh
./bin/deploy.sh
トラブルシューティング
ビルド時間の長さ
解決策:
- 不要な依存を削除
- キャッシュ戦略の最適化
- CIランナーのリソース強化
テストの不安定性
解決策:
- テスト環境の一貫性確保
- タイムアウトの設定
- 競合条件の修正