MAINT: Upgrade the remaining GitHub Actions. <!-- Thank you for contributing to Pyodide! All improvements are welcome,
     so don't be afraid to make a PR. -->

### Description

<!-- Please explain what your PR is about:
     - reasoning for the change
     - some details of updated code
     - any noteworthy choices to be aware of
	Please refer to any related issues by #<issue_id> -->

- https://github.com/actions/checkout/releases
- https://github.com/docker/build-push-action/releases
- https://github.com/docker/login-action/releases
- https://github.com/docker/metadata-action/releases
- https://github.com/pypa/gh-action-pypi-publish/releases
- https://github.com/test-summary/action/releases

Also, add a final check to the CPython upgrade instructions.

### Checklists

<!-- Note:
     If you think some of these steps are not necessary for your PR,
     remove those checkboxes, or mark them as checked. If you keep unchecked checkboxes,
     we will assume that your PR is not ready to be merged  -->

- [ ] Add a [CHANGELOG](https://github.com/pyodide/pyodide/blob/main/docs/project/changelog.md) entry
- [x] Add / update tests
- [ ] Add new / update outdated documentation

name: main

on:
  push:
    branches: [main]
  pull_request:

concurrency:
  group: main-${{ github.head_ref }}
  cancel-in-progress: true

jobs:
  get_python_version:
    runs-on: ubuntu-latest
    outputs:
      PYVERSION: ${{ steps.get_python_version.outputs.PYVERSION }}
    steps:
      - uses: actions/checkout@v3
      - name: Read the Python version from Makefile.envs
        id: get_python_version
        run: |
          echo "PYVERSION=$(git grep 'export PYVERSION ?=' Makefile.envs | cut -d' ' -f4)"  >> "$GITHUB_OUTPUT"
  lint:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - uses: pre-commit/action@v2.0.3

  test-docs:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - name: Install requirements
        shell: bash -l {0}
        run: |
          pip install -r requirements.txt -r docs/requirements-doc.txt
      - name: Run tests
        shell: bash -l {0}
        run: |
          mkdir -p test-results
          pytest docs/sphinx_pyodide/tests --junitxml=test-results/junit.xml
      - name: Test Summary
        uses: test-summary/action@v1
        with:
          paths: "test-results/*.xml"
        if: always()

  test-python:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - name: Install requirements
        shell: bash -l {0}
        run: |
          mkdir test-results
          cd pyodide-build && python3 -m pip install -e ".[test]" && cd ..
          python3 -m pip install pytest-cov hypothesis pytz
      - name: Run tests
        shell: bash -l {0}
        run: |
          PYODIDE_ROOT=. pytest \
              --junitxml=test-results/junit.xml \
              --verbose \
              --runtime=host \
              --cov=pyodide_build --cov=pyodide \
              src pyodide-build packages/micropip/ packages/_tests
      - uses: codecov/codecov-action@v3
        with:
          fail_ci_if_error: false

  build-core:
    runs-on: ubuntu-latest
    env:
      EMSDK_NUM_CORES: 3
      EMCC_CORES: 3
      PYODIDE_JOBS: 3
      CCACHE_DIR: /tmp/ccache

    steps:
      - uses: actions/checkout@v3

      - name: Cache ccache output
        uses: actions/cache@v3
        with:
          path: |
            /tmp/ccache
          key: ${{ hashFiles('Makefile.envs') }}-v20211025-

      - uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: pyodide-env
          python-version: "3.11"
          channels: conda-forge

      - name: Check Python versions
        shell: bash -l {0}
        run: |
          python --version
          which python

      - name: Install dependencies
        shell: bash -l {0}
        run: |
          sudo apt install -y build-essential git xxd
          conda install -y nodejs ccache f2c pkg-config swig make patch pkg-config texinfo autoconf automake libtool
          pip install -r requirements.txt

      - name: Build emsdk
        shell: bash -l {0}
        run: |
          which ccache

          ccache -z
          make -C emsdk
          ccache -s

      - name: Build Cpython
        shell: bash -l {0}
        run: |
          # This is necessary to use the ccache from emsdk
          source pyodide_env.sh

          which ccache

          ccache -z
          make -C cpython
          ccache -s

      - name: build Pyodide core + numpy
        shell: bash -l {0}
        run: |
          # This is necessary to use the ccache from emsdk
          source pyodide_env.sh
          ccache -z
          PYODIDE_PACKAGES="tag:core,numpy" make
          ccache -s

      - name: check-size
        run: |
          ls -lh dist/

          pip install brotli
          ./tools/check_compressed_size.py dist/pyodide.asm.* dist/python_stdlib*

      - name: Store artifacts build
        uses: actions/upload-artifact@v3
        with:
          name: core-build
          path: ./dist/
          retention-days: 60

  test-core:
    runs-on: ubuntu-latest
    env:
      DISPLAY: :99

    needs: [build-core]
    strategy:
      fail-fast: false
      matrix:
        runner: [playwright]
        browser: [chrome]

    steps:
      - uses: actions/checkout@v3

      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: core-build
          path: ./dist/

      - uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: pyodide-env
          python-version: "3.11"
          channels: conda-forge

      - name: install test requirements
        shell: bash -l {0}
        run: |
          pip install -r requirements.txt
          pip install -e ./pyodide-build
          python -m playwright install

      - name: run core tests
        env:
          BROWSER: ${{ matrix.browser }}
          RUNNER: ${{ matrix.runner }}
        shell: bash -l {0}
        run: |
          ls -lh
          ls -lh dist/
          tools/pytest_wrapper.py src packages/micropip/ \
            -v \
            --runtime="${BROWSER}-no-host" \
            --runner "${RUNNER}" \
            --durations 50 \
            --junitxml=test-results/core_test.xml

      - name: run package tests
        env:
          BROWSER: ${{ matrix.browser }}
          RUNNER: ${{ matrix.runner }}
        shell: bash -l {0}
        run: |
          ls -lh
          ls -lh dist/
          tools/pytest_wrapper.py packages/*/test* \
            -v \
            -k "numpy and not joblib" \
            --runtime="${BROWSER}-no-host" \
            --runner "${RUNNER}" \
            --durations 50 \
            --junitxml=test-results/packages_test.xml

      - name: Test Summary
        uses: test-summary/action@v1
        with:
          paths: "test-results/*.xml"
        if: always()

name: main

on:
  push:
    branches: [main]
  pull_request:

concurrency:
  group: main-${{ github.head_ref }}
  cancel-in-progress: true

jobs:
  get_python_version:
    runs-on: ubuntu-latest
    outputs:
      PYVERSION: ${{ steps.get_python_version.outputs.PYVERSION }}
    steps:
      - uses: actions/checkout@v3
      - name: Read the Python version from Makefile.envs
        id: get_python_version
        run: |
          echo "PYVERSION=$(git grep 'export PYVERSION ?=' Makefile.envs | cut -d' ' -f4)"  >> "$GITHUB_OUTPUT"
  lint:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - uses: pre-commit/action@v2.0.3

  test-docs:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - name: Install requirements
        shell: bash -l {0}
        run: |
          pip install -r requirements.txt -r docs/requirements-doc.txt
      - name: Run tests
        shell: bash -l {0}
        run: |
          mkdir -p test-results
          pytest docs/sphinx_pyodide/tests --junitxml=test-results/junit.xml
      - name: Test Summary
        uses: test-summary/action@v2
        with:
          paths: "test-results/*.xml"
        if: always()

  test-python:
    runs-on: ubuntu-latest
    needs: get_python_version
    steps:
      - uses: actions/checkout@v3
      - name: Setup Python ${{ needs.get_python_version.outputs.PYVERSION }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ needs.get_python_version.outputs.PYVERSION }}
      - name: Install requirements
        shell: bash -l {0}
        run: |
          mkdir test-results
          cd pyodide-build && python3 -m pip install -e ".[test]" && cd ..
          python3 -m pip install pytest-cov hypothesis pytz
      - name: Run tests
        shell: bash -l {0}
        run: |
          PYODIDE_ROOT=. pytest \
              --junitxml=test-results/junit.xml \
              --verbose \
              --runtime=host \
              --cov=pyodide_build --cov=pyodide \
              src pyodide-build packages/micropip/ packages/_tests
      - uses: codecov/codecov-action@v3
        with:
          fail_ci_if_error: false

  build-core:
    runs-on: ubuntu-latest
    env:
      EMSDK_NUM_CORES: 3
      EMCC_CORES: 3
      PYODIDE_JOBS: 3
      CCACHE_DIR: /tmp/ccache

    steps:
      - uses: actions/checkout@v3

      - name: Cache ccache output
        uses: actions/cache@v3
        with:
          path: |
            /tmp/ccache
          key: ${{ hashFiles('Makefile.envs') }}-v20211025-

      - uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: pyodide-env
          python-version: "3.11"
          channels: conda-forge

      - name: Check Python versions
        shell: bash -l {0}
        run: |
          python --version
          which python

      - name: Install dependencies
        shell: bash -l {0}
        run: |
          sudo apt install -y build-essential git xxd
          conda install -y nodejs ccache f2c pkg-config swig make patch pkg-config texinfo autoconf automake libtool
          pip install -r requirements.txt

      - name: Build emsdk
        shell: bash -l {0}
        run: |
          which ccache

          ccache -z
          make -C emsdk
          ccache -s

      - name: Build Cpython
        shell: bash -l {0}
        run: |
          # This is necessary to use the ccache from emsdk
          source pyodide_env.sh

          which ccache

          ccache -z
          make -C cpython
          ccache -s

      - name: build Pyodide core + numpy
        shell: bash -l {0}
        run: |
          # This is necessary to use the ccache from emsdk
          source pyodide_env.sh
          ccache -z
          PYODIDE_PACKAGES="tag:core,numpy" make
          ccache -s

      - name: check-size
        run: |
          ls -lh dist/

          pip install brotli
          ./tools/check_compressed_size.py dist/pyodide.asm.* dist/python_stdlib*

      - name: Store artifacts build
        uses: actions/upload-artifact@v3
        with:
          name: core-build
          path: ./dist/
          retention-days: 60

  test-core:
    runs-on: ubuntu-latest
    env:
      DISPLAY: :99

    needs: [build-core]
    strategy:
      fail-fast: false
      matrix:
        runner: [playwright]
        browser: [chrome]

    steps:
      - uses: actions/checkout@v3

      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: core-build
          path: ./dist/

      - uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: pyodide-env
          python-version: "3.11"
          channels: conda-forge

      - name: install test requirements
        shell: bash -l {0}
        run: |
          pip install -r requirements.txt
          pip install -e ./pyodide-build
          python -m playwright install

      - name: run core tests
        env:
          BROWSER: ${{ matrix.browser }}
          RUNNER: ${{ matrix.runner }}
        shell: bash -l {0}
        run: |
          ls -lh
          ls -lh dist/
          tools/pytest_wrapper.py src packages/micropip/ \
            -v \
            --runtime="${BROWSER}-no-host" \
            --runner "${RUNNER}" \
            --durations 50 \
            --junitxml=test-results/core_test.xml

      - name: run package tests
        env:
          BROWSER: ${{ matrix.browser }}
          RUNNER: ${{ matrix.runner }}
        shell: bash -l {0}
        run: |
          ls -lh
          ls -lh dist/
          tools/pytest_wrapper.py packages/*/test* \
            -v \
            -k "numpy and not joblib" \
            --runtime="${BROWSER}-no-host" \
            --runner "${RUNNER}" \
            --durations 50 \
            --junitxml=test-results/packages_test.xml

      - name: Test Summary
        uses: test-summary/action@v2
        with:
          paths: "test-results/*.xml"
        if: always()
