mcpField
G
github-actions-templates

github-actions-templates

github.com/hermeticormusクラウド・DevOps12376 回インストール4 週間前に更新
リポジトリ

github-actions-templates について

テスト、ビルド、デプロイを自動化するチーム向けのGitHub Actionsワークフローパターン集です。Dockerイメージの公開、Kubernetesへのデプロイ、マトリックスビルド、セキュリティスキャン、再利用可能なワークフローを扱います。

主な機能

  1. 01テストとデプロイのテンプレート
  2. 02Dockerイメージのビルドと公開
  3. 03Kubernetesデプロイパターン
  4. 04マトリックスビルドパターン
  5. 05セキュリティスキャンのワークフロー
  6. 06再利用可能なワークフロー例

活用シーン

  1. 01アプリのテストとデプロイの自動化
  2. 02Dockerイメージのビルドと公開
  3. 03アプリのKubernetesへのデプロイ
  4. 04複数環境でのテスト

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/hermeticormus/libreuiux-claude-code --skill github-actions-templates

インストール後、Claude Code で/github-actions-templatesを入力して読み込まれたか確認します。

詳細

GitHub Actions テンプレート

アプリケーションのテスト、ビルド、デプロイに対応した、本番環境ですぐに使える GitHub Actions のワークフローパターンです。

目的

さまざまな技術スタックでの継続的インテグレーションとデプロイに対応する、効率的でセキュアな GitHub Actions ワークフローを作成します。

使用する場面

  • テストとデプロイを自動化する
  • Docker イメージをビルドしてレジストリにプッシュする
  • Kubernetes クラスターにデプロイする
  • セキュリティスキャンを実行する
  • 複数の環境向けにマトリックスビルドを実装する

よく使われるワークフローパターン

パターン 1:テストワークフロー
name: Test

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x, 20.x]

    steps:
    - uses: actions/checkout@v4

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'

    - name: Install dependencies
      run: npm ci

    - name: Run linter
      run: npm run lint

    - name: Run tests
      run: npm test

    - name: Upload coverage
      uses: codecov/codecov-action@v3
      with:
        files: ./coverage/lcov.info

参考: assets/test-workflow.yml を参照してください。

パターン 2:Docker イメージのビルドとプッシュ
name: Build and Push

on:
  push:
    branches: [ main ]
    tags: [ 'v*' ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - uses: actions/checkout@v4

    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=semver,pattern={{version}}
          type=semver,pattern={{major}}.{{minor}}

    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max

参考: assets/deploy-workflow.yml を参照してください。

パターン 3:Kubernetes へのデプロイ
name: Deploy to Kubernetes

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v4
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-west-2

    - name: Update kubeconfig
      run: |
        aws eks update-kubeconfig --name production-cluster --region us-west-2

    - name: Deploy to Kubernetes
      run: |
        kubectl apply -f k8s/
        kubectl rollout status deployment/my-app -n production
        kubectl get services -n production

    - name: Verify deployment
      run: |
        kubectl get pods -n production
        kubectl describe deployment my-app -n production
パターン 4:マトリックスビルド
name: Matrix Build

on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.9', '3.10', '3.11', '3.12']

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Run tests
      run: pytest

参考: assets/matrix-build.yml を参照してください。

ワークフローのベストプラクティス

  1. アクションのバージョンを明示する(@latest ではなく @v4)
  2. 依存関係をキャッシュしてビルドを高速化する
  3. 機密データにはシークレットを使用する
  4. PR に対するステータスチェックを実装する
  5. 複数バージョンのテストにはマトリックスビルドを使用する
  6. 適切な権限を設定する
  7. よく使うパターンには再利用可能なワークフローを使用する
  8. 本番環境向けに承認ゲートを実装する
  9. 失敗時の通知ステップを追加する
  10. 機密性の高いワークロードにはセルフホストランナーを使用する

再利用可能なワークフロー

# .github/workflows/reusable-test.yml
name: Reusable Test Workflow

on:
  workflow_call:
    inputs:
      node-version:
        required: true
        type: string
    secrets:
      NPM_TOKEN:
        required: true

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}
    - run: npm ci
    - run: npm test

再利用可能なワークフローの使用方法:

jobs:
  call-test:
    uses: ./.github/workflows/reusable-test.yml
    with:
      node-version: '20.x'
    secrets:
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

セキュリティスキャン

name: Security Scan

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  security:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Run Trivy vulnerability scanner
      uses: aquasecurity/trivy-action@master
      with:
        scan-type: 'fs'
        scan-ref: '.'
        format: 'sarif'
        output: 'trivy-results.sarif'

    - name: Upload Trivy results to GitHub Security
      uses: github/codeql-action/upload-sarif@v2
      with:
        sarif_file: 'trivy-results.sarif'

    - name: Run Snyk Security Scan
      uses: snyk/actions/node@master
      env:
        SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

承認を伴うデプロイ

name: Deploy to Production

on:
  push:
    tags: [ 'v*' ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://app.example.com

    steps:
    - uses: actions/checkout@v4

    - name: Deploy application
      run: |
        echo "Deploying to production..."
        # Deployment commands here

    - name: Notify Slack
      if: success()
      uses: slackapi/slack-github-action@v1
      with:
        webhook-url: ${{ secrets.SLACK_WEBHOOK }}
        payload: |
          {
            "text": "Deployment to production completed successfully!"
          }

参考ファイル

  • assets/test-workflow.yml - テストワークフローテンプレート
  • assets/deploy-workflow.yml - デプロイワークフローテンプレート
  • assets/matrix-build.yml - マトリックスビルドテンプレート
  • references/common-workflows.md - よく使われるワークフローパターン

関連スキル

  • gitlab-ci-patterns - GitLab CI ワークフロー向け
  • deployment-pipeline-design - パイプラインアーキテクチャ向け
  • secrets-management - シークレット管理向け

コメント