---

name: Release

on: workflow_dispatch

jobs:
  build:
    name: Publish a release
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.10"]

    steps:
      - uses: actions/checkout@v2

      - name: "Set up Python"
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}

      - name: "Calver calculate version"
        uses: StephaneBour/actions-calver@master
        id: calver
        with:
          date_format: "%Y.%m.%d"
          release: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: "Update changelog"
        uses: jacobtomlinson/gha-find-replace@v2
        env:
          NEXT_VERSION: ${{ steps.calver.outputs.release }}
        with:
          find: "Next\n----"
          replace: "Next\n----\n\n${{ env.NEXT_VERSION }}\n------------"
          include: "CHANGELOG.rst"
          regex: false

      - uses: stefanzweifel/git-auto-commit-action@v4
        id: commit
        with:
          commit_message: Bump CHANGELOG

      - name: Bump version and push tag
        id: tag_version
        uses: mathieudutour/github-tag-action@v6.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          custom_tag: ${{ steps.calver.outputs.release }}
          tag_prefix: ""
          commit_sha: ${{ steps.commit.outputs.commit_hash }}

      - name: Create a GitHub release
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ steps.tag_version.outputs.new_tag }}
          name: Release ${{ steps.tag_version.outputs.new_tag }}
          body: ${{ steps.tag_version.outputs.changelog }}

      - name: Build a binary wheel and a source tarball
        run: |
          # Checkout the latest tag - the one we just created.
          git fetch --tags
          git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
          python -m pip install build
          python -m build --sdist --wheel --outdir dist/ .

      - name: Publish distribution 📦 to PyPI
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verbose: true

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build base Docker image
        uses: docker/build-push-action@v2.7.0
        with:
          file: src/mock_vws/_flask_server/dockerfiles/base/Dockerfile
          push: false
          tags: |
            vws-mock:base

      - name: Build and push target manager Docker image
        uses: docker/build-push-action@v2.7.0
        with:
          file: src/mock_vws/_flask_server/dockerfiles/target_manager/Dockerfile
          push: true
          tags: |
            adamtheturtle/vuforia-target-manager-mock:latest
            adamtheturtle/vuforia-target-manager-mock:${{ steps.calver.outputs.release }}

      - name: Build and push VWS Docker image
        uses: docker/build-push-action@v2.7.0
        with:
          file: src/mock_vws/_flask_server/dockerfiles/vws/Dockerfile
          push: true
          tags: |
            adamtheturtle/vuforia-vws-mock:latest
            adamtheturtle/vuforia-vws-mock:${{ steps.calver.outputs.release }}

      - name: Build and push VWQ Docker image
        uses: docker/build-push-action@v2.7.0
        with:
          file: src/mock_vws/_flask_server/dockerfiles/vwq/Dockerfile
          push: true
          tags: |
            adamtheturtle/vuforia-vwq-mock:latest
            adamtheturtle/vuforia-vwq-mock:${{ steps.calver.outputs.release }}
