name: Deploy and Release

# Controls when the workflow will run
on:
  # Triggers the workflow on version change
  push:
    branches: 
      - master
    paths:
      - dpath/version.py

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "deploy"
  deploy:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Get Version
        id: get-version
        run: |
          python -c "from dpath.version import VERSION; print(f'::set-output name=version::v{VERSION}');"

      - name: Check Tag
        uses: mukunku/tag-exists-action@v1.0.0
        id: check-tag
        with:
          tag: ${{ steps.get-version.outputs.version }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create Tag
        if: steps.check-tag.outputs.exists == 'false'
        uses: negz/create-tag@v1
        with:
          version: ${{ steps.get-version.outputs.version }}
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate Changelog
        id: generate-changelog
        uses: loopwerk/tag-changelog@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          config_file: .github/tag-changelog-config.js

      - name: PyPI Deployment
        uses: casperdcl/deploy-pypi@v2
        with:
          # PyPI username
          user: ${{ secrets.PYPI_USER }}
          # PyPI password or API token
          password: ${{ secrets.PYPI_PASS }}
          # `setup.py` command to run ("true" is a shortcut for "clean sdist -d <dist_dir> bdist_wheel -d <dist_dir>")
          build: clean sdist -d dist/
          # `pip` command to run ("true" is a shortcut for "wheel -w <dist_dir> --no-deps .")
          pip: true

      - name: Github Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.get-version.outputs.version }}
          body: ${{ steps.generate-changelog.outputs.changes }}
          files: dist/*
