name: Full EWAH Release - GitHub tag, PyPi package, and Docker image

on:
  push:  # Only run on a merged PR to main
    branches:
      - master
    paths:  # Only run if VERSION is bumped by the push
      - VERSION

jobs:
  release:
    name: Release to PyPi
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Create GitHub Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # To authenticate POST
        run: |
          VERSION=$(cat VERSION)
          echo Creating Release v$VERSION
          curl -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            -H 'Authorization: token '"${GITHUB_TOKEN}" \
            'https://api.github.com/repos/'"${GITHUB_REPOSITORY}"'/releases' \
            -d '{"tag_name":"v'"${VERSION}"'", "name":"v'"${VERSION}"'", "body":"This Release was automatically created by a GitHub Action after the merge of a PR. Please check the appropriate PR for the changelog."}'
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.8'
      - name: Install dependencies for PyPi publication
        run: |
          python -m pip install --upgrade pip
          pip install setuptools wheel twine
      - name: Build and publish Python package to PyPi
        env:
          TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
          TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
        run: |
          python setup.py sdist bdist_wheel
          twine upload --skip-existing dist/*
  build:
    name: Build Docker image (${{ matrix.platform }})
    needs: release
    # Matrix strategy runs this job multiple times in parallel, once per configuration.
    # Each configuration specifies the Docker platform and corresponding GitHub runner.
    # Docs: https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
    strategy:
      matrix:
        include:
          - platform: linux/amd64
            arch: amd64
            runner: ubuntu-24.04
          - platform: linux/arm64
            arch: arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set executable permissions
        run: |
          # Need to give executable rights locally, otherwise image will not
          # be able to run properly - else will get a permission denied error on it
          chmod +x docker/scripts/entrypoint_prod.sh
      - name: Get version
        id: version
        run: echo "VERSION=$(cat VERSION)" >> $GITHUB_OUTPUT
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Build and push by digest
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          platforms: ${{ matrix.platform }}
          build-args: |
            package_version=${{ steps.version.outputs.VERSION }}
          outputs: type=image,name=gemmaanalytics/ewah,push-by-digest=true,name-canonical=true,push=true
      - name: Export digest
        run: |
          mkdir -p /tmp/digests
          digest="${{ steps.build.outputs.digest }}"
          touch "/tmp/digests/${digest#sha256:}"
      - name: Upload digest
        uses: actions/upload-artifact@v4
        with:
          name: digests-${{ matrix.arch }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  merge:
    name: Create multi-arch manifest
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Get version
        id: version
        run: echo "VERSION=$(cat VERSION)" >> $GITHUB_OUTPUT
      - name: Download digests
        uses: actions/download-artifact@v4
        with:
          path: /tmp/digests
          pattern: digests-*
          merge-multiple: true
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Create manifest and push
        working-directory: /tmp/digests
        run: |
          docker buildx imagetools create \
            -t gemmaanalytics/ewah:v${{ steps.version.outputs.VERSION }} \
            -t gemmaanalytics/ewah:latest \
            $(printf 'gemmaanalytics/ewah@sha256:%s ' *)
