# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# This workflow builds a Docker container and deploys it to the TypeAgent container repository

name: Build and deploy Node.js app to Azure Web App - typeagent

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest # pnpm deploy does not work currently on Windows. Fails with EPERM.
    steps:
      - name: Setup Git LF
        run: |
          git config --global core.autocrlf false

      - uses: actions/checkout@v4
        with:
          ref: main

      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            ts:
              - "ts/**"
              - ".github/workflows/build-ts.yml"

      - uses: pnpm/action-setup@v4
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts == 'true' }}
        name: Install pnpm
        with:
          package_json_file: ts/package.json

      - uses: actions/setup-node@v4
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts == 'true' }}
        with:
          node-version: ${{ matrix.version }}
          #cache: "pnpm"
          #cache-dependency-path: ts/pnpm-lock.yaml

      - name: Login to Azure
        uses: azure/login@v2.2.0
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_5B0D2D6BA40F4710B45721D2112356DD }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_39BB903136F14B6EAD8F53A8AB78E3AA }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_F36C1F2C4B2C49CA8DD5C52FAB98FA30 }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - run: az acr build -t typeagent:latest -r typeagentContainerRegistry --file ${{ github.workspace }}/ts/Dockerfile --subscription b64471de-f2ac-4075-a3cb-7656bca768d0 ${{ github.workspace }}/ts
