# Auto-generated by ninja-deploy for {{ project_name }}
name: Deploy {{ project_name }}

on:
  push:
    branches: [main]
  workflow_dispatch:

env:
  REGISTRY: {{ registry }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Install dependencies
        run: uv sync

      - name: Lint
        run: uv run ruff check .

      - name: Test
        run: uv run pytest --tb=short

  build:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    strategy:
      matrix:
        app:
{% for app in apps %}
          - {{ app }}
{% endfor %}
    steps:
      - uses: actions/checkout@v4

      - name: Log in to registry
        uses: docker/login-action@v3
        with:
          registry: {% raw %}${{ env.REGISTRY }}{% endraw %}

          username: {% raw %}${{ github.actor }}{% endraw %}

          password: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          file: infrastructure/docker/{% raw %}${{ matrix.app }}{% endraw %}/Dockerfile
          push: true
          tags: {% raw %}${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.app }}:${{ github.sha }}{% endraw %}

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4

      - name: Set up Helm
        uses: azure/setup-helm@v4

      - name: Set up kubeconfig
        uses: azure/k8s-set-context@v4
        with:
          kubeconfig: {% raw %}${{ secrets.KUBECONFIG }}{% endraw %}

      - name: Helm upgrade
        run: |
          helm upgrade --install {{ project_name }} \
            infrastructure/helm/{{ project_name }} \
            --values infrastructure/helm/{{ project_name }}/values-prod.yaml \
            --set image.tag={% raw %}${{ github.sha }}{% endraw %} \
            --wait --timeout 5m
