# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
name: Automate_Publish
on:
  workflow_run:
    workflows: ["Automate_Tag"]
    types:
      - completed
jobs:
  auto-publish:
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/heads/master')
    steps:
      - uses: actions/checkout@v2.2.0
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v1
        with:
          node-version: 14
          registry-url: https://registry.npmjs.org/
      - name: 'Get Previous tag'
        id: previoustag
        uses: "WyriHaximus/github-action-get-previous-tag@v1"
      - run: npm install
      - run: |
          npm_tag="v$(npm dist-tags | sed -n 's/^.* \([0-9].[0-9].[0-9]\)$/\1/p')"
          git_tag="$(git describe --abbrev=0)"
          if [ "$npm_tag" != "$git_tag" ] ; then npm publish; fi
        env:
          NODE_AUTH_TOKEN: ${{ secrets.npm_bot_token }}
      - name: 'Publish Release'
        uses: Roang-zero1/github-create-release-action@master
        with:
          created_tag: ${{ steps.previoustag.outputs.tag }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
