Skip to content

Commit 4598d48

Browse files
authored
*: harden generated Bazel artifact workflow (#68243)
ref #68199
1 parent c6056f8 commit 4598d48

3 files changed

Lines changed: 105 additions & 7 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Check Bazel Prepare
2+
on:
3+
pull_request:
4+
branches:
5+
- master
6+
7+
permissions:
8+
contents: read
9+
10+
concurrency:
11+
group: check-bazel-prepare-${{ github.event.pull_request.number }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
check:
16+
name: Check Bazel Prepare
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v6
20+
with:
21+
repository: ${{ github.event.pull_request.head.repo.full_name }}
22+
ref: ${{ github.event.pull_request.head.sha }}
23+
fetch-depth: 0
24+
persist-credentials: false
25+
- name: Set up Go
26+
uses: actions/setup-go@v6
27+
with:
28+
go-version: 1.25
29+
cache: false
30+
- name: Set up Bazelisk
31+
uses: bazel-contrib/setup-bazel@0.16.0
32+
with:
33+
bazelisk-cache: false
34+
repository-cache: false
35+
external-cache: false
36+
- name: Check Bazel Prepare
37+
shell: bash
38+
run: |
39+
set -euo pipefail
40+
# Avoid the TiDB CI path that assumes Jenkins-owned temp directories.
41+
unset CI
42+
make check-bazel-prepare

.github/workflows/generate-bazel-files.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ jobs:
4242
sed -i '/ats.apps.svc/d' DEPS.bzl
4343
sed -i '/bazel-cache/d' WORKSPACE
4444
sed -i '/ats.apps.svc/d' WORKSPACE
45-
make bazel_prepare
45+
bazel run //:gazelle
46+
bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro DEPS.bzl%go_deps -build_file_proto_mode=disable -prune
47+
bazel run \
48+
--run_under="cd ${PWD} && " \
49+
//tools/tazel:tazel
50+
51+
tmp_out="$(mktemp -d -t tidbbzl.XXXXXX)"
52+
trap 'rm -rf "${tmp_out}"' EXIT
53+
bazel run //cmd/mirror -- --mirror > "${tmp_out}/DEPS.bzl"
54+
cp "${tmp_out}/DEPS.bzl" DEPS.bzl
4655
- name: Restore non-generated files
4756
shell: bash
4857
run: |
@@ -62,10 +71,16 @@ jobs:
6271
fi
6372
6473
case "${path}" in
74+
build/BUILD.bazel|build/*/BUILD.bazel)
75+
;;
76+
WORKSPACE|WORKSPACE.bazel|build|build/*)
77+
echo "Unsafe generated file path: ${path}" >&2
78+
exit 1
79+
;;
6580
DEPS.bzl|*.bazel|*.bzl)
6681
;;
6782
*)
68-
echo "Unexpected file changed by bazel_prepare: ${path}" >&2
83+
echo "Unexpected generated file path: ${path}" >&2
6984
exit 1
7085
;;
7186
esac

.github/workflows/update-bazel-files.yml

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ jobs:
5959
6060
if [[ "${head_repo}" != "${REPOSITORY}" ]]; then
6161
echo "same_repo=false" >> "${GITHUB_OUTPUT}"
62-
exit 0
62+
else
63+
echo "same_repo=true" >> "${GITHUB_OUTPUT}"
6364
fi
6465
65-
echo "same_repo=true" >> "${GITHUB_OUTPUT}"
66-
6766
if [[ "${pr_head_sha}" != "${RUN_HEAD_SHA}" ]]; then
6867
echo "stale=true" >> "${GITHUB_OUTPUT}"
6968
exit 0
@@ -75,6 +74,42 @@ jobs:
7574
shell: bash
7675
run: |
7776
echo "Skip stale bazel update for PR #${{ steps.pr.outputs.pr_number }}."
77+
- name: Download Fork Bazel Files Artifact
78+
if: steps.pr.outputs.same_repo != 'true' && steps.pr.outputs.stale != 'true'
79+
uses: actions/download-artifact@v4
80+
with:
81+
name: bazel-files
82+
path: bazel-artifact
83+
repository: ${{ github.repository }}
84+
run-id: ${{ github.event.workflow_run.id }}
85+
github-token: ${{ github.token }}
86+
- name: Report fork PR Bazel changes
87+
if: steps.pr.outputs.same_repo != 'true' && steps.pr.outputs.stale != 'true'
88+
shell: bash
89+
env:
90+
PR_NUMBER: ${{ steps.pr.outputs.pr_number }}
91+
REPOSITORY: ${{ github.repository }}
92+
RUN_ID: ${{ github.event.workflow_run.id }}
93+
run: |
94+
set -euo pipefail
95+
96+
patch_path="${PWD}/bazel-artifact/bazel.patch"
97+
if [[ ! -f "${patch_path}" ]]; then
98+
echo "Missing bazel.patch artifact for fork PR #${PR_NUMBER}." >&2
99+
exit 1
100+
fi
101+
102+
if [[ ! -s "${patch_path}" ]]; then
103+
echo "No generated Bazel changes for fork PR #${PR_NUMBER}."
104+
exit 0
105+
fi
106+
107+
{
108+
echo "Generated Bazel changes cannot be committed automatically for fork PR #${PR_NUMBER}."
109+
echo "Run: gh run download ${RUN_ID} --repo ${REPOSITORY} --name bazel-files --dir bazel-artifact && git apply bazel-artifact/bazel.patch"
110+
} | tee -a "${GITHUB_STEP_SUMMARY}"
111+
112+
exit 1
78113
- name: Checkout PR branch
79114
if: steps.pr.outputs.same_repo == 'true' && steps.pr.outputs.stale != 'true'
80115
uses: actions/checkout@v6
@@ -101,7 +136,7 @@ jobs:
101136
set -euo pipefail
102137
103138
patch_path="${PWD}/bazel-artifact/bazel.patch"
104-
summary="$(git apply --summary "${patch_path}")"
139+
summary="$(git -C pr apply --summary "${patch_path}")"
105140
106141
while IFS= read -r line; do
107142
case "${line}" in
@@ -126,6 +161,12 @@ jobs:
126161
echo "Unexpected patch path: ${path}" >&2
127162
exit 1
128163
;;
164+
build/BUILD.bazel|build/*/BUILD.bazel)
165+
;;
166+
WORKSPACE|WORKSPACE.bazel|build|build/*)
167+
echo "Unsafe patch path: ${path}" >&2
168+
exit 1
169+
;;
129170
DEPS.bzl|*.bazel|*.bzl)
130171
;;
131172
*)
@@ -139,7 +180,7 @@ jobs:
139180
exit 1
140181
fi
141182
seen_paths["${path}"]=1
142-
done < <(git apply --numstat -z "${patch_path}")
183+
done < <(git -C pr apply --numstat -z "${patch_path}")
143184
144185
git -C pr apply --check --index "${patch_path}"
145186
git -C pr apply --index "${patch_path}"

0 commit comments

Comments
 (0)