Docker Build and Push #71
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 Build and Push | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version_tag: | |
| description: '版本标签 (例如: v1.0.0)' | |
| required: false | |
| type: string | |
| default: '' | |
| include_latest: | |
| description: 'latest标签' | |
| required: false | |
| type: boolean | |
| default: true | |
| 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 | |
| - name: 登录到 GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: 提取 Docker 元数据 | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: | | |
| ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| ${{ inputs.include_latest == true && 'type=raw,value=latest' || '' }} | |
| ${{ inputs.version_tag != '' && format('type=raw,value={0}', inputs.version_tag) || '' }} | |
| flavor: | | |
| latest=false | |
| - name: 构建并推送 Docker 镜像 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: 输出镜像信息 | |
| run: | | |
| echo "### 🐳 Docker镜像构建成功!" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**镜像标签:**" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| if [ -n "${{ inputs.version_tag }}" ]; then | |
| echo "**版本标签:** ${{ inputs.version_tag }}" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| if [ "${{ inputs.include_latest }}" == "true" ]; then | |
| echo "**包含latest标签:** 是" >> $GITHUB_STEP_SUMMARY | |
| else | |
| echo "**包含latest标签:** 否" >> $GITHUB_STEP_SUMMARY | |
| fi | |