name: Release & Publish

on:
    push:
        # Sequence of patterns matched against refs/tags
        tags:
            - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
permissions: write-all

jobs:
    release:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
              with:
                  # Number of commits to fetch. 0 indicates all history for all branches and tags.
                  fetch-depth: 0
            - name: Changelog
              uses: Bullrich/generate-release-changelog@master
              id: Changelog
              env:
                  REPO: ${{ github.repository }}
            - name: Create Release
              id: create_release
              uses: actions/create-release@latest
              env:
                  # https://docs.github.com/en/actions/security-guides/automatic-token-authentication
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  tag_name: ${{ github.ref }}
                  release_name: Release ${{ github.ref }}
                  body: |
                      ${{ steps.Changelog.outputs.changelog }}
                  draft: false
                  prerelease: false

    publish:
        strategy:
            matrix:
                os: [ubuntu-latest]
                version: [18]
        runs-on: ${{ matrix.os }}
        steps:
            - uses: actions/checkout@v4
            # Setup .npmrc file to publish to npm
            - uses: actions/setup-node@v4
              with:
                  node-version: ${{ matrix.version }}
                  registry-url: 'https://registry.npmjs.org'
                  scope: '@myria'
              env:
                  NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
            - name: show npm config
              run: cat $NPM_CONFIG_USERCONFIG
            - name: Install dependencies
              run: npm ci
            - name: Build package
              run: npm run build
            - name: Publish package to NPM registry
              run: npm publish --access public
            # Temporary disable publishing to github package while package is testing in private repo. Enable once public it
            # - uses: actions/setup-node@v4
            #   with:
            #       node-version: ${{ matrix.version }}
            #       registry-url: 'https://npm.pkg.github.com'
            #       scope: '@myria'
            # - name: Publish package to NPM registry
            #   run: npm publish --access public
            #   env:
            #       NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
