name: Publish

on:
  push:
    branches: [ master, next ]

jobs:
  build-and-test:
    uses: ./.github/workflows/_build-and-test.yaml
    secrets:
      npm_token: ${{ secrets.NPM_TOKEN }}

  publish:
    needs: build-and-test
    name: Publish
    runs-on: ubuntu-latest
    steps:
      - uses: wow-actions/use-app-token@v2
        with:
          app_id: ${{ secrets.GH_FRONTEGG_BOT_APP_ID }}
          private_key: ${{ secrets.GH_FRONTEGG_BOT_APP_SECRET }}

      - uses: actions/checkout@v4
        with:
          token: ${{ env.BOT_TOKEN }}
     
      - name: Use Node.js 18
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'
      - name: generate NPMRC file
        run:
          echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }}" > .npmrc

      - name: Install
        run: yarn

      - name: Build
        run: yarn build
    
      - name: Set git config
        run: |
          git config --global user.name "$${{ env.BOT_NAME }}[bot]"
          git config --global user.email "${{ env.BOT_NAME }}[bot]@users.noreply.github.com"
        env:
          GITHUB_TOKEN: ${{ env.BOT_TOKEN }}
      
      - name: Semantic Release
        run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ env.BOT_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
