From e51deb8f8736eecce013bfaf7cd79da6726db253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Thu, 19 Dec 2024 11:48:52 +0800 Subject: [PATCH] Enhance GitHub Actions workflow for Docker image synchronization: added error handling for unsupported platforms, improved logging for successful and failed syncs, and updated environment variables to store synced and failed platforms for subsequent steps. --- .github/workflows/mirror.yaml | 43 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/.github/workflows/mirror.yaml b/.github/workflows/mirror.yaml index d8ff1af..5cbe9ae 100644 --- a/.github/workflows/mirror.yaml +++ b/.github/workflows/mirror.yaml @@ -42,21 +42,35 @@ jobs: # 将平台列表转换为数组 IFS=',' read -ra PLATFORM_ARRAY <<< "${{ github.event.inputs.PLATFORMS }}" + # 用于存储成功同步的架构 + SYNCED_PLATFORMS=() + FAILED_PLATFORMS=() + # 遍历每个平台 for PLATFORM in "${PLATFORM_ARRAY[@]}"; do echo "Processing platform: $PLATFORM" - # 拉取特定平台的镜像 - docker pull --platform $PLATFORM ${{ github.event.inputs.IMAGE_NAME }}:${{ github.event.inputs.IMAGE_VERSION }} - - # 标记镜像,添加平台后缀 - PLATFORM_SUFFIX=$(echo $PLATFORM | sed 's/\//-/g') - docker tag ${{ github.event.inputs.IMAGE_NAME }}:${{ github.event.inputs.IMAGE_VERSION }} \ - ${{ github.event.inputs.TARGET_REGISTRY }}/${{ github.event.inputs.TARGET_REPOSITORY }}/${{ github.event.inputs.NEW_NAME }}:${{ github.event.inputs.IMAGE_VERSION }}-${PLATFORM_SUFFIX} - - # 推送镜像 - docker push ${{ github.event.inputs.TARGET_REGISTRY }}/${{ github.event.inputs.TARGET_REPOSITORY }}/${{ github.event.inputs.NEW_NAME }}:${{ github.event.inputs.IMAGE_VERSION }}-${PLATFORM_SUFFIX} + # 尝试拉取特定平台的镜像 + if docker pull --platform $PLATFORM ${{ github.event.inputs.IMAGE_NAME }}:${{ github.event.inputs.IMAGE_VERSION }} 2>/dev/null; then + # 标记镜像,添加平台后缀 + PLATFORM_SUFFIX=$(echo $PLATFORM | sed 's/\//-/g') + docker tag ${{ github.event.inputs.IMAGE_NAME }}:${{ github.event.inputs.IMAGE_VERSION }} \ + ${{ github.event.inputs.TARGET_REGISTRY }}/${{ github.event.inputs.TARGET_REPOSITORY }}/${{ github.event.inputs.NEW_NAME }}:${{ github.event.inputs.IMAGE_VERSION }}-${PLATFORM_SUFFIX} + + # 推送镜像 + docker push ${{ github.event.inputs.TARGET_REGISTRY }}/${{ github.event.inputs.TARGET_REPOSITORY }}/${{ github.event.inputs.NEW_NAME }}:${{ github.event.inputs.IMAGE_VERSION }}-${PLATFORM_SUFFIX} + + SYNCED_PLATFORMS+=($PLATFORM) + echo "✅ Successfully synced platform: $PLATFORM" + else + FAILED_PLATFORMS+=($PLATFORM) + echo "⚠️ Platform $PLATFORM not supported by source image, skipping..." + fi done + + # 将成功同步的平台列表保存到文件中,供后续步骤使用 + echo "SYNCED_PLATFORMS=${SYNCED_PLATFORMS[*]}" >> $GITHUB_ENV + echo "FAILED_PLATFORMS=${FAILED_PLATFORMS[*]}" >> $GITHUB_ENV - name: qyweixin send message if: ${{ env.QYWX_ROBOT_URL != '' }} @@ -67,10 +81,13 @@ jobs: with: msgtype: markdown content: | - # 镜像同步成功 + # 镜像同步完成 ``` 基础镜像地址:${{ env.IMAGE_URL }} - 同步的架构版本: - ${{ github.event.inputs.PLATFORMS }} + 成功同步的架构: + ${{ env.SYNCED_PLATFORMS }} + + 不支持的架构: + ${{ env.FAILED_PLATFORMS }} ``` \ No newline at end of file