name: Test Docker

on:
  push:
    branches:
      - "**"
    paths-ignore: &paths-ignore
      - ".github/workflows/docs.yml"
      - ".github/workflows/wheels*"
      - ".gitmodules"
      - "docs/**"
      - "wheels/**"
  pull_request:
    paths-ignore: *paths-ignore
  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-26.04-resolute-ppc64le,
          ubuntu-26.04-resolute-s390x,
          # Then run the remainder
          alpine,
          amazon-2023-amd64,
          arch,
          centos-stream-9-amd64,
          centos-stream-10-amd64,
          debian-13-trixie-x86,
          debian-13-trixie-amd64,
          fedora-43-amd64,
          fedora-44-amd64,
          gentoo,
          ubuntu-24.04-noble-amd64,
          ubuntu-26.04-resolute-amd64,
        ]
        dockerTag: [main]
        include:
          - docker: "ubuntu-26.04-resolute-ppc64le"
            qemu-arch: "ppc64le"
          - docker: "ubuntu-26.04-resolute-s390x"
            qemu-arch: "s390x"
          - docker: "ubuntu-26.04-resolute-arm64v8"
            os: "ubuntu-24.04-arm"
            dockerTag: main

    name: ${{ matrix.docker }}

    steps:
    - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      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@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0
      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@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
      with:
        flags: GHA_Docker
        name: ${{ matrix.docker }}

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