Skip to content

Commit 7b2d267

Browse files
author
ImageManager Bot
committed
feat(ci): delete cicd branch on non-merged runs too (no stale-branch buildup)
New CLEANUP_BRANCH job deletes the cicd branch when MERGE_TO_MAIN was SKIPPED — i.e. the run did not succeed (build/push failed, public approval rejected, invalid manifest). It does NOT fire on a merge conflict (MERGE_TO_MAIN failure: branch kept for manual merge), and uses !cancelled() so a superseded/cancelled run never deletes a branch a newer run already owns. Needs SUMMARY so deletion is after SUMMARY's checkout. Best-effort (exits 0).
1 parent 68fed61 commit 7b2d267

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

.github/workflows/ci-dispatch.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,45 @@ jobs:
237237
acacia_bucket: ${{ vars.ACACIA_BUCKETNAME }}
238238
acacia_sif_bucket: ${{ vars.ACACIA_SIF_BUCKETNAME }}
239239
secrets: inherit
240+
241+
CLEANUP_BRANCH:
242+
# Delete the cicd branch when the run did NOT end in a merge (build/push
243+
# failed, public approval rejected, or invalid manifest) so failed runs don't
244+
# accumulate stale branches. Fires only when MERGE_TO_MAIN was SKIPPED — NOT
245+
# when it FAILED (merge conflict), where the branch is kept for manual merge.
246+
# Uses !cancelled() (not always()) so a run that was superseded/cancelled —
247+
# where a newer run already owns this branch — never deletes it out from under
248+
# the new run. Needs SUMMARY so deletion happens only after SUMMARY's checkout.
249+
needs: [SUMMARY, MERGE_TO_MAIN]
250+
if: |
251+
!cancelled() && needs.MERGE_TO_MAIN.result == 'skipped'
252+
runs-on: ubuntu-latest
253+
permissions:
254+
contents: write
255+
steps:
256+
- name: Delete stale cicd branch
257+
env:
258+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
259+
BRANCH: ${{ github.ref_name }}
260+
REPO: ${{ github.repository }}
261+
run: |
262+
set -uo pipefail
263+
echo "Run did not produce a merge — deleting stale branch ${BRANCH}…"
264+
dcode=$(curl -sS -o /dev/null -w '%{http_code}' -X DELETE \
265+
-H "Authorization: Bearer ${GH_TOKEN}" \
266+
-H "Accept: application/vnd.github+json" \
267+
-H "X-GitHub-Api-Version: 2022-11-28" \
268+
"https://api.github.com/repos/${REPO}/git/refs/heads/${BRANCH}")
269+
echo "delete HTTP ${dcode}"
270+
{
271+
echo "## 🧹 Branch cleanup"
272+
if [ "$dcode" = "204" ]; then
273+
echo "🧹 Run did not produce a merge — deleted stale branch \`${BRANCH}\` (recreated on next upload)."
274+
elif [ "$dcode" = "422" ]; then
275+
echo "ℹ️ Branch \`${BRANCH}\` already gone."
276+
else
277+
echo "ℹ️ Branch \`${BRANCH}\` not deleted (HTTP ${dcode})."
278+
fi
279+
} >> "$GITHUB_STEP_SUMMARY"
280+
# Best-effort housekeeping — never pile a red job on top of the real failure.
281+
exit 0

0 commit comments

Comments
 (0)