name: Ember CI

on:
  push:
    branches: [ main ]
    tags:
      - 'v*'
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x]  # Build on Node.js 18

    steps:
    - uses: actions/checkout@v2

    - name: Setup Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}

    - name: Setup pnpm
      uses: pnpm/action-setup@v2.0.1
      with:
        version: latest

    - 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: Create .npmrc
      run: |
        if [[ -n "${{ secrets._GITHUB_AUTH_TOKEN }}" ]]; then
          echo "@fleetbase:registry=https://npm.pkg.github.com/" > .npmrc
          echo "//npm.pkg.github.com/:_authToken=${{ secrets._GITHUB_AUTH_TOKEN }}" >> .npmrc
        fi

    - name: Install Dependencies
      run: pnpm install

    - name: Lint
      run: pnpm run lint

    - name: Build
      run: npx ember build --environment production