name: backframe.js/deploy

on:
  push:
    branches:
      - main

permissions:
  contents: write
  issues: write
  pull-requests: write
  packages: write

env:
  CI: true
  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
  TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
  PG_DATABASE_URL: ${{ secrets.PG_DATABASE_URL }}
  PRISMA_DATABASE_URL: ${{ secrets.PRISMA_DATABASE_URL }}
  BF_AUTH_SECRET: "somesecret"

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: checkout code repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - uses: pnpm/action-setup@v2
        name: Install pnpm
        id: pnpm-install
        with:
          version: 7
          run_install: false

      - name: Configure pnpm
        run: |
          pnpm config set git-checks false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v3
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      # - name: Setup npmrc
      #   run: |
      #     echo "@backframe:registry=https://npm.pkg.github.com/" > ~/.npmrc
      #     echo "//npm.pkg.github.com/:_authToken=${{ secrets.NPM_TOKEN }}" >> ~/.npmrc

      - name: install dependencies
        run: pnpm install

      - name: build packages
        run: pnpm build

      - name: push db schema(s)
        run: pnpm turbo db:push

      - name: create and publish versions
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: changesets/action@v1
        with:
          commit: "ci: update versions"
          title: "ci: update versions"
          publish: pnpm ci:publish
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
