name: Post-Release

on:
  release:
    types: [published]

jobs:
  post-release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.repository.default_branch }}
          fetch-depth: 0

      - name: Put version in env
        run: echo "DRIBBBLISH_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV

      - name: Get commit SHA
        run: echo "LATEST_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: "14"

      - name: Build Webpack
        run: |
          npm install
          npm run build
          echo "${{ env.DRIBBBLISH_VERSION }}" > dist/VERSION
        env:
          DRIBBBLISH_VERSION: ${{ env.DRIBBBLISH_VERSION }}
          COMMIT_HASH: ${{ env.LATEST_SHA	}}

      - name: Push to release branch
        uses: s0/git-publish-subdir-action@develop
        env:
          REPO: self
          BRANCH: release
          FOLDER: dist
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set package.json version
        run: |
          sed -i 's/"version":.*",/"version": "${{ env.DRIBBBLISH_VERSION }}",/' package.json

      - name: Empty CHANGELOG.md
        run: |
          rm CHANGELOG.md
          touch CHANGELOG.md

      - name: Commit
        run: |
          git config --global user.email "action@github.com"
          git config --global user.name "github-actions"

          git add .
          git commit --allow-empty -m "Release v${{ env.DRIBBBLISH_VERSION }}"
          git push
