name: Assets
on:
  push:
    branches:
      - main
      - "v*.*"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
      - name: Install Erlang & Elixir
        uses: erlef/setup-beam@v1
        with:
          otp-version: ${{ env.otp }}
          elixir-version: ${{ env.elixir }}
      - name: Cache Mix
        uses: actions/cache@v4
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-
      # Note: we need to get Phoenix and LV because package.json points to them directly
      - name: Install mix dependencies
        run: mix deps.get
      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
      - name: Cache npm dependencies
        uses: actions/cache@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Clean generated assets
        run: rm -rf static/assets
      - name: Install npm dependencies
        run: npm ci --prefix assets
      - name: Build assets
        run: npm run deploy --prefix assets
      - name: Push updated assets
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: Update assets
          file_pattern: static
