name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version'
        required: true

jobs:
  release-moto-job:
    runs-on: ubuntu-latest
    name: Release Moto
    env:
      VERSION: 0.0.0
    steps:
    - name: Set Env
      run: |
        echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Set up Python 3.8
      uses: actions/setup-python@v4
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install wheel setuptools packaging twine --upgrade
    - name: Verify Tag does not exist
      run: |
        ! git rev-parse ${{ env.VERSION }} || { echo "Ensure that no tag exists for ${{ env.VERSION }}" ; exit 1; }
    - name: Verify supplied version exists in the CHANGELOG
      run: |
        grep ${{ env.VERSION }} CHANGELOG.md || { echo "Ensure that the CHANGELOG contains an entry for ${{ env.VERSION }}" ; exit 1; }
    - name: Set version number
      run: python update_version_from_git.py ${{ env.VERSION }}
    - name: Build
      run: python setup.py sdist bdist_wheel
    - name: Publish to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        password: ${{ secrets.PYPI_API_TOKEN }}
    - name: Tag version on Github
      run: |
        git tag ${{ env.VERSION }}
        git push origin ${{ env.VERSION }}
    - name: Build Docker release
      run: |
        docker build -t motoserver/moto . --tag moto:${{ env.VERSION }}
    # Required to get the correct Digest
    # See https://github.com/docker/build-push-action/issues/461
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
    - name: Login to DockerHub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - id: build_and_push
      name: Build and push
      uses: docker/build-push-action@v2
      with:
        platforms: linux/amd64,linux/arm64
        push: true
        tags: motoserver/moto:${{ env.VERSION }}
    - name: Increase patch version number
      run: |
        python update_version_from_git.py patch
        sed -i 's/Docker Digest for ${{ env.VERSION }}: <autopopulateddigest>/Docker Digest for ${{ env.VERSION }}: _${{ steps.build_and_push.outputs.digest }}_/' CHANGELOG.md
        git config --local user.email "admin@getmoto.org"
        git config --local user.name "Moto Admin"
        git add moto/__init__.py
        git add CHANGELOG.md
        git commit -m "Post-release steps"
        git push
