Build and Push Docker Image #22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push Docker Image | |
| on: | |
| push: | |
| tags: | |
| - 'v*' | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to build (e.g., v1.0.4)' | |
| required: true | |
| type: string | |
| env: | |
| REGISTRY: ghcr.io | |
| jobs: | |
| info: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image_name: ${{ steps.meta.outputs.image_name }} | |
| version: ${{ steps.meta.outputs.version }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| steps: | |
| - name: Show build info | |
| id: meta | |
| run: | | |
| IMAGE_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') | |
| # Determine version | |
| if [ -n "${{ github.event.inputs.version }}" ]; then | |
| VERSION="${{ github.event.inputs.version }}" | |
| TRIGGER="Manual (workflow_dispatch)" | |
| else | |
| VERSION="${{ github.ref_name }}" | |
| TRIGGER="Tag push" | |
| fi | |
| TAGS="${{ env.REGISTRY }}/$IMAGE_NAME:$VERSION,${{ env.REGISTRY }}/$IMAGE_NAME:latest" | |
| echo "🚀 Docker Build Starting" | |
| echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" | |
| echo "📦 Repository: ${{ github.repository }}" | |
| echo "🏷️ Version: $VERSION" | |
| echo "🎯 Trigger: $TRIGGER" | |
| echo "🐳 Registry: ${{ env.REGISTRY }}" | |
| echo "" | |
| echo "📋 Will be tagged as:" | |
| echo " • ${{ env.REGISTRY }}/$IMAGE_NAME:$VERSION" | |
| echo " • ${{ env.REGISTRY }}/$IMAGE_NAME:latest" | |
| echo "" | |
| echo "🏗️ Platforms: linux/amd64, linux/arm64" | |
| echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" | |
| echo "image_name=$IMAGE_NAME" >> $GITHUB_OUTPUT | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "tags=$TAGS" >> $GITHUB_OUTPUT | |
| build-amd64: | |
| needs: info | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 20 | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push AMD64 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/amd64 | |
| push: true | |
| tags: ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-amd64 | |
| cache-from: type=gha,scope=amd64 | |
| cache-to: type=gha,mode=max,scope=amd64 | |
| labels: | | |
| org.opencontainers.image.source=https://github.com/${{ github.repository }} | |
| org.opencontainers.image.version=${{ needs.info.outputs.version }} | |
| build-arm64: | |
| needs: info | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 20 | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push ARM64 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/arm64 | |
| push: true | |
| tags: ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-arm64 | |
| cache-from: type=gha,scope=arm64 | |
| cache-to: type=gha,mode=max,scope=arm64 | |
| labels: | | |
| org.opencontainers.image.source=https://github.com/${{ github.repository }} | |
| org.opencontainers.image.version=${{ needs.info.outputs.version }} | |
| create-manifest: | |
| needs: [info, build-amd64, build-arm64] | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 10 | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create and push multi-arch manifest | |
| run: | | |
| # Create manifest for version tag | |
| docker buildx imagetools create \ | |
| -t ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }} \ | |
| ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-amd64 \ | |
| ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-arm64 | |
| # Create manifest for latest tag | |
| docker buildx imagetools create \ | |
| -t ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:latest \ | |
| ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-amd64 \ | |
| ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}-arm64 | |
| - name: Clean up temporary tags | |
| run: | | |
| # Note: GitHub doesn't provide a direct way to delete tags via docker CLI | |
| # The temporary architecture-specific tags will remain but won't interfere | |
| echo "✅ Multi-arch manifests created successfully" | |
| echo "⚠️ Architecture-specific tags remain as build artifacts" | |
| - name: Summary | |
| run: | | |
| echo "## 🚀 Docker Image Built Successfully!" >> $GITHUB_STEP_SUMMARY | |
| echo "**Image:** \`${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}\`" >> $GITHUB_STEP_SUMMARY | |
| echo "**Version:** \`${{ needs.info.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY | |
| echo "**Tags:** \`${{ needs.info.outputs.version }}\`, \`latest\`" >> $GITHUB_STEP_SUMMARY | |
| echo "**Platforms:** linux/amd64, linux/arm64" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**Pull:**" >> $GITHUB_STEP_SUMMARY | |
| echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY | |
| echo "docker pull ${{ env.REGISTRY }}/${{ needs.info.outputs.image_name }}:${{ needs.info.outputs.version }}" >> $GITHUB_STEP_SUMMARY | |
| echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |