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

jobs:
  build:

    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest"]
        docker: [
          alpine,
          amazon-2-amd64,
          amazon-2023-amd64,
          arch,
          centos-stream-9-amd64,
          centos-stream-10-amd64,
          debian-12-bookworm-x86,
          debian-12-bookworm-amd64,
          fedora-40-amd64,
          fedora-41-amd64,
          gentoo,
          ubuntu-22.04-jammy-amd64,
          ubuntu-24.04-noble-amd64,
        ]
        dockerTag: [main]
        include:
          - docker: "ubuntu-24.04-noble-ppc64le"
            os: "ubuntu-22.04"
            qemu-arch: "ppc64le"
            dockerTag: main
          - docker: "ubuntu-24.04-noble-s390x"
            os: "ubuntu-22.04"
            qemu-arch: "s390x"
            dockerTag: main
          - docker: "ubuntu-24.04-noble-arm64v8"
            os: "ubuntu-24.04-arm"
            dockerTag: main

    name: ${{ matrix.docker }}

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

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

    - name: Set up QEMU
      if: "matrix.qemu-arch"
      run: |
        docker run --rm --privileged aptman/qus -s -- -p ${{ matrix.qemu-arch }}

    - name: Docker pull
      run: |
        docker pull 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
