name: "Deploy app-store-notifications"

on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - 'apps/app-store-notifications/**'

jobs:
  deploy-image:
    runs-on: ubuntu-24.04

    env:
      DOCKER_IMAGE_NAME: app-store-notifications
      SCW_REGION: fr-par
      SCW_REGISTRY_NAMESPACE: ${{ vars.SCW_REGISTRY_NAMESPACE }}
      SCW_CONTAINER_ID: ${{ vars.SCW_CONTAINER_ID }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v6.0.2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4.0.0

      - name: Login to Scaleway Container Registry
        uses: docker/login-action@v4.0.0
        with:
          registry: rg.${{ env.SCW_REGION }}.scw.cloud
          username: nologin
          password: ${{ secrets.SCW_SECRET_KEY }}

      - name: Build and push
        uses: docker/build-push-action@v7.0.0
        with:
          context: ./apps/app-store-notifications
          platforms: linux/amd64
          push: true
          tags: |
            rg.${{ env.SCW_REGION }}.scw.cloud/${{ env.SCW_REGISTRY_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }}:latest
            rg.${{ env.SCW_REGION }}.scw.cloud/${{ env.SCW_REGISTRY_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.sha }}

      - name: Configure Scaleway CLI
        uses: scaleway/action-scw@v0.0.2
        with:
          version: v2.52.0
          access-key: ${{ secrets.SCW_ACCESS_KEY }}
          secret-key: ${{ secrets.SCW_SECRET_KEY }}
          default-project-id: ${{ vars.SCW_PROJECT_ID }}
          default-organization-id: ${{ vars.SCW_ORGANIZATION_ID }}
          save-config: true

      - name: Configure Serverless Container
        run: >-
          scw container container update ${{ env.SCW_CONTAINER_ID }}
          registry-image=rg.${{ env.SCW_REGION }}.scw.cloud/${{ env.SCW_REGISTRY_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }}:${{ github.sha }}
          region=${{ env.SCW_REGION }}
          port=8080
          memory-limit=128
          cpu-limit=100
          min-scale=0
          max-scale=1
          http-option=redirected
          scaling-option.concurrent-requests-threshold=80
          sandbox=v2

      - name: Deploy Serverless Container
        run: scw container container deploy ${{ env.SCW_CONTAINER_ID }}
