|
1 | 1 | name: Update Bazel Files |
2 | 2 | on: |
3 | | - pull_request_target: |
4 | | - branches: |
5 | | - - master |
| 3 | + workflow_run: |
| 4 | + workflows: |
| 5 | + - Generate Bazel Files |
6 | 6 | types: |
7 | | - - opened |
8 | | - - synchronize |
9 | | - - reopened |
10 | | - - ready_for_review |
| 7 | + - completed |
11 | 8 |
|
12 | 9 | permissions: |
| 10 | + actions: read |
13 | 11 | contents: write |
14 | | - pull-requests: read |
15 | 12 |
|
16 | 13 | concurrency: |
17 | | - group: update-bazel-files-${{ github.event.pull_request.number }} |
| 14 | + group: update-bazel-files-${{ github.event.workflow_run.id }} |
18 | 15 | cancel-in-progress: true |
19 | 16 |
|
20 | 17 | jobs: |
21 | 18 | update: |
22 | 19 | name: Update Bazel Files |
23 | 20 | if: > |
24 | | - github.event.pull_request.head.repo.full_name == github.repository || |
25 | | - github.event.pull_request.maintainer_can_modify |
| 21 | + github.event.workflow_run.conclusion == 'success' && |
| 22 | + github.event.workflow_run.event == 'pull_request' |
26 | 23 | runs-on: ubuntu-latest |
27 | 24 | steps: |
28 | | - - uses: actions/checkout@v6 |
| 25 | + - name: Resolve PR metadata |
| 26 | + id: pr |
| 27 | + shell: bash |
| 28 | + env: |
| 29 | + GH_TOKEN: ${{ github.token }} |
| 30 | + REPOSITORY: ${{ github.repository }} |
| 31 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 32 | + run: | |
| 33 | + set -euo pipefail |
| 34 | +
|
| 35 | + pr_number="$(gh api "repos/${REPOSITORY}/actions/runs/${RUN_ID}" --jq '.pull_requests[0].number')" |
| 36 | + head_repo="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.repo.full_name')" |
| 37 | + head_ref="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.ref')" |
| 38 | + pr_head_sha="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.sha')" |
| 39 | + run_head_sha="$(gh api "repos/${REPOSITORY}/actions/runs/${RUN_ID}" --jq '.head_sha')" |
| 40 | +
|
| 41 | + printf 'pr_number=%s\n' "${pr_number}" >> "${GITHUB_OUTPUT}" |
| 42 | + printf 'head_repo=%s\n' "${head_repo}" >> "${GITHUB_OUTPUT}" |
| 43 | + printf 'head_ref=%s\n' "${head_ref}" >> "${GITHUB_OUTPUT}" |
| 44 | +
|
| 45 | + if [[ "${head_repo}" != "${REPOSITORY}" ]]; then |
| 46 | + echo "same_repo=false" >> "${GITHUB_OUTPUT}" |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +
|
| 50 | + echo "same_repo=true" >> "${GITHUB_OUTPUT}" |
| 51 | +
|
| 52 | + if [[ "${pr_head_sha}" != "${run_head_sha}" ]]; then |
| 53 | + echo "stale=true" >> "${GITHUB_OUTPUT}" |
| 54 | + exit 0 |
| 55 | + fi |
| 56 | +
|
| 57 | + echo "stale=false" >> "${GITHUB_OUTPUT}" |
| 58 | + - name: Skip stale workflow run |
| 59 | + if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale == 'true' |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + echo "Skip stale bazel update for PR #${{ steps.pr.outputs.pr_number }}." |
| 63 | + - name: Checkout PR branch |
| 64 | + if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale != 'true' |
| 65 | + uses: actions/checkout@v6 |
29 | 66 | with: |
30 | | - repository: ${{ github.event.pull_request.head.repo.full_name }} |
31 | | - ref: ${{ github.event.pull_request.head.ref }} |
| 67 | + repository: ${{ steps.pr.outputs.head_repo }} |
| 68 | + ref: ${{ steps.pr.outputs.head_ref }} |
| 69 | + path: pr |
32 | 70 | fetch-depth: 0 |
33 | | - token: ${{ secrets.BAZEL_PREPARE_PUSH_TOKEN || github.token }} |
34 | | - - name: Build Prepare |
35 | | - uses: ./.github/actions/tidb_build |
| 71 | + token: ${{ github.token }} |
| 72 | + persist-credentials: false |
| 73 | + - name: Download Bazel Files Artifact |
| 74 | + if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale != 'true' |
| 75 | + uses: actions/download-artifact@v4 |
36 | 76 | with: |
37 | | - bazelrc: ${{ secrets.BAZELRC }} |
38 | | - gcp_sa_key: ${{ secrets.GCP_SA_KEY }} |
39 | | - go-version: 1.25 |
40 | | - - name: Run Bazel Prepare |
| 77 | + name: bazel-files |
| 78 | + path: bazel-artifact |
| 79 | + repository: ${{ github.repository }} |
| 80 | + run-id: ${{ github.event.workflow_run.id }} |
| 81 | + github-token: ${{ github.token }} |
| 82 | + - name: Apply Bazel Files Artifact |
| 83 | + if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale != 'true' |
41 | 84 | shell: bash |
42 | 85 | run: | |
43 | | - #!/bin/bash |
44 | | - unset CI |
45 | | - sed -i '/bazel-cache/d' DEPS.bzl |
46 | | - sed -i '/ats.apps.svc/d' DEPS.bzl |
47 | | - sed -i '/bazel-cache/d' WORKSPACE |
48 | | - sed -i '/ats.apps.svc/d' WORKSPACE |
49 | | - make bazel_prepare |
| 86 | + set -euo pipefail |
| 87 | +
|
| 88 | + patch_path="${PWD}/bazel-artifact/bazel.patch" |
| 89 | + summary="$(git apply --summary "${patch_path}")" |
| 90 | +
|
| 91 | + while IFS= read -r line; do |
| 92 | + case "${line}" in |
| 93 | + " rename "*|" copy "*) |
| 94 | + echo "Unsupported patch summary: ${line}" >&2 |
| 95 | + exit 1 |
| 96 | + ;; |
| 97 | + esac |
| 98 | + done <<< "${summary}" |
| 99 | +
|
| 100 | + declare -A seen_paths=() |
| 101 | +
|
| 102 | + while IFS= read -r -d '' record; do |
| 103 | + if [[ -z "${record}" ]]; then |
| 104 | + continue |
| 105 | + fi |
| 106 | +
|
| 107 | + IFS=$'\t' read -r _ _ path <<< "${record}" |
| 108 | +
|
| 109 | + case "${path}" in |
| 110 | + ""|/*|..|../*|*/..|*/../*) |
| 111 | + echo "Unexpected patch path: ${path}" >&2 |
| 112 | + exit 1 |
| 113 | + ;; |
| 114 | + DEPS.bzl|*.bazel|*.bzl) |
| 115 | + ;; |
| 116 | + *) |
| 117 | + echo "Unexpected patch path: ${path}" >&2 |
| 118 | + exit 1 |
| 119 | + ;; |
| 120 | + esac |
| 121 | +
|
| 122 | + if [[ -n "${seen_paths[${path}]:-}" ]]; then |
| 123 | + echo "Duplicate patch path: ${path}" >&2 |
| 124 | + exit 1 |
| 125 | + fi |
| 126 | + seen_paths["${path}"]=1 |
| 127 | + done < <(git apply --numstat -z "${patch_path}") |
| 128 | +
|
| 129 | + git -C pr apply --check --index "${patch_path}" |
| 130 | + git -C pr apply --index "${patch_path}" |
50 | 131 | - name: Commit Bazel Files |
51 | | - uses: stefanzweifel/git-auto-commit-action@v5 |
52 | | - with: |
53 | | - branch: ${{ github.event.pull_request.head.ref }} |
54 | | - commit_message: "chore: update bazel file" |
55 | | - disable_globbing: true |
56 | | - file_pattern: >- |
57 | | - DEPS.bzl |
58 | | - :(top,glob)**/*.bazel |
59 | | - :(top,glob)**/*.bzl |
| 132 | + if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale != 'true' |
| 133 | + shell: bash |
| 134 | + env: |
| 135 | + HEAD_REF: ${{ steps.pr.outputs.head_ref }} |
| 136 | + PUSH_TOKEN: ${{ github.token }} |
| 137 | + TARGET_REPOSITORY: ${{ steps.pr.outputs.head_repo }} |
| 138 | + run: | |
| 139 | + set -euo pipefail |
| 140 | +
|
| 141 | + if git -C pr diff --cached --quiet -- DEPS.bzl ':(glob)**/*.bazel' ':(glob)**/*.bzl'; then |
| 142 | + echo "No generated Bazel changes to commit." |
| 143 | + exit 0 |
| 144 | + fi |
| 145 | +
|
| 146 | + git -C pr config user.name "Ti Chi Robot" |
| 147 | + git -C pr config user.email "ti-community-prow-bot@tidb.io" |
| 148 | + git -C pr commit -m "chore: update bazel file" |
| 149 | + git -C pr push "https://x-access-token:${PUSH_TOKEN}@github.com/${TARGET_REPOSITORY}.git" "HEAD:${HEAD_REF}" |
0 commit comments