This repository was archived by the owner on Nov 3, 2025. It is now read-only.
构建和推送 Docker 镜像 #6
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: 构建和推送 Docker 镜像 | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| tags: [ 'v*' ] | |
| pull_request: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: '自定义标签 (可选)' | |
| required: false | |
| default: 'manual' | |
| push_to_registry: | |
| description: '推送到注册表' | |
| required: true | |
| default: true | |
| type: boolean | |
| build_arm64: | |
| description: '构建 ARM64 架构 (较慢)' | |
| required: true | |
| default: false | |
| type: boolean | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver-opts: | | |
| image=moby/buildkit:buildx-stable-1 | |
| network=host | |
| - name: 登录到 GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: 提取元数据 | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=semver,pattern={{major}} | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| type=raw,value=${{ github.event.inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }} | |
| - name: 构建并推送多架构 Docker 镜像 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: Dockerfile | |
| platforms: linux/amd64${{ (github.event_name != 'workflow_dispatch' || github.event.inputs.build_arm64 == 'true') && ',linux/arm64' || '' }} | |
| push: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.push_to_registry == 'true' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha,scope=multi-arch | |
| cache-to: type=gha,mode=max,scope=multi-arch | |
| build-args: | | |
| BUILDPLATFORM=linux/amd64 | |
| TARGETPLATFORM=linux/amd64 | |
| outputs: type=registry | |
| provenance: false | |
| sbom: false | |
| - name: 生成摘要 | |
| if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | |
| run: | | |
| echo "## 🐳 Docker 镜像构建完成" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**触发方式:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**构建架构:** multi-arch (amd64${{ (github.event_name != 'workflow_dispatch' || github.event.inputs.build_arm64 == 'true') && ' + arm64' || '' }})" >> $GITHUB_STEP_SUMMARY | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| echo "**自定义标签:** ${{ github.event.inputs.tag }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**推送到注册表:** ${{ github.event.inputs.push_to_registry }}" >> $GITHUB_STEP_SUMMARY | |
| echo "**构建 ARM64:** ${{ github.event.inputs.build_arm64 }}" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**镜像标签:**" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**拉取命令:**" >> $GITHUB_STEP_SUMMARY | |
| echo '```bash' >> $GITHUB_STEP_SUMMARY | |
| echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ github.event.inputs.tag }}" != "" ]; then | |
| echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.tag }}" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| echo '```' >> $GITHUB_STEP_SUMMARY |