Skip to content

Commit 68fed61

Browse files
author
ImageManager Bot
committed
feat(ci): delete cicd branch after a successful merge to main
MERGE_TO_MAIN now deletes the cicd branch once the merge is clean (HTTP 201/204) — best-effort cleanup, a delete failure is a warning, not a job failure. Added SUMMARY to needs so the branch is removed only after SUMMARY's checkout (a parallel job fetching a commit on a just-deleted branch can fail). Branch is recreated from main on the next upload.
1 parent 270c26d commit 68fed61

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

.github/workflows/ci-dispatch.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ jobs:
147147
# approval); for private-only / devmode runs PUSH_PUBLIC is skipped and it
148148
# merges as soon as build + private push succeed. Uses the server-side merges
149149
# API (atomic, no checkout/push race). main is the default, unprotected branch.
150-
needs: [PREPARE, BUILD, PUSH_PRIV, PUSH_PUBLIC, DEPLOY]
150+
# Also needs SUMMARY so the branch is deleted only AFTER SUMMARY's checkout — a
151+
# parallel job fetching a commit on a just-deleted branch can fail. SUMMARY's
152+
# result is not gated on (it's always() / cosmetic); we just wait for it.
153+
needs: [PREPARE, BUILD, PUSH_PRIV, PUSH_PUBLIC, DEPLOY, SUMMARY]
151154
if: |
152155
!cancelled() &&
153156
needs.PREPARE.outputs.proceed_valid == 'true' &&
@@ -159,10 +162,11 @@ jobs:
159162
permissions:
160163
contents: write
161164
steps:
162-
- name: Merge branch into main
165+
- name: Merge branch into main and delete it
163166
env:
164167
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
165168
BRANCH: ${{ github.ref_name }}
169+
REPO: ${{ github.repository }}
166170
run: |
167171
set -euo pipefail
168172
echo "Promoting ${BRANCH} -> main via the GitHub merges API…"
@@ -173,7 +177,7 @@ jobs:
173177
-H "Authorization: Bearer ${GH_TOKEN}" \
174178
-H "Accept: application/vnd.github+json" \
175179
-H "X-GitHub-Api-Version: 2022-11-28" \
176-
"https://api.github.com/repos/${{ github.repository }}/merges" \
180+
"https://api.github.com/repos/${REPO}/merges" \
177181
-d "$body")
178182
echo "HTTP ${code}"; cat /tmp/merge.json 2>/dev/null || true; echo
179183
{
@@ -185,6 +189,24 @@ jobs:
185189
*) echo "❌ Merge failed (HTTP ${code}). See job log." ;;
186190
esac
187191
} >> "$GITHUB_STEP_SUMMARY"
192+
193+
# Only on a clean merge (content is safely in main) delete the now-redundant
194+
# cicd branch. Best-effort: a delete failure is a warning, not a job failure.
195+
if [ "$code" = "201" ] || [ "$code" = "204" ]; then
196+
echo "Deleting branch ${BRANCH}…"
197+
dcode=$(curl -sS -o /dev/null -w '%{http_code}' -X DELETE \
198+
-H "Authorization: Bearer ${GH_TOKEN}" \
199+
-H "Accept: application/vnd.github+json" \
200+
-H "X-GitHub-Api-Version: 2022-11-28" \
201+
"https://api.github.com/repos/${REPO}/git/refs/heads/${BRANCH}")
202+
echo "delete HTTP ${dcode}"
203+
if [ "$dcode" = "204" ]; then
204+
echo "🧹 Deleted branch \`${BRANCH}\`." >> "$GITHUB_STEP_SUMMARY"
205+
else
206+
echo "⚠️ Branch \`${BRANCH}\` not deleted (HTTP ${dcode}) — harmless, it's recreated on next upload." >> "$GITHUB_STEP_SUMMARY"
207+
fi
208+
fi
209+
188210
case "$code" in 201|204) exit 0 ;; *) exit 1 ;; esac
189211
190212
SUMMARY:

0 commit comments

Comments
 (0)