name: Deploy
on:
  workflow_run:
    workflows: [ci]
    strategy: wait-all
    timeout: 0
    branches: [main]
    types:
      - completed
  workflow_dispatch: # Allow manual trigger
concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: false
jobs:
  paas:
    if: vars.SSH_HOSTNAME != ''
    name: PaaS
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://${{ vars.HOSTNAME }}/
    concurrency: production
    env: ${{ secrets }}
    steps:
      - uses: actions/checkout@v6
      - uses: webfactory/ssh-agent@v0.9.1
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
      - name: Pin known hosts
        run: |
          echo "::group::Pinning known hosts"
          echo "${{ vars.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts
          echo "::debug::Known hosts updated:"
          cat ~/.ssh/known_hosts
          echo "::endgroup::"
      - name: Setting up Docker context for remote deployment
        run: |
          docker context create production --description "Production Server" --docker "host=ssh://github@${{ vars.SSH_HOSTNAME }}"
          docker context use production
          touch .env
      - name: Deploy production containers
        run: docker compose --file compose.paas.yml --project-name paas up --detach --quiet-pull
        env: ${{ vars }}
  app:
    if: vars.SSH_HOSTNAME != ''
    needs: paas
    name: App
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://${{ vars.HOSTNAME }}/
    concurrency: production
    env: ${{ secrets }}
    steps:
      - uses: actions/checkout@v6
      - uses: webfactory/ssh-agent@v0.9.1
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
      - name: Pin known hosts
        run: |
          echo "::group::Pinning known hosts"
          echo "${{ vars.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts
          echo "::debug::Known hosts updated:"
          cat ~/.ssh/known_hosts
          echo "::endgroup::"
      - name: Setting up Docker context for remote deployment
        run: |
          docker context create production --description "Production Server" --docker "host=ssh://github@${{ vars.SSH_HOSTNAME }}"
          docker context use production
          touch .env
      - name: Deploy production containers
        run: docker compose --file compose.app.yml --project-name app up --detach --quiet-pull
        env: ${{ vars }}
