name: Tag release commit

on:
  # Trigger the workflow on push only for the master branch
  push:
    branches:
      - master

env:
  NODE_ENV: 'development'
  GITHUB_TOKEN: ${{ secrets.BRANCH_CREATOR_TOKEN }}

jobs:
  tag:
      runs-on: ubuntu-latest
      # Only run on a release commit
      if: "startsWith(github.event.head_commit.message, 'chore(release):')"
      steps:
        - uses: actions/checkout@v2
          with:
            fetch-depth: '0'
            token: ${{ secrets.BRANCH_CREATOR_TOKEN }}
        - run: echo ::set-env name=RELEASE_VERSION::$(git log -1 --pretty=%B)
        # parse out non-tag text
        - run: echo ::set-env name=RELEASE_VERSION::$( echo $RELEASE_VERSION | sed 's/chore(release)://' )
        # remove spaces, but add back in `v` to tag, which is needed for standard-version
        - run: echo ::set-env name=RELEASE_VERSION::v$(echo $RELEASE_VERSION | tr -d '[:space:]')
        - run: echo $RELEASE_VERSION
        - run: git tag $RELEASE_VERSION

        - name: Setup git for push
          run: |
            git remote add github "$REPO"
            git config --local user.email "action@github.com"
            git config --local user.name "GitHub Action"
        - name: Push tags to master
          run: git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" HEAD:master --tags
