name: Prerelease 🚀

on:
  workflow_dispatch:

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
  prerelease:
    name: Changests Prerelease
    if: github.repository == 'evmts/evmts-monorepo'
    runs-on: ubuntu-latest
    # Permissions necessary for Changesets to push a new branch and open PRs
    # (for automated Version Packages PRs), and request the JWT for provenance.
    # More info: https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings
    permissions:
      contents: write
      pull-requests: write
      id-token: write
      actions: 'write'
    steps:
      - uses: actions/checkout@v3
        name: Checkout Repo
        with:
          submodules: recursive

      - name: "Setup"
        uses: ./.github/actions/setup

      - name: Build
        run: pnpm build
        env:
          NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}

      - name: Set deployment token
        run: npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Check for pre.json file existence
        id: check_files
        uses: andstor/file-existence-action@v2.0.0
        with:
          files: ".changeset/pre.json"

      - name: Enter next prerelease mode
        # If .changeset/pre.json does not exist and we did not recently exit
        # prerelease mode, enter prerelease mode with tag next
        if: steps.check_files.outputs.files_exists == 'false' && !contains(github.event.head_commit.message, 'Exit prerelease')
        run: npx changeset pre enter next

      - name: Create next version PR or publish 🚀
        uses: changesets/action@v1
        with:
          version: pnpm release:version
          publish: pnpm release:publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

