コンテンツにスキップ

GitHub Actions自己修復型ワークフロー実装ガイド

この記事の対象者

  • CI/CDパイプラインの障害対応を自動化したい中級者

この記事のポイント

  1. ビルド失敗時の自動リトライ戦略実装
  2. テストエラーの原因分析と自動修正
  3. 依存関係問題の自己解決メカニズム構築

なぜこの問題が今重要か

GitHub Actionsワークフローの失敗率は平均15-20%。その70%は一時的エラーや既知の問題による。手動介入なしに自己修復できれば、開発速度が大幅に向上する。

解決ステップ概要

ステップ内容到達指標
1リトライマトリックス実装失敗率50%削減
2エラー分類と自動対処手動介入80%削減
3通知とロールバック設定復旧時間3分以内

ステップ1: インテリジェントリトライの実装

一時的なネットワークエラーやリソース不足を自動判定し、段階的にリトライ間隔を調整する戦略を実装する。

name: Self-Healing Build
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Smart Build with Retry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          retry_wait_seconds: 30
          retry_on: error
          command: |
            npm ci --cache .npm
            npm run build

ステップ2: エラー分類と自動修正

ビルドログを解析し、エラータイプに応じた修正アクションを自動実行する。

- name: Auto-Fix Known Issues
  if: failure()
  run: |
    if grep -q "ENOSPC" ${{ github.workspace }}/error.log; then
      echo "Cleaning workspace..."
      rm -rf node_modules .next
      npm cache clean --force
    elif grep -q "peer dep" ${{ github.workspace }}/error.log; then
      npm install --legacy-peer-deps
    fi

- name: Retry After Fix
  if: failure()
  run: npm run build

ステップ3: エスカレーションとロールバック

自動修正が失敗した場合の段階的エスカレーション戦略を定義する。

- name: Rollback on Critical Failure
  if: failure() && github.ref == 'refs/heads/main'
  uses: actions/github-script@v7
  with:
    script: |
      const { data: commit } = await github.rest.repos.getCommit({
        owner: context.repo.owner,
        repo: context.repo.repo,
        ref: context.sha
      });

      if (commit.parents.length > 0) {
        await github.rest.git.updateRef({
          owner: context.repo.owner,
          repo: context.repo.repo,
          ref: 'heads/main',
          sha: commit.parents[0].sha,
          force: true
        });
      }

よくある落とし穴と対処

症状原因即時対処
無限リトライmax_attempts未設定上限3回を必須設定
キャッシュ競合並列ジョブの衝突concurrencyグループ使用
権限エラーGITHUB_TOKEN権限不足permissions明示的設定
高度な自己修復パターン ### 機械学習ベースのエラー予測 過去のワークフロー実行データを分析し、失敗パターンを事前検知:
- name: ML-based Failure Prediction
  run: |
    python analyze_history.py \
      --workflow-runs 100 \
      --predict-failure-probability
### 動的リソース調整 メモリ不足エラーを検知して自動的にランナースペックを変更:
strategy:
  matrix:
    runner: [ubuntu-latest, ubuntu-latest-8-cores]
  fail-fast: false

次に読む