name: Laravel CI-CD

on:
  push:
    branches: ["main"]

env:
  KAMAL_IMAGE: ghcr.io/basecamp/kamal:v2.11.0

jobs:
  test:
    name: Tests Unitaires
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Run Tests
        uses: docker://laravelsail/php84-composer:latest
        env:
          APP_ENV: testing
          APP_KEY: base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
          DB_CONNECTION: sqlite
          DB_DATABASE: ":memory:"
        with:
          args: >
            bash -c "composer install --no-interaction --ignore-platform-req=ext-intl
            && php artisan test --compact"

  deploy:
    name: Deploy with Kamal
    needs: test
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Prepare SSH agent
        env:
          SSH_HOST: ${{ secrets.SSH_HOST }}
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          set -eu

          eval "$(ssh-agent -s)"
          echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >> "$GITHUB_ENV"

          printf '%s\n' "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          ssh-keyscan -H "${SSH_HOST:-89.167.35.217}" >> ~/.ssh/known_hosts

      - name: Prepare Kamal network
        env:
          SSH_HOST: ${{ secrets.SSH_HOST }}
          SSH_USER: ${{ secrets.SSH_USER }}
        run: |
          set -eu

          REMOTE_HOST="${SSH_HOST:-89.167.35.217}"
          REMOTE_USER="${SSH_USER:-root}"

          ssh "$REMOTE_USER@$REMOTE_HOST" <<'EOF'
          set -eu

          docker network inspect kamal >/dev/null 2>&1 || docker network create kamal

          for container in bemeal-pgsql bemeal-redis bemeal-meilisearch bemeal-adminer; do
            if docker inspect "$container" >/dev/null 2>&1; then
              docker network connect kamal "$container" >/dev/null 2>&1 || true
            fi
          done
          EOF

      - name: Write Kamal secrets
        run: |
          set -eu

          mkdir -p .kamal
          cat > .kamal/secrets <<'EOF'
          GITEA_TOKEN=$GITEA_TOKEN
          APP_KEY=$APP_KEY
          DB_DATABASE=$DB_DATABASE
          DB_USERNAME=$DB_USERNAME
          DB_PASSWORD=$DB_PASSWORD
          MEILISEARCH_KEY=$MEILISEARCH_KEY
          EOF
          chmod 600 .kamal/secrets

      - name: Deploy
        env:
          GITEA_TOKEN: ${{ secrets.TOKEN_GITEA }}
          APP_KEY: ${{ secrets.APP_KEY }}
          DB_DATABASE: ${{ secrets.DB_DATABASE }}
          DB_USERNAME: ${{ secrets.DB_USERNAME }}
          DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
          MEILISEARCH_KEY: ${{ secrets.MEILISEARCH_KEY }}
          SSH_HOST: ${{ secrets.SSH_HOST }}
          SSH_USER: ${{ secrets.SSH_USER }}
        run: |
          set -eu

          docker run --rm \
            -v "$PWD:/workdir" \
            -v "$SSH_AUTH_SOCK:/ssh-agent" \
            -v "$HOME/.ssh:/root/.ssh:ro" \
            -v /var/run/docker.sock:/var/run/docker.sock \
            -e SSH_AUTH_SOCK=/ssh-agent \
            -e GITEA_TOKEN \
            -e APP_KEY \
            -e DB_DATABASE \
            -e DB_USERNAME \
            -e DB_PASSWORD \
            -e MEILISEARCH_KEY \
            -e SSH_HOST \
            -e SSH_USER \
            "$KAMAL_IMAGE" deploy
