Skip to content

Commit 59358a0

Browse files
committed
Updated promotion check logic to account for squash merges
1 parent 0e685b2 commit 59358a0

1 file changed

Lines changed: 89 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,101 @@ jobs:
5656
username: ${{ github.actor }}
5757
password: ${{ secrets.GITHUB_TOKEN }}
5858

59-
- name: Check if image already exists
59+
- name: Check if image already exists or can be re-tagged
6060
id: check
6161
run: |
62-
IMAGE="ghcr.io/${{ github.repository_owner }}/python-container-builder:${{ matrix.python_version }}-${{ github.sha }}"
63-
echo "Checking if ${IMAGE} already exists..."
62+
CURRENT_IMAGE="ghcr.io/${{ github.repository_owner }}/python-container-builder:${{ matrix.python_version }}-${{ github.sha }}"
63+
echo "=== Image Existence Check ==="
64+
echo "Checking if ${CURRENT_IMAGE} already exists..."
6465
65-
if docker manifest inspect "${IMAGE}" >/dev/null 2>&1; then
66-
echo "✅ Image already exists, skipping build"
66+
# Check if image with current commit SHA exists
67+
if docker manifest inspect "${CURRENT_IMAGE}" >/dev/null 2>&1; then
68+
echo "✅ Image already exists for current commit, skipping build"
6769
echo "exists=true" >> $GITHUB_OUTPUT
68-
else
69-
echo "❌ Image does not exist, will build"
70-
echo "exists=false" >> $GITHUB_OUTPUT
70+
exit 0
7171
fi
7272
73+
echo "❌ Image does not exist for current commit: ${{ github.sha }}"
74+
75+
# If this is a push to main (likely a PR merge), try to find and re-tag the PR image
76+
if [ "${{ github.event_name }}" == "push" ] && [ "${{ github.ref }}" == "refs/heads/main" ]; then
77+
echo ""
78+
echo "=== Attempting PR Image Re-tag (Squash Merge Handling) ==="
79+
80+
# Method 1: Extract PR number from commit message
81+
# Squash merges typically include "#123" in the commit message
82+
PR_NUM=$(git log -1 --pretty=%B | grep -oP '#\K\d+' | head -1 || echo "")
83+
84+
if [ -n "$PR_NUM" ]; then
85+
echo "Found PR #${PR_NUM} in commit message"
86+
PR_IMAGE="ghcr.io/${{ github.repository_owner }}/python-container-builder:pr-${PR_NUM}-${{ matrix.python_version }}"
87+
88+
echo "Checking if PR image exists: ${PR_IMAGE}"
89+
if docker manifest inspect "${PR_IMAGE}" >/dev/null 2>&1; then
90+
echo "✅ Found PR image, re-tagging to new commit SHA..."
91+
92+
# Re-tag the PR image with the new commit SHA (no rebuild!)
93+
docker buildx imagetools create \
94+
"${PR_IMAGE}" \
95+
--tag "${CURRENT_IMAGE}"
96+
97+
if [ $? -eq 0 ]; then
98+
echo "✅ Successfully re-tagged PR image to commit SHA"
99+
echo " Source: ${PR_IMAGE}"
100+
echo " Target: ${CURRENT_IMAGE}"
101+
echo "exists=true" >> $GITHUB_OUTPUT
102+
exit 0
103+
else
104+
echo "❌ Re-tagging failed, will build from scratch"
105+
fi
106+
else
107+
echo "⚠️ PR image not found: ${PR_IMAGE}"
108+
fi
109+
else
110+
echo "⚠️ Could not extract PR number from commit message"
111+
fi
112+
113+
# Method 2: Use GitHub API to find PR by merge commit SHA
114+
echo ""
115+
echo "Trying GitHub API to find PR by commit SHA..."
116+
PR_NUM=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
117+
"https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls" \
118+
| jq -r '.[0].number // empty')
119+
120+
if [ -n "$PR_NUM" ]; then
121+
echo "Found PR #${PR_NUM} via GitHub API"
122+
PR_IMAGE="ghcr.io/${{ github.repository_owner }}/python-container-builder:pr-${PR_NUM}-${{ matrix.python_version }}"
123+
124+
echo "Checking if PR image exists: ${PR_IMAGE}"
125+
if docker manifest inspect "${PR_IMAGE}" >/dev/null 2>&1; then
126+
echo "✅ Found PR image, re-tagging to new commit SHA..."
127+
128+
docker buildx imagetools create \
129+
"${PR_IMAGE}" \
130+
--tag "${CURRENT_IMAGE}"
131+
132+
if [ $? -eq 0 ]; then
133+
echo "✅ Successfully re-tagged PR image to commit SHA"
134+
echo " Source: ${PR_IMAGE}"
135+
echo " Target: ${CURRENT_IMAGE}"
136+
echo "exists=true" >> $GITHUB_OUTPUT
137+
exit 0
138+
else
139+
echo "❌ Re-tagging failed, will build from scratch"
140+
fi
141+
else
142+
echo "⚠️ PR image not found: ${PR_IMAGE}"
143+
fi
144+
else
145+
echo "⚠️ Could not find PR via GitHub API"
146+
fi
147+
fi
148+
149+
echo ""
150+
echo "=== Final Decision ==="
151+
echo "❌ No existing image found, will build from scratch"
152+
echo "exists=false" >> $GITHUB_OUTPUT
153+
73154
- name: Extract metadata
74155
if: steps.check.outputs.exists == 'false'
75156
id: meta

0 commit comments

Comments
 (0)