name: "Publish"

# the workflow is only triggered on version tag pushes to the master branch
on:
  push:
    tags:
      - v*.*.*

jobs:
  publish-npm:
    # running only on tag pushes
    if: ${{ github.ref_type == 'tag' }}

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Using Node 16
        uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: "https://registry.npmjs.org"
      - name: Update npm version
        run: npm install -g npm@7.x # stop showing warnings about the lockfile
      - name: Install dependencies
        run: npm ci
      - name: Run tests
        run: npm test
      - if: ${{ success() }}
        run: |
          npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
          npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
