name: 🚀 Create and publish a Docker image

on:
  push:
    branches: ['main', 'dev']

env:
  BACKEND_PATH: backend
  FRONTEND_PATH: frontend
  SERVER_DOMAIN: ${{ gitea.server_url.replace('https://', '') }}

jobs:
  detect-changes:
    runs-on: ubuntu-latest
    name: Detect changes in backend and frontend
    container: catthehacker/ubuntu:act-latest
    outputs:
      backend_changed: ${{ steps.backend-changed.outputs.backend }}
      frontend_changed: ${{ steps.frontend-changed.outputs.frontend }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Check for backend changes
        id: backend-changed
        uses: dorny/paths-filter@v2
        with:
          filters: |
            backend:
              - '${{ env.BACKEND_PATH }}/**'

      - name: Check for frontend changes
        id: frontend-changed
        uses: dorny/paths-filter@v2
        with:
          filters: |
            frontend:
              - '${{ env.FRONTEND_PATH }}/**'

  backend:
    runs-on: ubuntu-latest
    name: Build & publish backend image
    container: catthehacker/ubuntu:act-latest
    needs: [detect-changes]
    if: ${{ needs.detect-changes.outputs.backend_changed == 'true' }}
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: https://github.com/docker/metadata-action@v4
        with:
          images: ${{ vars.SERVER_DOMAIN }}/${{ gitea.repository }}/backend

      - name: Log in to the Container registry
        uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
        with:
          registry: ${{ vars.SERVER_DOMAIN }}
          username: ${{ gitea.actor }}
          password: ${{ secrets.TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
        with:
          context: ./${{ env.BACKEND_PATH }}
          file: ./${{ env.BACKEND_PATH }}/UniVerse.Api/Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
  frontend:
    runs-on: ubuntu-latest
    name: Build & publish frontend image
    container: catthehacker/ubuntu:act-latest
    needs: [detect-changes]
    if: ${{ needs.detect-changes.outputs.frontend_changed == 'true' }}
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Log in to the Container registry
        uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
        with:
          registry: ${{ vars.SERVER_DOMAIN }}
          username: ${{ gitea.actor }}
          password: ${{ secrets.TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: https://github.com/docker/metadata-action@v4
        with:
          images: ${{ vars.SERVER_DOMAIN }}/${{ gitea.repository }}/frontend

      - name: Build and push Docker image
        uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
        with:
          context: ./${{ env.FRONTEND_PATH }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

  deploy:
    runs-on: ubuntu-latest
    needs: [frontend, backend]
    # always() - костыль для того, чтобы деплой выполнялся даже если один из билдов пропущен
    if: github.ref == 'refs/heads/dev' && always() && (needs.backend.result == 'success' || needs.frontend.result == 'success')
    name: Update stack on Portainer
    steps:
      - name: Deploy Stage
        uses: fjogeleit/http-request-action@v1
        with:
          url: ${{ secrets.PORTAINER_WEBHOOK_URL }}
          method: 'POST'
          ignoreSsl: true
          timeout: 60000
