Skip to content

Commit 2a30b7a

Browse files
newbe36524claude
andcommitted
feat: Add fallback method for Aliyun sync without publish result
When publish result file doesn't exist in old releases, use fallback method that fetches all tags directly from Azure ACR. This ensures backward compatibility with releases created before the publish result feature was added. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 36e5b58 commit 2a30b7a

1 file changed

Lines changed: 104 additions & 18 deletions

File tree

.github/workflows/azure-to-aliyun-image-sync.yml

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,42 @@ jobs:
113113
ASSET_URL=$(echo "$RELEASE_DATA" | jq -r --arg asset_name "$ASSET_NAME" '.assets[] | select(.name == $asset_name) | .browser_download_url')
114114
115115
if [ -z "$ASSET_URL" ] || [ "$ASSET_URL" == "null" ]; then
116-
echo "Error: Publish result file ${ASSET_NAME} not found in release ${RELEASE_TAG}"
117-
exit 1
118-
fi
116+
echo "Publish result file ${ASSET_NAME} not found in release ${RELEASE_TAG}"
117+
echo "use_fallback=true" >> $GITHUB_OUTPUT
118+
echo "Will use fallback method: sync all tags from Azure ACR"
119+
else
120+
echo "Downloading publish result from: ${ASSET_URL}"
121+
curl -s -o azure-publish-result.json "${ASSET_URL}"
119122
120-
echo "Downloading publish result from: ${ASSET_URL}"
121-
curl -s -o azure-publish-result.json "${ASSET_URL}"
123+
if [ ! -s "azure-publish-result.json" ]; then
124+
echo "Error: Failed to download publish result file"
125+
exit 1
126+
fi
122127
123-
if [ ! -s "azure-publish-result.json" ]; then
124-
echo "Error: Failed to download publish result file"
125-
exit 1
128+
echo "publish_result_file=azure-publish-result.json" >> $GITHUB_OUTPUT
129+
echo "use_fallback=false" >> $GITHUB_OUTPUT
130+
echo "Successfully downloaded publish result file"
131+
echo "Content:"
132+
cat azure-publish-result.json
126133
fi
127134
128-
echo "publish_result_file=azure-publish-result.json" >> $GITHUB_OUTPUT
129-
echo "Successfully downloaded publish result file"
130-
echo "Content:"
131-
cat azure-publish-result.json
135+
- name: Determine sync method
136+
id: sync-method
137+
run: |
138+
USE_FALLBACK="${{ steps.fetch-publish-result.outputs.use_fallback }}"
139+
VERSION="${{ steps.fetch-publish-result.outputs.version }}"
140+
141+
if [ "$USE_FALLBACK" == "true" ]; then
142+
echo "sync_method=fallback" >> $GITHUB_OUTPUT
143+
echo "Using fallback method: will fetch all tags from Azure ACR"
144+
else
145+
echo "sync_method=publish_result" >> $GITHUB_OUTPUT
146+
echo "Using publish result file method"
147+
fi
132148
133149
- name: Parse publish result for image list
134150
id: parse-images
151+
if: steps.sync-method.outputs.sync_method == 'publish_result'
135152
run: |
136153
PUBLISH_RESULT_FILE="${{ steps.fetch-publish-result.outputs.publish_result_file }}"
137154
@@ -159,22 +176,90 @@ jobs:
159176
echo "image_versions=${VERSIONS}" >> $GITHUB_OUTPUT
160177
echo "Versions to sync: ${VERSIONS}"
161178
179+
- name: Fallback: Fetch tags from Azure ACR
180+
id: fallback-tags
181+
if: steps.sync-method.outputs.sync_method == 'fallback'
182+
run: |
183+
VERSION="${{ steps.fetch-publish-result.outputs.version }}"
184+
SOURCE_REGISTRY="${{ env.SOURCE_REGISTRY }}"
185+
IMAGE_NAME="hagicode"
186+
187+
echo "Using fallback method for version ${VERSION}"
188+
echo "Fetching all tags from Azure ACR..."
189+
190+
# Get all tags from Azure ACR
191+
TAGS=$(curl -s -u "${{ env.AZURE_ACR_USERNAME }}:${{ env.AZURE_ACR_PASSWORD }}" \
192+
"https://${SOURCE_REGISTRY}/v2/${IMAGE_NAME}/tags/list" | \
193+
jq -r '.tags[]' 2>/dev/null || echo "")
194+
195+
echo "Available tags: $TAGS"
196+
197+
# Filter version tags (v*.*.*), special tags, and tags matching the specific version
198+
VERSIONS=""
199+
200+
# First, check if we can find tags matching the specific version
201+
if [ "$VERSION" != "latest" ]; then
202+
# Add the exact version tag (with and without v prefix)
203+
if echo "$TAGS" | grep -q "^${VERSION}$"; then
204+
VERSIONS="${VERSIONS}${VERSIONS:+,}${VERSION}"
205+
fi
206+
if echo "$TAGS" | grep -q "^v${VERSION}$"; then
207+
VERSIONS="${VERSIONS}${VERSIONS:+,}v${VERSION}"
208+
fi
209+
fi
210+
211+
# Add special tags if they exist
212+
for tag in $TAGS; do
213+
if [[ "$tag" == "latest" ]] || [[ "$tag" == "base" ]]; then
214+
# Avoid duplicates
215+
if ! echo "$VERSIONS" | grep -q "$tag"; then
216+
VERSIONS="${VERSIONS}${VERSIONS:+,}${tag}"
217+
fi
218+
fi
219+
done
220+
221+
# If no versions found, use latest as fallback
222+
if [ -z "$VERSIONS" ]; then
223+
VERSIONS="latest"
224+
echo "No matching tags found, using latest"
225+
fi
226+
227+
echo "azure_registry=${SOURCE_REGISTRY}" >> $GITHUB_OUTPUT
228+
echo "image_versions=${VERSIONS}" >> $GITHUB_OUTPUT
229+
echo "Versions to sync: ${VERSIONS}"
230+
231+
- name: Set registry and versions for sync
232+
id: sync-config
233+
run: |
234+
if [ "${{ steps.sync-method.outputs.sync_method }}" == "publish_result" ]; then
235+
AZURE_REGISTRY="${{ steps.parse-images.outputs.azure_registry }}"
236+
VERSIONS="${{ steps.parse-images.outputs.image_versions }}"
237+
else
238+
AZURE_REGISTRY="${{ steps.fallback-tags.outputs.azure_registry }}"
239+
VERSIONS="${{ steps.fallback-tags.outputs.image_versions }}"
240+
fi
241+
242+
echo "azure_registry=${AZURE_REGISTRY}" >> $GITHUB_OUTPUT
243+
echo "image_versions=${VERSIONS}" >> $GITHUB_OUTPUT
244+
echo "Final config: registry=${AZURE_REGISTRY}, versions=${VERSIONS}"
245+
162246
- name: Display sync information
163247
run: |
164248
echo "=========================================="
165249
echo "Azure to Aliyun Image Sync"
166250
echo "=========================================="
167251
echo "Version: ${{ steps.fetch-publish-result.outputs.version }}"
168252
echo "Release Tag: ${{ steps.fetch-publish-result.outputs.release_tag }}"
169-
echo "Azure Registry: ${{ steps.parse-images.outputs.azure_registry }}"
253+
echo "Sync Method: ${{ steps.sync-method.outputs.sync_method }}"
254+
echo "Azure Registry: ${{ steps.sync-config.outputs.azure_registry }}"
170255
echo "Target Registry: ${{ env.TARGET_REGISTRY }}"
171-
echo "Images to sync: ${{ steps.parse-images.outputs.image_versions }}"
256+
echo "Images to sync: ${{ steps.sync-config.outputs.image_versions }}"
172257
echo "=========================================="
173258
174259
- name: Login to Azure ACR
175260
uses: docker/login-action@v3
176261
with:
177-
registry: ${{ steps.parse-images.outputs.azure_registry }}
262+
registry: ${{ steps.sync-config.outputs.azure_registry }}
178263
username: ${{ env.AZURE_ACR_USERNAME }}
179264
password: ${{ env.AZURE_ACR_PASSWORD }}
180265

