Skip to content

Commit cf8e45a

Browse files
committed
github actions: Optimize kernel tree clone/fetch
Cloning the base branch takes a long time because a fetch-depth of 0 causes all history for all branches and tags to be cloned (!). In addition, the entire history of the PR branch is fetched. Only the tip of the base branch along with the new commits present in a PR need to be cloned; anything more than that is unneeded. Reduce the depth of the base branch clone to 1 commit and the PR branch fetch to the number of commits in the PR plus 1 (to get the tip of the base branch). The same philosophy is applied to fetching the mainline branch, which is substantially more complex. All that is needed _at most_ is the history between the kernel version tag and the tip of mainline, since the kernel version tag is the most recent common ancestor between us and mainline. History older than the kernel version tag is thus excluded via --shallow-exclude. And as an added bonus, skip checkout on the base branch clone since all of the python scripts inspecting the tree perform checkouts themselves. That way, no time is wasted on checking out the tree to a different SHA than the first checkout by one of the python scripts.
1 parent 9895ab4 commit cf8e45a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

.github/workflows/validate-kernel-commits.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ jobs:
1414
timeout-minutes: 120
1515

1616
steps:
17-
- name: Checkout base branch
18-
uses: actions/checkout@v4
19-
with:
20-
fetch-depth: 0
21-
ref: ${{ github.base_ref }}
17+
- name: Clone base branch
18+
run: |
19+
# Optimization: don't checkout at the start because it's superfluous
20+
git clone --depth=1 --no-checkout "${{ github.event.pull_request.base.repo.clone_url }}" -b "${{ github.base_ref }}" .
2221
2322
- name: Fetch PR branch
2423
run: |
25-
git fetch "${{ github.event.pull_request.head.repo.clone_url }}" "${{ github.head_ref }}"
24+
# Fetch all of the commits in the PR plus one more commit before,
25+
# which should be the head of the base branch
26+
git fetch --depth=$((${{ github.event.pull_request.commits }} + 1)) "${{ github.event.pull_request.head.repo.clone_url }}" "${{ github.head_ref }}"
2627
echo "HEAD_SHA=$(git rev-parse FETCH_HEAD)" >> "$GITHUB_ENV"
2728
2829
- name: Verify PR branch isn't on stale base
@@ -32,6 +33,21 @@ jobs:
3233
exit 1
3334
fi
3435
36+
- name: Fetch upstream mainline branch
37+
run: |
38+
# Determine the kernel version tag, since it is the most recent common
39+
# ancestor between the base branch and the upstream mainline branch
40+
MAKEFILE=$(git show "${{ github.base_ref }}":Makefile)
41+
VERSION=$(grep -m1 -Po '(?<=^VERSION = )\d+' <<< "$MAKEFILE")
42+
PATCHLEVEL=$(grep -m1 -Po '(?<=^PATCHLEVEL = )\d+' <<< "$MAKEFILE")
43+
44+
# Fetch upstream mainline branch without tags, fetching only history
45+
# that is newer than this kernel version tag. This reduces the amount
46+
# of commits cloned knowing that there's no need to check any commits
47+
# for Fixes commits older than what the base branch already contains
48+
git fetch --no-tags --shallow-exclude="v$VERSION.$PATCHLEVEL" origin kernel-mainline
49+
echo "MAINLINE_SHA=$(git rev-parse FETCH_HEAD)" >> "$GITHUB_ENV"
50+
3551
- name: Checkout kernel-src-tree-tools
3652
uses: actions/checkout@v4
3753
with:
@@ -55,6 +71,7 @@ jobs:
5571
--pr_branch "$HEAD_SHA" \
5672
--base_branch "${{ github.base_ref }}" \
5773
--markdown \
74+
--upstream-ref "$MAINLINE_SHA" \
5875
--check-cves | tee ../ckc_result.txt
5976
EXIT_CODE=$?
6077

0 commit comments

Comments
 (0)