Skip to content

ci: improve cron workflow functionality#61

Merged
shimomo merged 3 commits intogh-pagesfrom
ci/update-checkout-step
Aug 31, 2025
Merged

ci: improve cron workflow functionality#61
shimomo merged 3 commits intogh-pagesfrom
ci/update-checkout-step

Conversation

@shimomo
Copy link
Copy Markdown
Contributor

@shimomo shimomo commented Aug 31, 2025

git pull --rebase --autostash の安定性を高めるために fetch-depth: 0 を追加しました。さらに、Composer 依存関係のキャッシュ化と重複実行の制御も行いました。

Summary by CodeRabbit

  • Chores
    • 定期実行ワークフローに同時実行制御を追加して、古いジョブの取り消し動作を明確化。
    • CIのチェックアウト設定を更新し、リポジトリの全履歴を取得するよう変更。履歴依存タスクの安定性が向上。
    • 依存キャッシュ(Composer)を導入し、依存解決の再利用とワークフローの高速化を図る。

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Aug 31, 2025

Walkthrough

GitHub Actions ワークフロー .github/workflows/cron.yml を更新し、トップレベルの concurrency を追加、actions/checkoutfetch-depth: 0 に変更してフルクローン化し、Composer のキャッシュ取得・復元ステップを導入した。その他のステップは維持された。

Changes

Cohort / File(s) Change Summary
CI: workflow updates
\.github/workflows/cron.yml
追加: トップレベル concurrency ブロック(group: cron-${{ github.ref }}, cancel-in-progress: false)を導入。actions/checkoutfetch-depth: 0 に変更(フル履歴取得)。Composer キャッシュ用ステップを追加(キャッシュディレクトリ取得、actions/cache によるキャッシュ保存/復元、キーに hashFiles('**/composer.lock') を使用)。その他のステップは変更なし。

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: キャッシュ保存完了
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

ぴょん、CIの夜に跳ねてきたよ🐇
履歴まるごと抱えて、深く潜るクローン
キャッシュを抱いて、次のランへスタート
時計の針が回れば、僕らまた走るよ
ふわふわ尾で合図、デプロイへ進め!

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ci/update-checkout-step

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

📥 Commits

Reviewing files that changed from the base of the PR and between 2ddf4da and 6dcf5ea.

📒 Files selected for processing (1)
  • .github/workflows/cron.yml (1 hunks)

@shimomo
Copy link
Copy Markdown
Contributor Author

shimomo commented Aug 31, 2025

@coderabbitai
改善点を PR に追記してください。

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Aug 31, 2025

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
    fi

3. 変更がない場合のスキップ処理

不必要なコミットを避けるため、変更がない場合はスキップ:

- 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 push

4. セキュリティの向上

GITHUB_TOKEN の権限を明示的に設定:

permissions:
  contents: write
  actions: read

5. 実行時間の制限

無限ループを防ぐためタイムアウトを設定:

jobs:
  update:
    runs-on: ubuntu-latest
    timeout-minutes: 10

これらの改善により、ワークフローの安定性、パフォーマンス、セキュリティが向上します。

🐰 💭 ✨


🏁 Script executed:

#!/bin/bash
# Let's examine the workflow file to understand the context
cat .github/workflows/cron.yml

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: true

3. スクレイピング失敗時のエラーハンドリング

- 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
    fi

4. 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."
    fi

5. 実行時間制限とタイムアウト設定

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
Learnt from: CR
PR: BoatraceOpenAPI/programs#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-08-29T16:13:40.187Z
Learning: コードレビューは日本語で行う

@shimomo shimomo changed the title ci: fetch full git history in checkout step for reliable rebase ci: improve cron workflow functionality Aug 31, 2025
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

📥 Commits

Reviewing files that changed from the base of the PR and between 6dcf5ea and 5570f26.

📒 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目的に合致しており妥当です。

@shimomo shimomo merged commit f9dbd4e into gh-pages Aug 31, 2025
6 checks passed
@shimomo shimomo deleted the ci/update-checkout-step branch August 31, 2025 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant