|
| 1 | +name: ledger scan (disk -> ledger reconcile PR) |
| 2 | + |
| 3 | +# 每日扫盘对账:台账是"请求即下载"时由 bot 回写的,但回写链路有已知脆点 |
| 4 | +# (fork 辅助分支重跑 push race、Actions 无建 PR 权限、人工落盘未登记——#151/#163 即此)。 |
| 5 | +# 本 workflow 以盘为准做并集对账:只补录"盘上有、台账无"的条目,绝不删除 |
| 6 | +# (清理是 model-cache-cleanup 的职责),产物是一个 PR,checks 全绿自动 squash-merge |
| 7 | +# (与 model-library.yml 的 merge job 同设计:人工介入点在配置,不在合入)。 |
| 8 | +# |
| 9 | +# 前提:仓库设置需允许 Actions 建 PR(Settings→Actions→General→ |
| 10 | +# "Allow GitHub Actions to create and approve pull requests")。不开则降级为 |
| 11 | +# 推分支+warning,留人工开 PR(同 ledger 的既有降级路径)。 |
| 12 | + |
| 13 | +on: |
| 14 | + schedule: |
| 15 | + - cron: "37 2 * * *" # 每日 02:37Z(北京 10:37),错峰避开整点(PR#146) |
| 16 | + workflow_dispatch: {} |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | +concurrency: |
| 23 | + group: ledger-scan |
| 24 | + cancel-in-progress: false |
| 25 | + |
| 26 | +jobs: |
| 27 | + prepare: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + outputs: |
| 30 | + arc-matrix: ${{ steps.m.outputs.arc-matrix }} |
| 31 | + host-matrix: ${{ steps.m.outputs.host-matrix }} |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + - id: m |
| 35 | + run: | |
| 36 | + jq -c '[to_entries[] | select(.value.runner != null) |
| 37 | + | select((.value.mode // "host") == "arc") |
| 38 | + | {cluster: .key, runner: .value.runner, |
| 39 | + hf: .value.caches.huggingface, ms: .value.caches.modelscope}]' \ |
| 40 | + model-library/clusters.json > /tmp/arc.json |
| 41 | + jq -c '[to_entries[] | select(.value.runner != null) |
| 42 | + | select((.value.mode // "host") != "arc") |
| 43 | + | {cluster: .key, runner: .value.runner}]' \ |
| 44 | + model-library/clusters.json > /tmp/host.json |
| 45 | + echo "arc-matrix=$(cat /tmp/arc.json)" >> "$GITHUB_OUTPUT" |
| 46 | + echo "host-matrix=$(cat /tmp/host.json)" >> "$GITHUB_OUTPUT" |
| 47 | + echo "arc=$(jq -r '.[].cluster' /tmp/arc.json | paste -sd, -)" |
| 48 | + echo "host=$(jq -r '.[].cluster' /tmp/host.json | paste -sd, -)" |
| 49 | +
|
| 50 | + scan-arc: |
| 51 | + needs: prepare |
| 52 | + if: needs.prepare.outputs.arc-matrix != '[]' |
| 53 | + name: scan ${{ matrix.cluster }} |
| 54 | + runs-on: ${{ matrix.runner }} |
| 55 | + # 扫描只读,但仍与下载同组排队,避免和正在写盘的 job 抢 IO |
| 56 | + concurrency: |
| 57 | + group: ledger-scan-${{ matrix.runner }} |
| 58 | + cancel-in-progress: false |
| 59 | + container: |
| 60 | + image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/sync-tools:latest |
| 61 | + env: |
| 62 | + MODELSCOPE_CACHE: ${{ matrix.ms }} |
| 63 | + HF_HOME: ${{ matrix.hf }} |
| 64 | + strategy: |
| 65 | + fail-fast: false |
| 66 | + matrix: |
| 67 | + include: ${{ fromJson(needs.prepare.outputs.arc-matrix) }} |
| 68 | + steps: |
| 69 | + - name: Fetch repo snapshot via codeload (mainland-safe, no github.com git) |
| 70 | + run: | |
| 71 | + curl -fsSL --retry 4 --retry-delay 5 -m 600 \ |
| 72 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 73 | + -o /tmp/repo.tgz "https://codeload.github.com/${{ github.repository }}/tar.gz/${{ github.sha }}" |
| 74 | + mkdir -p "$GITHUB_WORKSPACE" |
| 75 | + tar xzf /tmp/repo.tgz -C "$GITHUB_WORKSPACE" --strip-components=1 |
| 76 | + env: |
| 77 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + - name: Scan disk |
| 79 | + run: bash .github/scripts/scan-landed-ledger.sh '${{ matrix.cluster }}' "/tmp/scan-${{ matrix.cluster }}.json" |
| 80 | + - uses: actions/upload-artifact@v4 |
| 81 | + with: |
| 82 | + name: scan-${{ matrix.cluster }} |
| 83 | + path: /tmp/scan-${{ matrix.cluster }}.json |
| 84 | + |
| 85 | + scan-host: |
| 86 | + needs: prepare |
| 87 | + if: needs.prepare.outputs.host-matrix != '[]' |
| 88 | + name: scan ${{ matrix.cluster }} |
| 89 | + runs-on: ${{ matrix.cluster }} |
| 90 | + concurrency: |
| 91 | + group: ledger-scan-${{ matrix.cluster }} |
| 92 | + cancel-in-progress: false |
| 93 | + strategy: |
| 94 | + fail-fast: false |
| 95 | + matrix: |
| 96 | + include: ${{ fromJson(needs.prepare.outputs.host-matrix) }} |
| 97 | + steps: |
| 98 | + - name: Fetch repo snapshot via codeload (mainland-safe, no github.com git) |
| 99 | + run: | |
| 100 | + curl -fsSL --retry 4 --retry-delay 5 -m 600 \ |
| 101 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 102 | + -o /tmp/repo.tgz "https://codeload.github.com/${{ github.repository }}/tar.gz/${{ github.sha }}" |
| 103 | + mkdir -p "$GITHUB_WORKSPACE" |
| 104 | + tar xzf /tmp/repo.tgz -C "$GITHUB_WORKSPACE" --strip-components=1 |
| 105 | + env: |
| 106 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + - name: Resolve cache env from host profile |
| 108 | + run: | |
| 109 | + SUFFIX='${{ matrix.cluster }}'; SUFFIX="${SUFFIX#model-sync-}" |
| 110 | + bash -lc ". /etc/profile.d/model-tools.sh 2>/dev/null; [ -f /etc/profile.d/model-tools-$SUFFIX.sh ] && . /etc/profile.d/model-tools-$SUFFIX.sh; env | grep -E '^(HF_|HUGGINGFACE_|MODELSCOPE_)'" | tee -a "$GITHUB_ENV" |
| 111 | + - name: Scan disk |
| 112 | + run: bash .github/scripts/scan-landed-ledger.sh '${{ matrix.cluster }}' "/tmp/scan-${{ matrix.cluster }}.json" |
| 113 | + - uses: actions/upload-artifact@v4 |
| 114 | + with: |
| 115 | + name: scan-${{ matrix.cluster }} |
| 116 | + path: /tmp/scan-${{ matrix.cluster }}.json |
| 117 | + |
| 118 | + ledger-pr: |
| 119 | + needs: [prepare, scan-arc, scan-host] |
| 120 | + if: >- |
| 121 | + always() && needs.prepare.result == 'success' && |
| 122 | + (needs.scan-arc.result == 'success' || needs.scan-host.result == 'success') |
| 123 | + runs-on: ubuntu-latest |
| 124 | + outputs: |
| 125 | + pr: ${{ steps.open.outputs.pr }} |
| 126 | + branch: ${{ steps.commit.outputs.branch }} |
| 127 | + steps: |
| 128 | + - uses: actions/checkout@v4 |
| 129 | + - uses: actions/download-artifact@v4 |
| 130 | + with: |
| 131 | + pattern: "scan-*" |
| 132 | + path: /tmp/scans |
| 133 | + - name: Reconcile ledgers (add-only union) |
| 134 | + id: commit |
| 135 | + env: |
| 136 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + run: | |
| 138 | + set -uo pipefail |
| 139 | + BR="ledger/scan-$(date -u +%Y%m%d)-${GITHUB_RUN_ID}" |
| 140 | + git config user.name "model-library-bot" |
| 141 | + git config user.email "model-library-bot@users.noreply.github.com" |
| 142 | + git checkout -qb "$BR" |
| 143 | + summary="" |
| 144 | + changed=0 |
| 145 | + for d in /tmp/scans/scan-*; do |
| 146 | + cluster=$(basename "$d" | sed 's/^scan-//') |
| 147 | + LIB="model-library/$cluster.json" |
| 148 | + mkdir -p "$(dirname "$LIB")" |
| 149 | + [ -f "$LIB" ] || printf '{"models": [], "datasets": []}\n' > "$LIB" |
| 150 | + res=$(jq -n --slurpfile s $d/scan-*.json --slurpfile l "$LIB" ' |
| 151 | + def mkey: (.platform + "/" + .organization + "/" + .model_name); |
| 152 | + def dkey: (.platform + "/" + .organization + "/" + .dataset_name); |
| 153 | + { added_m: ($s[0].models | map(select(mkey as $k | ($l[0].models | map(mkey) | index($k)) | not))), |
| 154 | + added_d: ($s[0].datasets | map(select(dkey as $k | ($l[0].datasets | map(dkey) | index($k)) | not))) } | |
| 155 | + .merged = { models: (($l[0].models + $s[0].models) | unique_by(mkey)), |
| 156 | + datasets: (($l[0].datasets + $s[0].datasets) | unique_by(dkey)) }') |
| 157 | + am=$(jq -r '.added_m | length' <<<"$res"); ad=$(jq -r '.added_d | length' <<<"$res") |
| 158 | + if [ "$am" != 0 ] || [ "$ad" != 0 ]; then |
| 159 | + jq '.merged' <<<"$res" > /tmp/lib.json && mv /tmp/lib.json "$LIB" |
| 160 | + git add "$LIB" |
| 161 | + changed=1 |
| 162 | + summary="${summary}| ${cluster} | +${am} models / +${ad} datasets |\n" |
| 163 | + fi |
| 164 | + done |
| 165 | + if [ "$changed" = 0 ]; then echo "ledger already in sync with disks"; exit 0; fi |
| 166 | + git commit -qm "library: ledger scan $(date -u +%F) (add-only disk reconcile)" |
| 167 | + git push -q --force origin "HEAD:refs/heads/$BR" |
| 168 | + echo "branch=$BR" >> "$GITHUB_OUTPUT" |
| 169 | + printf '%s' "$summary" > /tmp/scan-summary.md |
| 170 | + - name: Open PR |
| 171 | + id: open |
| 172 | + if: steps.commit.outputs.branch != '' |
| 173 | + env: |
| 174 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 175 | + run: | |
| 176 | + BR='${{ steps.commit.outputs.branch }}' |
| 177 | + body=$(printf '每日扫盘对账(run %s):以盘为准补录台账缺失条目,只增不删。\n\n| 集群 | 新增 |\n|--|--|\n' "$GITHUB_RUN_ID") |
| 178 | + body="$body$(cat /tmp/scan-summary.md)" |
| 179 | + body="$body$(printf '\n\nresolve https://github.com/opensourceways/backlog/issues/2647\n')" |
| 180 | + if url=$(gh pr create --base main --head "$BR" --title "library: ledger scan $(date -u +%F)" --body "$body" 2>&1); then |
| 181 | + echo "pr=$url" >> "$GITHUB_OUTPUT" |
| 182 | + echo "$url" |
| 183 | + else |
| 184 | + echo "::warning::建 PR 失败(多半是仓库未开启 Actions 建 PR 权限);分支 $BR 已推送,请手动开 PR。输出: $url" |
| 185 | + fi |
| 186 | +
|
| 187 | + merge: |
| 188 | + needs: ledger-pr |
| 189 | + if: needs.ledger-pr.outputs.pr != '' |
| 190 | + runs-on: ubuntu-latest |
| 191 | + steps: |
| 192 | + - name: Squash-merge when checks are green |
| 193 | + env: |
| 194 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 195 | + REPO: ${{ github.repository }} |
| 196 | + PR: ${{ needs.ledger-pr.outputs.pr }} |
| 197 | + run: | |
| 198 | + set -uo pipefail |
| 199 | + PR_NUM=$(echo "$PR" | grep -oE '[0-9]+$') |
| 200 | + t=0 |
| 201 | + while [ "$t" -lt 900 ]; do |
| 202 | + info=$(gh api "repos/$REPO/pulls/$PR_NUM" --jq '[.merged,.state,.head.sha,.mergeable_state]|@tsv') |
| 203 | + merged=${info%%$'\t'*}; rest=${info#*$'\t'} |
| 204 | + state=${rest%%$'\t'*}; rest=${rest#*$'\t'} |
| 205 | + sha=${rest%%$'\t'*}; ms=${rest#*$'\t'} |
| 206 | + if [ "$merged" = "true" ]; then echo "already merged"; exit 0; fi |
| 207 | + if [ "$state" != "open" ]; then echo "PR closed?"; exit 1; fi |
| 208 | + if [ "$ms" = "dirty" ]; then echo "::error::与 base 冲突,留人工"; exit 1; fi |
| 209 | + fails=$(gh api "repos/$REPO/commits/$sha/check-runs?per_page=100" --paginate \ |
| 210 | + --jq '.check_runs[] | select(.status=="completed" and .conclusion!="" and .conclusion!="success" and .conclusion!="skipped" and .conclusion!="neutral") | .name' | sort -u | paste -sd, -) |
| 211 | + if [ -n "$fails" ]; then echo "::error::head $sha 存在失败检查: $fails"; exit 1; fi |
| 212 | + if [ "$ms" = "clean" ]; then |
| 213 | + if gh pr merge "$PR_NUM" --repo "$REPO" --squash; then echo "merged PR #$PR_NUM"; exit 0; fi |
| 214 | + if [ "$(gh api "repos/$REPO/pulls/$PR_NUM" --jq .merged)" = "true" ]; then echo "concurrently merged"; exit 0; fi |
| 215 | + fi |
| 216 | + sleep 15; t=$((t+15)) |
| 217 | + done |
| 218 | + echo "::warning::15 分钟内未自动合入(mergeable 未就绪或超时),留人工" |
| 219 | + exit 1 |
0 commit comments