# 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

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    environment: development-fork
    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
        continue-on-error: true
        with:
          filters: |
            ts:
              - "ts/**"
              - ".github/workflows/build-ts.yml"

      - uses: pnpm/action-setup@v4
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        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 != 'false' }}
        with:
          node-version: 22
          #cache: "pnpm"
          #cache-dependency-path: ts/pnpm-lock.yaml

      - name: Login to Azure
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        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
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        uses: docker/setup-buildx-action@v3

      - if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        run: az acr build -t typeagent:latest -r typeagentContainerRegistry --file ${{ github.workspace }}/ts/Dockerfile --subscription ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_F36C1F2C4B2C49CA8DD5C52FAB98FA30 }} ${{ github.workspace }}/ts
