|
| 1 | +name: Sync Backfill |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '53 * * * *' |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + days: |
| 9 | + description: '1 回の実行で遡る日数' |
| 10 | + default: '3' |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: cron-${{ github.ref }} |
| 14 | + cancel-in-progress: false |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + |
| 19 | +env: |
| 20 | + TARGET_BRANCH: gh-pages |
| 21 | + |
| 22 | +jobs: |
| 23 | + backfill: |
| 24 | + runs-on: ubuntu-24.04 |
| 25 | + strategy: |
| 26 | + matrix: |
| 27 | + php-version: |
| 28 | + - '8.4' |
| 29 | + steps: |
| 30 | + - name: Setup PHP ${{ matrix.php-version }} |
| 31 | + uses: shivammathur/setup-php@v2 |
| 32 | + with: |
| 33 | + php-version: ${{ matrix.php-version }} |
| 34 | + |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v7 |
| 37 | + with: |
| 38 | + fetch-depth: 0 |
| 39 | + |
| 40 | + - name: Get composer cache directory |
| 41 | + id: composer-cache |
| 42 | + run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT" |
| 43 | + |
| 44 | + - name: Cache composer packages |
| 45 | + uses: actions/cache@v6 |
| 46 | + with: |
| 47 | + path: ${{ steps.composer-cache.outputs.dir }} |
| 48 | + key: ${{ runner.os }}-php${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }} |
| 49 | + restore-keys: | |
| 50 | + ${{ runner.os }}-php${{ matrix.php-version }}-composer- |
| 51 | +
|
| 52 | + - name: Install dependencies (without scraper) |
| 53 | + run: | |
| 54 | + composer remove bvp/scraper --no-update || true |
| 55 | + composer install --prefer-dist --no-interaction --no-progress |
| 56 | +
|
| 57 | + - name: Require bvp/scraper ^6.0 |
| 58 | + run: | |
| 59 | + composer require bvp/scraper:^6.0 --no-update |
| 60 | + composer update bvp/scraper --with-dependencies |
| 61 | + composer install --prefer-dist --no-interaction --no-progress |
| 62 | +
|
| 63 | + - name: Sync with remote JSON files |
| 64 | + run: | |
| 65 | + git config --global user.name "github-actions[bot]" |
| 66 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 67 | + git checkout -- composer.json composer.lock 2>/dev/null || true |
| 68 | + git pull --rebase origin "${TARGET_BRANCH}" |
| 69 | +
|
| 70 | + - name: Sync JSON files |
| 71 | + id: backfill |
| 72 | + run: | |
| 73 | + set -e |
| 74 | +
|
| 75 | + STOP_FILE="docs/v3/.backfill-limit-reached" |
| 76 | + if [ -f "$STOP_FILE" ]; then |
| 77 | + echo "既に取得限界に達しています($STOP_FILE を参照)。スキップします。" |
| 78 | + echo "files_changed=false" >> "$GITHUB_OUTPUT" |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | +
|
| 82 | + DAYS="${{ github.event.inputs.days || '1' }}" |
| 83 | + CHANGED="" |
| 84 | +
|
| 85 | + for i in $(seq 1 "$DAYS"); do |
| 86 | + oldest=$(find docs/v3 -regextype posix-extended -regex '.*/[0-9]{4}/[0-9]{8}\.json' | sort | head -n1) |
| 87 | + if [ -z "$oldest" ]; then |
| 88 | + echo "既存の日付付きJSONが見つかりません。中断します。" |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +
|
| 92 | + oldest_ymd=$(basename "$oldest" .json) |
| 93 | + target_date=$(date -d "${oldest_ymd} -1 day" +%Y-%m-%d) |
| 94 | + target_ymd=$(date -d "$target_date" +%Y%m%d) |
| 95 | + target_y=$(date -d "$target_date" +%Y) |
| 96 | +
|
| 97 | + echo "::group::Backfill ${target_date}" |
| 98 | + set +e |
| 99 | + php bin/sync-backfill.php v3 "$target_date" |
| 100 | + code=$? |
| 101 | + set -e |
| 102 | + echo "::endgroup::" |
| 103 | +
|
| 104 | + if [ $code -eq 2 ]; then |
| 105 | + echo "${target_date} のデータが取得できませんでした。取得限界とみなして停止します。" |
| 106 | + mkdir -p docs/v3 |
| 107 | + echo "${target_date}" > "$STOP_FILE" |
| 108 | + CHANGED="${CHANGED} ${STOP_FILE}" |
| 109 | + break |
| 110 | + elif [ $code -ne 0 ]; then |
| 111 | + echo "::error::${target_date} の同期に失敗しました (exit=$code)" |
| 112 | + exit $code |
| 113 | + fi |
| 114 | +
|
| 115 | + f="docs/v3/${target_y}/${target_ymd}.json" |
| 116 | + if ! jq empty "$f" 2>/dev/null; then |
| 117 | + echo "::error::invalid JSON: $f" |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + CHANGED="${CHANGED} ${f}" |
| 121 | + done |
| 122 | +
|
| 123 | + if [ -z "$CHANGED" ]; then |
| 124 | + echo "files_changed=false" >> "$GITHUB_OUTPUT" |
| 125 | + else |
| 126 | + echo "files_changed=true" >> "$GITHUB_OUTPUT" |
| 127 | + echo "$CHANGED" > /tmp/changed_files.txt |
| 128 | + fi |
| 129 | +
|
| 130 | + - name: Commit JSON files |
| 131 | + if: steps.backfill.outputs.files_changed == 'true' |
| 132 | + env: |
| 133 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + run: | |
| 135 | + git add $(cat /tmp/changed_files.txt) |
| 136 | + git commit -m "chore: deploy JSON files to docs/v3" || { |
| 137 | + echo "Nothing to commit." |
| 138 | + exit 0 |
| 139 | + } |
| 140 | + git push origin HEAD:"${TARGET_BRANCH}" || (git pull --rebase origin "${TARGET_BRANCH}" && git push origin HEAD:"${TARGET_BRANCH}") |
0 commit comments