name: NPM publish CD workflow

on:
  release:
    # This specifies that the build will be triggered when we publish a release
    types: [published]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.release.target_commitish }}
      # install Node.js
      - name: Use Node.js 14
        uses: actions/setup-node@v1
        with:
          node-version: 14.18.1
      - run: npm install
      # set up git since we will later push to the repo
      - run: git config --global user.name "Pablo Ibanez"
      - run: git config --global user.email "me@pabloibanez.com"
      - run: npm version ${{ github.event.release.tag_name }}
      # build the project
      - run: npm run build
      - run: npm config set '//registry.npmjs.org/:_authToken' "${{ secrets.NPM_TOKEN }}"
      - run: npm publish
        env:
          # Use a token to publish to NPM. See below for how to set it up
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      # push the version changes to GitHub
      - run: git push
        env:
          # The secret is passed automatically. Nothing to configure.
          github-token: ${{ secrets.GITHUB_TOKEN }}
