name: Release app
on:
  workflow_dispatch:
  push:
    branches: [main, beta]
permissions:
  id-token: write
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest
    env:
      YARN_IGNORE_ENGINES: true
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node 20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: yarn
          cache-dependency-path: yarn.lock
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: yarn install

      - name: Fix version
        if: github.ref_name == 'main'
        run: yarn fix-version


      - name: Publish to npm
        if: github.ref_name == 'main'
        run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      
      - name: Publish to npm beta
        if: github.ref_name == 'beta'
        run: npm publish --tag beta --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publish_release:
    needs: publish
    runs-on: ubuntu-latest
    env:
      YARN_IGNORE_ENGINES: true

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node 20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: yarn
          cache-dependency-path: yarn.lock

      - name: Install dependencies
        run: yarn install

      - name: Fix version
        if: github.ref_name == 'main'
        run: yarn fix-version

      - name: Release app
        run: yarn release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BEFORE: ${{ github.event.before }}
