name: Docker Publish on: push: tags: - 'v*' workflow_dispatch: # 允许手动触发测试 env: REGISTRY: ghcr.io jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 # 拉完整历史,git describe 能取到 tag - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to GHCR uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract version from git id: version run: | echo "version=$(git describe --tags --always --dirty)" >> "$GITHUB_OUTPUT" echo "commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" - name: Extract Docker metadata id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ github.repository }} tags: | type=semver,pattern={{version}} type=sha,format=short labels: | org.opencontainers.image.version=${{ steps.version.outputs.version }} org.opencontainers.image.revision=${{ steps.version.outputs.commit }} - name: Build and push 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 }} build-args: | VERSION=${{ steps.version.outputs.version }} COMMIT=${{ steps.version.outputs.commit }}