・約2分で読めます

GitHub Actions CI/CD入門2026 — 自動テスト・デプロイをゼロから構築


PRXサーバー国内シェアNo.1のレンタルサーバー。高速・安定・サポート充実で初心者にもおすすめ。
Xサーバー 公式サイト →

GitHub Actionsは、GitHubが提供するCI/CDプラットフォームです。コードのプッシュやPRをトリガーに、テスト・ビルド・デプロイを自動化できます。パブリックリポジトリは無制限、プライベートリポジトリも月2,000分まで無料で使えます。

GitHub Actionsの基本概念

用語 説明
Workflow YAMLで書いた自動化の定義(.github/workflows/に配置)
Job Workflowの中の処理単位。並列実行可能
Step Jobの中の各処理(シェルコマンドまたはAction)
Action 再利用可能な処理のまとまり(uses:で利用)
Runner Jobが実行されるサーバー(ubuntu-latest等)

基本的なWorkflowの書き方

# .github/workflows/ci.yml
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run lint
        run: npm run lint

      - name: Run tests
        run: npm test

実践例①: Node.js + テスト + Cloudflare Pagesデプロイ

name: Build & Deploy

on:
  push:
    branches: [main]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'

      - run: npm ci

      - name: Lint
        run: npm run lint

      - name: Build
        run: npm run build

      - name: Deploy to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy dist --project-name=my-project

実践例②: Python + pytest + デプロイ

name: Python CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install pytest pytest-cov

      - name: Run tests with coverage
        run: pytest --cov=src --cov-fail-under=70

Secrets の管理

APIキー等の機密情報はSecretsとして管理します。

  1. GitHubリポジトリ → Settings → Secrets and variables → Actions
  2. 「New repository secret」でキーと値を追加
  3. ワークフロー内で ${{ secrets.SECRET_NAME }} として参照
- name: Deploy
  env:
    API_TOKEN: ${{ secrets.MY_API_TOKEN }}
  run: ./deploy.sh

キャッシュで高速化

依存関係のインストールはキャッシュすることで大幅に短縮できます。

- uses: actions/setup-node@v4
  with:
    node-version: '22'
    cache: 'npm' # package-lock.jsonのハッシュでキャッシュ
- uses: actions/setup-python@v5
  with:
    python-version: '3.12'
    cache: 'pip' # requirements.txtのハッシュでキャッシュ

Matrix Build(複数バージョンでのテスト)

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci && npm test

Workflowの条件分岐

jobs:
  deploy:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' # mainブランチのみ実行
    steps:
      - run: echo "Deploying to production"

  notify:
    runs-on: ubuntu-latest
    if: failure() # 前のJobが失敗した場合
    steps:
      - run: echo "Build failed!"

環境変数の使い方

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      NODE_ENV: production # Job全体の環境変数
    steps:
      - name: Build
        env:
          NEXT_PUBLIC_API_URL: ${{ vars.API_URL }} # Variablesも使える
        run: npm run build

よくあるパターン

用途 設定のポイント
PR自動レビュー pull_requestトリガー + lint/test
定期実行 schedule: cronで定期ジョブ
手動実行 workflow_dispatchで手動トリガー
成果物の保存 actions/upload-artifact
PR環境へのプレビュー ClouldFlare Pages / Vercel Preview

まとめ

  • ワークフローは .github/workflows/*.yml に定義する
  • on: でトリガー(push/PR/schedule等)を指定
  • jobs: で並列実行可能な処理を定義
  • Secretsで機密情報を安全に管理
  • cache:で依存関係インストールを高速化

GitHub Actionsを使うとコードをpushするたびに自動でテスト・デプロイが走る開発体制が整います。最初は単純なlint+testから始めて、徐々にデプロイ自動化まで拡張していくのがおすすめです。

関連記事