@@ -196,9 +281,9 @@ jobs:
196281
- name: Generate image-syncer config
197282
id: config
198283
run: |
199-
SOURCE_REGISTRY="${{ steps.parse-images.outputs.azure_registry }}"
284+
SOURCE_REGISTRY="${{ steps.sync-config.outputs.azure_registry }}"
200285
TARGET_REGISTRY="${{ env.TARGET_REGISTRY }}"
201-
VERSIONS="${{ steps.parse-images.outputs.image_versions }}"
286+
VERSIONS="${{ steps.sync-config.outputs.image_versions }}"
202287
203288
# Create config.yaml using echo commands to avoid heredoc issues
204289
echo "registry:" > config.yaml
@@ -268,6 +353,7 @@ jobs:
268353
echo "Image sync completed successfully!"
269354
echo "=========================================="
270355
echo "Version: ${{ steps.fetch-publish-result.outputs.version }}"
356+
echo "Sync Method: ${{ steps.sync-method.outputs.sync_method }}"
271357
echo "Images: ${{ steps.config.outputs.versions }}"
272358
echo "=========================================="
273359
@@ -278,7 +364,7 @@ jobs:
278364
if [ -n "$WEBHOOK_URL" ]; then
279365
echo "Sending failure notification..."
280366
curl -s -X POST -H "Content-Type: application/json" \
281-
-d "{\"msg_type\":\"interactive\",\"card\":{\"header\":{\"title\":{\"tag\":\"plain_text\",\"content\":\"⚠️ Image Sync Failed\"},\"template\":\"red\"},\"elements\":[{\"tag\":\"markdown\",\"content\":\"**Azure to Aliyun Image Sync Failed**\\n\\n**Version:** ${{ steps.fetch-publish-result.outputs.version }}\\n**Run:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}]}}" \
367+
-d "{\"msg_type\":\"interactive\",\"card\":{\"header\":{\"title\":{\"tag\":\"plain_text\",\"content\":\"⚠️ Image Sync Failed\"},\"template\":\"red\"},\"elements\":[{\"tag\":\"markdown\",\"content\":\"**Azure to Aliyun Image Sync Failed**\\n\\n**Version:** ${{ steps.fetch-publish-result.outputs.version }}\\n**Sync Method:** ${{ steps.sync-method.outputs.sync_method }}\\n**Run:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}]}}" \
282368
"$WEBHOOK_URL"
283369
echo "Notification sent!"
284370
else

0 commit comments

Comments
 (0)