Build Docker Image #23
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 Docker Image | |
| on: | |
| push: | |
| tags: | |
| - 'v*' # 当推送 v1.0.0 这样的 tag 时触发 | |
| branches: | |
| - main # 也可以在主分支推送时触发 | |
| workflow_dispatch: # 支持手动触发 | |
| inputs: | |
| push_to_registry: | |
| description: 'Push to Docker Hub/GHCR' | |
| required: false | |
| default: 'true' | |
| type: boolean | |
| push_to_dockerhub: | |
| description: 'Also push to Docker Hub (requires DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets)' | |
| required: false | |
| default: 'false' | |
| type: boolean | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| jobs: | |
| build: | |
| name: Build Docker Image | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: 登录到 GitHub Container Registry | |
| if: github.event_name != 'pull_request' && (github.event.inputs.push_to_registry == 'true' || github.event.inputs.push_to_registry == null) | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: 登录到 Docker Hub | |
| if: github.event_name != 'pull_request' && (github.event.inputs.push_to_dockerhub == 'true' || (github.event.inputs.push_to_dockerhub == null && env.DOCKERHUB_USERNAME != '')) | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_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=sha,prefix={{branch}}- | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: 构建并推送 Docker 镜像(多架构) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: ${{ github.event_name != 'pull_request' && (github.event.inputs.push_to_registry == 'true' || github.event.inputs.push_to_registry == null) }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: 显示镜像信息 | |
| if: github.event_name != 'pull_request' | |
| run: | | |
| echo "✅ Docker镜像构建完成!" | |
| echo "📦 镜像标签:" | |
| echo "${{ steps.meta.outputs.tags }}" | |
| echo "" | |
| echo "🔗 使用镜像:" | |
| echo " docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" | |
| echo "" | |
| echo "🚀 运行容器:" | |
| echo " docker run -it --rm \\" | |
| echo " -v \$(pwd)/data:/app/data \\" | |
| echo " -v \$(pwd)/config:/app/config \\" | |
| echo " ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" | |