You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Skip cherry-pick to branches locked for maintenance
Avoid attempting checkout/cherry-pick/push against a branch that has
GitHub's "lock branch" protection enabled (support ended), since the
push would only fail. Branch existence and lock status are cached per
branch to avoid repeat lookups when several commits target the same
branch.
Copy file name to clipboardExpand all lines: .github/workflows/spring-cherry-pick.yml
+35-1Lines changed: 35 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,10 @@ jobs:
61
61
# retry to fall back on: record it and keep going through the remaining commits/branches
62
62
# instead of aborting the whole loop, then fail the job at the end if anything failed.
63
63
failed=''
64
+
# Cached per branch across all commits/branches in this push, so a maintenance line
65
+
# mentioned by several commits only costs one existence check and one lock lookup.
66
+
declare -A branchExists
67
+
declare -A branchLocked
64
68
while IFS= read -r commit
65
69
do
66
70
sha=$(echo "$commit" | jq -r '.id')
@@ -72,15 +76,45 @@ jobs:
72
76
if [ "$branch" != '${{ github.ref_name }}' ]
73
77
then
74
78
79
+
if [ -z "${branchExists[$branch]+x}" ]
80
+
then
81
+
if git ls-remote --exit-code --heads origin "$branch" > /dev/null
82
+
then
83
+
branchExists[$branch]=true
84
+
else
85
+
branchExists[$branch]=false
86
+
fi
87
+
fi
88
+
75
89
# The commit message may mention a branch which does not exist (yet or different repository):
76
90
# a typo, or a maintenance line which has not been cut. Skip it with a warning instead of failing the whole run,
77
91
# so the remaining, existing branches are still cherry-picked into.
78
-
if ! git ls-remote --exit-code --heads origin "$branch" > /dev/null
92
+
if [ "${branchExists[$branch]}" = false ]
79
93
then
80
94
echo "::warning title=Skip cherry-pick::No '$branch' branch in ${{ github.repository }}. The commit $sha is not cherry-picked there"
81
95
continue
82
96
fi
83
97
98
+
if [ -z "${branchLocked[$branch]+x}" ]
99
+
then
100
+
if [ "$(gh api "repos/${{ github.repository }}/branches/$branch/protection" --jq '.lock_branch.enabled // false' 2>/dev/null)" = true ]
101
+
then
102
+
branchLocked[$branch]=true
103
+
else
104
+
branchLocked[$branch]=false
105
+
fi
106
+
fi
107
+
108
+
# A locked branch is a maintenance line for which support has ended: it is read-only,
109
+
# so checkout/cherry-pick/push would only fail. Skip it up front instead of wasting the
110
+
# attempt (and instead of a push failure going unnoticed, since only cherry-pick failures
111
+
# below are tracked in $failed).
112
+
if [ "${branchLocked[$branch]}" = true ]
113
+
then
114
+
echo "::warning title=Skip cherry-pick::Branch '$branch' in ${{ github.repository }} is locked (read-only, out of support). The commit $sha is not cherry-picked there"
115
+
continue
116
+
fi
117
+
84
118
git checkout "$branch"
85
119
86
120
# A rebase + force-push gives the same logical change a new SHA, so an equivalent
0 commit comments