name: Deploy ReSSPublica

on:
  workflow_run:
    workflows:
      - "Build RSS feeds"
    types:
      - completed

jobs:
  deploy:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}

    runs-on: ubuntu-latest

    steps:
      - name: Checkout generated files
        uses: actions/checkout@v4

      - name: Install rsync
        run: sudo apt-get update && sudo apt-get install -y rsync

      - name: Configure SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" >> ~/.ssh/known_hosts

      - name: Deploy feeds
        run: |
          rsync -avz --delete \
            -e "ssh -i ~/.ssh/id_ed25519" \
            --include='*/' \
            --include='*.xml' \
            --include='*.atom' \
            --exclude='*' \
            feed/ \
            ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/opt/server/resspublica/feed/

      - name: Deploy images
        run: |
          rsync -avz --delete \
            -e "ssh -i ~/.ssh/id_ed25519" \
            --include='*/' \
            --include='*.png' \
            --include='*.jpeg' \
            --include='*.jpg' \
            --include='*.webp' \
            --exclude='*' \
            .cache/ \
            ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/opt/server/resspublica/images/
