name: Update Memento Dev on VPS
run-name: ${{ gitea.actor }} is deploying the application on the VPS 🚀
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered."

      - name: Checkout the repository code
        uses: actions/checkout@v4

      - run: echo "💡 The repository has been cloned to the runner."

      - name: Setup SSH to the VPS
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H -p ${{ secrets.VPS_PORT }} ${{ secrets.VPS_HOST }} >> ~/.ssh/known_hosts
          echo "🔑 SSH has been set up to the VPS."

      - name: Pull main branch on the VPS
        env:
          VPS_USER: ${{ secrets.VPS_USER }}
          VPS_HOST: ${{ secrets.VPS_HOST }}
          VPS_PATH: ${{ secrets.VPS_PATH }}
          VPS_PORT: ${{ secrets.VPS_PORT }}
        run: |
          ssh -i ~/.ssh/id_ed25519 -p $VPS_PORT $VPS_USER@$VPS_HOST "cd $VPS_PATH && git pull origin main"
          echo "🚀 The main branch has been pulled on the VPS."

      - name: Building the application
        env:
          VPS_USER: ${{ secrets.VPS_USER }}
          VPS_HOST: ${{ secrets.VPS_HOST }}
          VPS_PATH: ${{ secrets.VPS_PATH }}
          VPS_PORT: ${{ secrets.VPS_PORT }}
        run: |
          ssh -i ~/.ssh/id_ed25519 -p $VPS_PORT $VPS_USER@$VPS_HOST "cd $VPS_PATH && COMPOSE_BAKE=true docker compose -f compose.prod.yml build"
          echo "📦 The application have been builded on the VPS."

      - name: Start the application
        env:
          VPS_USER: ${{ secrets.VPS_USER }}
          VPS_HOST: ${{ secrets.VPS_HOST }}
          VPS_PATH: ${{ secrets.VPS_PATH }}
          VPS_PORT: ${{ secrets.VPS_PORT }}
        run: |
          ssh -i ~/.ssh/id_ed25519 -p $VPS_PORT $VPS_USER@$VPS_HOST "cd $VPS_PATH && docker compose -f compose.prod.yml up -d"
          echo "🚀 The application has been started on the VPS."
