name: NPM Publish

on:
  release:
    types: [published]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.event.release.target_commitish }}
    - name: Use Node.js 12
      uses: actions/setup-node@v1
      with:
        node-version: 12
        registry-url: https://registry.npmjs.org/
    - run: npm ci
    - run: git config --global user.name "Michael Heap"
    - run: git config --global user.email "m@michaelheap.com"
    - run: npm version ${{ github.event.release.tag_name }}
    - run: npm run build --if-present
    - run: npm test --if-present
    - run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
    - run: git push
      env:
        github-token: ${{ secrets.GITHUB_TOKEN }}
    - run: git tag -f ${{ github.event.release.tag_name }} ${{ github.event.release.target_commitish }}
      env:
        github-token: ${{ secrets.GITHUB_TOKEN }}
    - run: git push origin ${{ github.event.release.tag_name }} --force
      env:
        github-token: ${{ secrets.GITHUB_TOKEN }}
