|
| 1 | +name: Build Python Base Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + python_version: |
| 7 | + description: "Python version to build" |
| 8 | + required: true |
| 9 | + default: "3.11.2" |
| 10 | + push: |
| 11 | + paths: |
| 12 | + - "Dockerfile.python-base" |
| 13 | + branches: |
| 14 | + - main |
| 15 | + - development |
| 16 | + |
| 17 | +env: |
| 18 | + REGISTRY: ghcr.io |
| 19 | + IMAGE_NAME: ${{ github.repository }}/python-base |
| 20 | + |
| 21 | +jobs: |
| 22 | + build: |
| 23 | + strategy: |
| 24 | + matrix: |
| 25 | + platform: ["linux/amd64", "linux/arm64"] |
| 26 | + runs-on: ${{ matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + packages: write |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v6 |
| 33 | + |
| 34 | + - name: Set up Docker Buildx |
| 35 | + uses: docker/setup-buildx-action@v3 |
| 36 | + |
| 37 | + - name: Log into registry ${{ env.REGISTRY }} |
| 38 | + uses: docker/login-action@v3 |
| 39 | + with: |
| 40 | + registry: ${{ env.REGISTRY }} |
| 41 | + username: ${{ github.actor }} |
| 42 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + |
| 44 | + - name: Set Python version |
| 45 | + id: python |
| 46 | + run: | |
| 47 | + # Use input if provided, otherwise extract from Dockerfile.python-base |
| 48 | + if [ -n "${{ inputs.python_version }}" ]; then |
| 49 | + echo "version=${{ inputs.python_version }}" >> $GITHUB_OUTPUT |
| 50 | + else |
| 51 | + VERSION=$(grep -oP 'ARG PYTHON_VERSION=\K[0-9.]+' Dockerfile.python-base) |
| 52 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 53 | + fi |
| 54 | +
|
| 55 | + - name: Extract Docker metadata |
| 56 | + id: meta |
| 57 | + uses: docker/metadata-action@v5 |
| 58 | + with: |
| 59 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 60 | + tags: | |
| 61 | + type=raw,value=${{ steps.python.outputs.version }}-trixie-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }} |
| 62 | +
|
| 63 | + - name: Build and push Python base image for ${{ matrix.platform }} |
| 64 | + uses: docker/build-push-action@v6 |
| 65 | + with: |
| 66 | + context: . |
| 67 | + file: Dockerfile.python-base |
| 68 | + push: true |
| 69 | + provenance: false |
| 70 | + platforms: ${{ matrix.platform }} |
| 71 | + tags: ${{ steps.meta.outputs.tags }} |
| 72 | + labels: ${{ steps.meta.outputs.labels }} |
| 73 | + build-args: | |
| 74 | + PYTHON_VERSION=${{ steps.python.outputs.version }} |
| 75 | +
|
| 76 | + manifest: |
| 77 | + needs: build |
| 78 | + runs-on: ubuntu-latest |
| 79 | + permissions: |
| 80 | + contents: read |
| 81 | + packages: write |
| 82 | + steps: |
| 83 | + - name: Checkout |
| 84 | + uses: actions/checkout@v6 |
| 85 | + |
| 86 | + - name: Log into registry ${{ env.REGISTRY }} |
| 87 | + uses: docker/login-action@v3 |
| 88 | + with: |
| 89 | + registry: ${{ env.REGISTRY }} |
| 90 | + username: ${{ github.actor }} |
| 91 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + |
| 93 | + - name: Set Python version |
| 94 | + id: python |
| 95 | + run: | |
| 96 | + if [ -n "${{ inputs.python_version }}" ]; then |
| 97 | + echo "version=${{ inputs.python_version }}" >> $GITHUB_OUTPUT |
| 98 | + else |
| 99 | + VERSION=$(grep -oP 'ARG PYTHON_VERSION=\K[0-9.]+' Dockerfile.python-base) |
| 100 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 101 | + fi |
| 102 | +
|
| 103 | + - name: Create multi-arch manifest |
| 104 | + run: | |
| 105 | + LOWER_IMAGE_NAME=$(echo "${IMAGE_NAME}" | tr '[:upper:]' '[:lower:]') |
| 106 | + PYTHON_VERSION="${{ steps.python.outputs.version }}" |
| 107 | + |
| 108 | + docker manifest create $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie \ |
| 109 | + $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie-amd64 \ |
| 110 | + $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie-arm64 |
| 111 | + docker manifest push $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie |
| 112 | + |
| 113 | + # Also tag as latest |
| 114 | + docker manifest create $REGISTRY/${LOWER_IMAGE_NAME}:latest \ |
| 115 | + $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie-amd64 \ |
| 116 | + $REGISTRY/${LOWER_IMAGE_NAME}:${PYTHON_VERSION}-trixie-arm64 |
| 117 | + docker manifest push $REGISTRY/${LOWER_IMAGE_NAME}:latest |
0 commit comments