GitHub ActionsでCI/CDを始めよう
GitHub Actionsは、GitHubリポジトリに統合されたCI/CDプラットフォームです。コードのプッシュ・プルリクエスト・スケジュールなど様々なイベントをトリガーに、自動でテスト・ビルド・デプロイを実行できます。
GitHub Actionsの基本構造
# .github/workflows/ci.yml
name: CI Pipeline # ワークフロー名
# トリガーイベント
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * 1' # 毎週月曜0時に実行
jobs:
test: # ジョブ名
runs-on: ubuntu-latest # 実行環境
strategy:
matrix:
node-version: [18, 20, 22] # 複数バージョンでテスト
steps:
- name: リポジトリをチェックアウト
uses: actions/checkout@v4
- name: Node.jsをセットアップ
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: 依存関係をインストール
run: npm ci
- name: Lint実行
run: npm run lint
- name: テスト実行
run: npm run test -- --coverage
- name: カバレッジレポートをアップロード
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
実践的なCI/CDパイプライン例
Node.jsアプリのフルパイプライン
# .github/workflows/deploy.yml
name: Build and Deploy
on:
push:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ジョブ1: テスト
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: testpass
POSTGRES_DB: testdb
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgresql://postgres:testpass@localhost:5432/testdb
# ジョブ2: Dockerイメージビルド・プッシュ
build:
needs: test
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
steps:
- uses: actions/checkout@v4
- name: Docker Buildxをセットアップ
uses: docker/setup-buildx-action@v3
- name: GitHub Container Registryにログイン
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: メタデータを生成
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix={{branch}}-
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
- name: ビルドしてプッシュ
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
# ジョブ3: 本番デプロイ
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- name: SSH経由でデプロイ
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PROD_HOST }}
username: ${{ secrets.PROD_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
docker stop app || true
docker rm app || true
docker run -d \
--name app \
-p 3000:3000 \
--env-file /etc/app.env \
--restart unless-stopped \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
echo "Deploy completed at $(date)"
セキュリティスキャンの自動化
# .github/workflows/security.yml
name: Security Scan
on:
push:
branches: [main, develop]
schedule:
- cron: '0 2 * * *' # 毎日午前2時
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# 依存関係の脆弱性チェック
- name: npm audit
run: npm audit --audit-level=high
# Snykによるセキュリティスキャン
- name: Snykスキャン
uses: snyk/actions/node@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
# Trivyによるコンテナイメージスキャン
- name: Trivyスキャン
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myapp:latest'
format: 'sarif'
output: 'trivy-results.sarif'
- name: 結果をGitHubにアップロード
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
まとめ
GitHub Actionsを使ったCI/CDパイプラインは、コードの品質を自動で保証し、デプロイの手動作業を排除します。最初は単純なテスト自動化から始め、徐々にセキュリティスキャン・マルチ環境デプロイへと拡張していくことをおすすめします。