name: Publish
on:
  workflow_dispatch:
    inputs:
      versionName:
        description: 'Version Name'
        required: true

jobs:
  publish:
    name: Publish
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      # Setup .npmrc file to publish to npm https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 20.x
          registry-url: 'https://registry.npmjs.org'

      - run: yarn
      - run: yarn publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Create and push tag
        run: |
          git config --global user.email "steven@invisiblecommerce.com"
          git config --global user.name "$GITHUB_ACTOR"
          git tag -a $TAG -m "Release v$TAG"
          git push origin $TAG
        env:
          TAG: ${{ github.event.inputs.versionName }}

      - name: Create Release on GitHub
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.event.inputs.versionName }}
          name: v${{ github.event.inputs.versionName }}
          draft: false
          prerelease: false