GitHub Actions自己修復型ワークフロー実装ガイド¶
この記事の対象者
- CI/CDパイプラインの障害対応を自動化したい中級者
この記事のポイント¶
- ビルド失敗時の自動リトライ戦略実装
- テストエラーの原因分析と自動修正
- 依存関係問題の自己解決メカニズム構築
なぜこの問題が今重要か¶
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