Skip to content

💚 补充 Docker push 时缺少的 tag #6

💚 补充 Docker push 时缺少的 tag

💚 补充 Docker push 时缺少的 tag #6

Workflow file for this run

name: Docker
on:
push:
branches:
- master
env:
LATEST_PYTHON_VERSION: 3.11
jobs:
check-version:
name: Check Version
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
VERSION: ${{ steps.version.outputs.VERSION }}
TAG_VERSION: ${{ steps.version.outputs.TAG_VERSION }}
TAG_NAME: ${{ steps.version.outputs.TAG_NAME }}
CHECK_PASS: ${{ steps.check.outputs.CHECK_PASS }}
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v4
- name: 🚀 Setup Python environment
uses: ./.github/actions/setup-python
- name: 🔖 Get version
id: version
run: |
echo "VERSION=$(poetry version -s)" >> $GITHUB_OUTPUT
echo "TAG_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: ✅ Check version
id: check
run: |
if [[ "${{ steps.version.outputs.VERSION }}" != "${{ steps.version.outputs.TAG_VERSION }}" ]]; then
echo "CHECK_PASS=false" >> $GITHUB_OUTPUT
else
echo "CHECK_PASS=true" >> $GITHUB_OUTPUT
fi
build:
name: Build Docker image
runs-on: ubuntu-latest
needs: check-version
permissions:
contents: read
packages: write
strategy:
fail-fast: true
matrix:
python_version:
- "3.9"
- "3.10"
- "3.11"
python_variant:
- ""
- "-slim"
platform:
- linux/amd64
- linux/arm64
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v4
- name: 🚀 Setup QEMU
uses: docker/setup-qemu-action@v3
- name: 🚀 Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: 🔐 Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: ✨ Generate Image Name and Scope
id: image
run: |
echo "IMAGE=${{ github.repository }}" >> $GITHUB_OUTPUT
echo "SCOPE=${{ hashFiles('**/poetry.lock') }}-${{ matrix.python_version }}${{ matrix.python_variant }}-${{ matrix.platform }}" >> $GITHUB_OUTPUT
- name: ✨ Generate Labels
uses: docker/metadata-action@v5
id: metadata
with:
images: ${{ steps.image.outputs.IMAGE }}
- name: 🔍 Get Latest Commit and Generate Build Arg
id: commit
run: |
if [[ "${{ needs.check-version.outputs.CHECK_PASS }}" == "true" ]]; then
echo "COMMIT=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
else
echo "COMMIT=${GITHUB_SHA::7}-dev" >> $GITHUB_OUTPUT
fi
- name: 🚧 Build and Publish
uses: docker/build-push-action@v5
id: build
with:
context: .
platforms: ${{ matrix.platform }}
build-args: |
SOURCE_COMMIT=${{ steps.commit.outputs.COMMIT }}
PYTHON_IMAGE=${{ matrix.python_version }}
VARIANT=${{ matrix.python_variant }}
labels: ${{ steps.metadata.outputs.labels }}
cache-from: type=gha,scope=${{ steps.image.outputs.SCOPE }}
cache-to: type=gha,scope=${{ steps.image.outputs.SCOPE }},mode=max
outputs: type=image,name=${{ steps.image.outputs.IMAGE }},push-by-digest=true,name-canonical=true,push=true
- name: 📤 Export digest
run: |
mkdir -p /tmp/digests/${{ matrix.python_version }}${{ matrix.python_variant }}
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${{ matrix.python_version }}${{ matrix.python_variant }}/${digest#sha256:}"
- name: 📦️ Upload digest
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
push:
name: Push Docker image
runs-on: ubuntu-latest
needs:
- check-version
- build
strategy:
matrix:
python_version:
- "3.9"
- "3.10"
- "3.11"
python_variant:
- ""
- "-slim"
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v3
- name: 📥 Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests
- name: 🚀 Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: 🔐 Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: ✨ Generate Image Name
id: image
run: |
echo "IMAGE=${{ github.repository }}" >> $GITHUB_OUTPUT
- name: ✨ Generate Tags
uses: docker/metadata-action@v5
id: metadata
with:
images: ${{ steps.image.outputs.IMAGE }}
flavor: |
suffix=-py${{ matrix.python_version }}${{ matrix.python_variant }},onlatest=${{ needs.check-version.outputs.CHECK_PASS }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: 📦️ Create manifest list and push
working-directory: /tmp/digests/${{ matrix.python_version }}${{ matrix.python_variant }}
run: |
docker buildx imagetools create --dry-run $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ steps.image.outputs.IMAGE }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ steps.image.outputs.IMAGE }}@sha256:%s ' *)
- name: ✨ Generate Tags
uses: docker/metadata-action@v5
id: metadata-latest
if: matrix.python_version == env.LATEST_PYTHON_VERSION
with:
images: |
${{ github.repository }}
flavor: |
suffix=${{ matrix.python_variant }},onlatest=${{ needs.check-version.outputs.CHECK_PASS }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: 📦️ Create manifest list and push for latest python version
if: matrix.python_version == env.LATEST_PYTHON_VERSION
working-directory: /tmp/digests/${{ matrix.python_version }}${{ matrix.python_variant }}
run: |
docker buildx imagetools create --dry-run $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ steps.image.outputs.IMAGE }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ steps.image.outputs.IMAGE }}@sha256:%s ' *)
- name: 📝 Docker Hub Description
uses: peter-evans/dockerhub-description@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
short-description: ${{ github.event.repository.description }}