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: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_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_TOKEN }}" > .npmrc

      - name: Install
        run: yarn

      - name: Build
        run: yarn build

      - uses: wow-actions/use-app-token@v2
        with:
          app_id: ${{ secrets.GH_FRONTEGG_BOT_APP_ID }}
          private_key: ${{ secrets.GH_FRONTEGG_BOT_APP_SECRET }}
          # Specify true to save app token and app slug into the secrets of current repository
          secret: true
          # Specify true to clean saved secrets when workflow run completed
          clean: true
          
      - name: Set git config
        run: |
          git config --global user.name "$${{ secrets.BOT_NAME }}[bot]"
          git config --global user.email "${{ secrets.BOT_NAME }}[bot]@users.noreply.github.com"
        env:
          GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
      
      - name: Semantic Release
        run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
