name: Auto tag-release-publish

on:
  push:
    branches:
      - main

jobs:
  tag:
    name: Create tag for new version
    runs-on: ubuntu-latest
    outputs:
      tag_name: ${{ steps.create_new_tag.outputs.tag }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 2
      - uses: salsify/action-detect-and-tag-new-version@v2
        id: create_new_tag

  release:
    name: Create release
    runs-on: ubuntu-latest
    needs: tag
    if: needs.tag.outputs.tag_name
    steps:
      - uses: actions/checkout@v2
      - uses: actions/create-release@v1
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ needs.tag.outputs.tag_name }}
          release_name: ${{ needs.tag.outputs.tag_name }}
          draft: false
          prerelease: false

  publish:
    name: Build and publish
    runs-on: ubuntu-latest
    needs: tag
    if: needs.tag.outputs.tag_name
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14.x'
          registry-url: 'https://registry.npmjs.org'
      - run: yarn
      - run: yarn build
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
