name: Deploy

on:
  workflow_dispatch:
  push:
    branches:
      - master
    paths:
      - setup.py

jobs:
  get_script:
    if: ${{ github.repository_owner == 'developomp' }} # prevents workflow from running in forked repos
    name: Get script
    runs-on: ubuntu-latest
    steps:
      - name: Checkout master
        uses: actions/checkout@v2

      - name: Archive Artifact
        uses: actions/upload-artifact@v2
        with:
          name: setup
          path: setup.py

  deploy_script:
    if: ${{ github.repository_owner == 'developomp' }} # prevents workflow from running in forked repos
    name: Deploy script
    runs-on: ubuntu-latest
    needs: get_script
    steps:
      - name: Checkout gh-pages
        uses: actions/checkout@v2
        with:
          ref: gh-pages
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

      - name: Download Artifact
        uses: actions/download-artifact@v2
        with:
          name: setup
          path: setup.py

      - name: Overwrite index.html
        run: |
          mv ./setup.py/setup.py ./index.html
          rm -rf ./setup.py

      - name: Commit files
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add index.html
          git commit -m "auto deploy from workflow"

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh-pages
