name: Test Docker

on:
  push:
    branches:
      - "**"
    paths-ignore:
      - ".github/workflows/docs.yml"
      - ".github/workflows/wheels*"
      - ".gitmodules"
      - "docs/**"
      - "wheels/**"
  pull_request:
    paths-ignore:
      - ".github/workflows/docs.yml"
      - ".github/workflows/wheels*"
      - ".gitmodules"
      - "docs/**"
      - "wheels/**"
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  FORCE_COLOR: 1

jobs:
  build:

    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest"]
        docker: [
          # Run slower jobs first to give them a headstart and reduce waiting time
          ubuntu-24.04-noble-ppc64le,
          ubuntu-24.04-noble-s390x,
          # Then run the remainder
          alpine,
          amazon-2-amd64,
          amazon-2023-amd64,
          arch,
          centos-stream-9-amd64,
          centos-stream-10-amd64,
          debian-12-bookworm-x86,
          debian-12-bookworm-amd64,
          debian-13-trixie-x86,
          debian-13-trixie-amd64,
          fedora-42-amd64,
          fedora-43-amd64,
          gentoo,
          ubuntu-22.04-jammy-amd64,
          ubuntu-24.04-noble-amd64,
        ]
        dockerTag: [main]
        include:
          - docker: "ubuntu-24.04-noble-ppc64le"
            qemu-arch: "ppc64le"
          - docker: "ubuntu-24.04-noble-s390x"
            qemu-arch: "s390x"
          - docker: "ubuntu-24.04-noble-arm64v8"
            os: "ubuntu-24.04-arm"
            dockerTag: main

    name: ${{ matrix.docker }}

    steps:
    - uses: actions/checkout@v6
      with:
        persist-credentials: false

    - name: Build system information
      run: python3 .github/workflows/system-info.py

    - name: Set up QEMU
      if: "matrix.qemu-arch"
      uses: docker/setup-qemu-action@v3
      with:
        platforms: ${{ matrix.qemu-arch }}

    - name: Docker pull
      run: |
        docker pull ${{ matrix.qemu-arch && format('--platform=linux/{0}', matrix.qemu-arch)}} pythonpillow/${{ matrix.docker }}:${{ matrix.dockerTag }}

    - name: Docker build
      run: |
        # The Pillow user in the docker container is UID 1001
        sudo chown -R 1001 $GITHUB_WORKSPACE
        docker run --name pillow_container  -v $GITHUB_WORKSPACE:/Pillow pythonpillow/${{ matrix.docker }}:${{ matrix.dockerTag }}
        sudo chown -R runner $GITHUB_WORKSPACE

    - name: After success
      run: |
        docker start pillow_container
        sudo docker cp pillow_container:/Pillow /Pillow
        sudo chown -R runner /Pillow
        pil_path=`docker exec pillow_container /vpy3/bin/python -c 'import os, PIL;print(os.path.realpath(os.path.dirname(PIL.__file__)))'`
        docker stop pillow_container
        sudo mkdir -p $pil_path
        sudo cp src/PIL/*.py $pil_path
        cd /Pillow
        .ci/after_success.sh

    - name: Upload coverage
      uses: codecov/codecov-action@v5
      with:
        flags: GHA_Docker
        name: ${{ matrix.docker }}
        token: ${{ secrets.CODECOV_ORG_TOKEN }}

  success:
    permissions:
      contents: none
    needs: build
    runs-on: ubuntu-latest
    name: Docker Test Successful
    steps:
      - name: Success
        run: echo Docker Test Successful
