ci: improve cron workflow functionality#61
Conversation
WalkthroughGitHub Actions ワークフロー Changes
Sequence Diagram(s)sequenceDiagram
participant GH as GitHub (cron)
participant R as Runner
participant Con as Concurrency
participant AC as actions/checkout
participant Cache as actions/cache
participant Repo as Repository
GH->>Con: スケジュール実行(グループ: cron-${{ github.ref }})
Con->>R: ジョブ起動(キャンセルはしない)
R->>AC: Checkout (fetch-depth: 0)
note over AC: フルクローン(履歴・タグ取得)
AC->>Repo: Fetch full repository
Repo-->>AC: 全コミット・タグ
AC-->>R: ワークスペース(フル履歴)
R->>R: Run: Get Composer cache dir
R->>Cache: Restore cache (key: ${ runner.os }-php-${ hashFiles('**/composer.lock') })
alt cache hit
Cache-->>R: キャッシュ復元
else cache miss
Cache-->>R: 復元なし
end
R->>R: 残りのジョブ実行(既存ステップ)
R->>Cache: Save cache (on success)
Cache-->>R: キャッシュ保存完了
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
✨ Finishing Touches🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
.github/workflows/cron.yml (3)
24-25: fetch-depth: 0 の追加は妥当。rebase 安定性に効きます完全履歴取得で
git pull --rebase --autostashの衝突や「shallow history」問題を回避できます。ネットワーク負荷は増えますが、このジョブの性質上受容範囲だと思います。安定度をさらに上げるなら、明示的にブランチを指定してチェックアウトするのも手です(スケジュール実行時の既定ブランチ依存を避けるため)。
- name: Checkout Code uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 with: fetch-depth: 0 + ref: gh-pages
58-59: pull/push のターゲットを明示して事故を減らす
HEAD依存を避け、追跡ブランチ未設定や分離ヘッドでも安定する形に。- git pull --rebase --autostash - git push origin HEAD + git pull --rebase --autostash origin "${GITHUB_REF_NAME:-gh-pages}" + git push origin "HEAD:${GITHUB_REF_NAME:-gh-pages}"
8-10: 同時実行を抑止して競合を回避(任意)30分間隔の実行が長引く場合の二重起動を防ぎ、
git pull --rebaseの競合確率を下げます。permissions: contents: write + +concurrency: + group: cron-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true必要なら PR に追記する形でパッチ用意します。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
.github/workflows/cron.yml(1 hunks)
|
@coderabbitai |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain@shimomo さん、ワークフローファイルを確認して改善点を特定しました。以下の改善提案をPRに追記させていただきます: 改善提案1. 依存関係のキャッシュ化Node.js の依存関係をキャッシュして実行時間を短縮できます: - name: Cache node modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-2. エラーハンドリングの強化git操作が失敗した場合の処理を追加: - name: Pull latest changes
run: |
if ! git pull --rebase --autostash; then
echo "Rebase failed, attempting to resolve conflicts..."
git rebase --abort
git pull --no-rebase
fi3. 変更がない場合のスキップ処理不必要なコミットを避けるため、変更がない場合はスキップ: - name: Check for changes
id: changes
run: |
if git diff --quiet; then
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "changed=true" >> $GITHUB_OUTPUT
fi
- name: Commit and push
if: steps.changes.outputs.changed == 'true'
run: |
git add .
git commit -m "chore: update data [skip ci]"
git push4. セキュリティの向上GITHUB_TOKEN の権限を明示的に設定: permissions:
contents: write
actions: read5. 実行時間の制限無限ループを防ぐためタイムアウトを設定: jobs:
update:
runs-on: ubuntu-latest
timeout-minutes: 10これらの改善により、ワークフローの安定性、パフォーマンス、セキュリティが向上します。 🐰 💭 ✨ 🏁 Script executed: Length of output: 1962 @shimomo さん、ワークフローの詳細を確認しました。現在の実装は既によく構成されていますが、以下の改善点を特定しました: 改善提案1. Composer依存関係のキャッシュ化Composer依存関係をキャッシュして実行時間を大幅に短縮: - name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: |
~/.composer/cache
vendor
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-2. 並行実行の制御30分毎の実行で重複を防ぐため: concurrency:
group: cron-deploy
cancel-in-progress: true3. スクレイピング失敗時のエラーハンドリング- name: Scrape Data (v2)
run: |
if ! php scraper.php v2; then
echo "Scraping failed, but continuing to check for existing changes..."
exit 0 # Don't fail the entire workflow
fi4. Git操作の最適化と安全性向上- name: Deploy JSON for scraper v2
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# より効率的なファイル検出
if ! git diff --quiet HEAD -- 'docs/v2/*.json' || [ -n "$(git ls-files --others --exclude-standard -- 'docs/v2/*.json')" ]; then
git add docs/v2/*.json
git commit -m "chore: deploy JSON files to docs/v2 [skip ci]" || {
echo "No changes to commit"
exit 0
}
# リトライ機能付きpush
for i in {1..3}; do
if git pull --rebase --autostash && git push origin HEAD; then
echo "Successfully pushed on attempt $i"
break
elif [ $i -eq 3 ]; then
echo "Failed to push after 3 attempts"
exit 1
else
echo "Push attempt $i failed, retrying..."
sleep $((i * 5))
fi
done
else
echo "No JSON changes in docs/v2 to commit."
fi5. 実行時間制限とタイムアウト設定jobs:
deploy-v2:
name: Deploy scraper ^5.0
runs-on: ubuntu-24.04
timeout-minutes: 15 # 長時間実行を防止6. 失敗時の通知機能(オプション)- name: Notify on failure
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🚨 Cron job failed. Please check the logs.'
});7. 環境変数での設定管理env:
SCRAPER_VERSION: '^5.0'
DOCS_PATH: 'docs/v2'これらの改善により、ワークフローの安定性、効率性、保守性が大幅に向上します。特に fetch-depth: 0 の追加と Composerキャッシュ の導入が最も効果的な改善となるでしょう。 🐰 💡 🔧 🧠 Learnings used |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
.github/workflows/cron.yml (4)
8-10: スケジュールの重複実行を避けるため cancel-in-progress: true を推奨30分間隔での
scheduleなので、長引いた前回実行と競合しないようキャンセル有効化を検討してください。concurrency: group: cron-${{ github.ref }} - cancel-in-progress: false + cancel-in-progress: true
31-41: Composer キャッシュ範囲を拡張してヒット率を向上
cache-files-dirだけでも効果はありますが、cache-dir(メタデータ/HTTPキャッシュ)も含めるとインストールがさらに安定・高速になります。Composer 1 の環境互換も考慮して~/.composer/cacheも併記可。- - name: Get Composer cache directory + - name: Get Composer cache directories id: composer-cache - run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + run: | + echo "files=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + echo "dir=$(composer config cache-dir)" >> $GITHUB_OUTPUT @@ - - name: Cache Composer dependencies + - name: Cache Composer dependencies uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 with: - path: ${{ steps.composer-cache.outputs.dir }} + path: | + ${{ steps.composer-cache.outputs.files }} + ${{ steps.composer-cache.outputs.dir }} + ~/.composer/cache key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} restore-keys: | ${{ runner.os }}-php-
60-79: git pull/rebase 失敗時のフォールバックと push リトライで耐障害性を上げましょうネットワーク断やリモート更新競合時に失敗しにくくなります(再base失敗→abort→非rebase pull→push へフォールバック)。
if [ -n "$FILES" ]; then - git add $FILES - git commit -m "chore: deploy JSON files to docs/v2 [skip ci]" || exit 0 - git pull --rebase --autostash - git push origin HEAD + echo "$FILES" | xargs -r -d '\n' git add -- + git commit -m "chore: deploy JSON files to docs/v2 [skip ci]" || exit 0 + attempts=0 + until [ $attempts -ge 3 ]; do + if git pull --rebase --autostash; then + git push origin HEAD && break + else + git rebase --abort || true + git pull --no-rebase && git push origin HEAD && break + fi + attempts=$((attempts+1)) + sleep $((2 ** attempts)) + done + [ $attempts -lt 3 ] || { echo "Push failed after retries"; exit 1; } else echo "No JSON changes in docs/v2 to commit." fi
62-63: git config は --global ではなくローカル設定で十分ワークスペースに限定する方が副作用が少ないです。
- git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com"
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
.github/workflows/cron.yml(2 hunks)
🔇 Additional comments (1)
.github/workflows/cron.yml (1)
28-29: fetch-depth: 0 の追加は適切 — rebase 安定化に有効です
git pull --rebase --autostashの信頼性向上というPR目的に合致しており妥当です。
git pull --rebase --autostash の安定性を高めるために fetch-depth: 0 を追加しました。さらに、Composer 依存関係のキャッシュ化と重複実行の制御も行いました。
Summary by CodeRabbit