name: Publish Documentation

on:
    push:
        paths: [ 'docs/**' ]
        branches: [ develop ]

jobs:
    build:
        runs-on: ubuntu-latest
        environment:
            name: Domain Watchdog Documentation
            url: https://domainwatchdog.eu

        steps:
            -   name: Checkout source
                uses: actions/checkout@v5
                with:
                    fetch-depth: 0

            -   name: Set up PHP
                uses: shivammathur/setup-php@v2
                with:
                    php-version: '8.4'
                    extensions: mbstring, xml, intl, curl, iconv, pdo_pgsql, sodium, zip, http

            -   name: Cache Composer dependencies
                uses: actions/cache@v3
                with:
                    path: vendor
                    key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
                    restore-keys: |
                        ${{ runner.os }}-composer-

            -   name: Install dependencies
                run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader

            -   name: Generate Swagger JSON
                run: bin/console api:openapi:export --output=docs/swagger_docs.json

            -   name: Setup Node
                uses: actions/setup-node@v4
                with:
                    node-version: 24

            -   name: Install dependencies
                run: npm install --global yarn && yarn install
                working-directory: docs

            -   name: Build Astro site
                run: yarn run build
                working-directory: docs


            -   name: Clean target folder on server
                uses: appleboy/ssh-action@v1
                with:
                    host: dw1.srv.domainwatchdog.eu
                    port: 2004
                    username: deploy
                    key: ${{ secrets.DEPLOYER_PRIVATE_KEY }}
                    script: |
                        rm -rf /var/www/domainwatchdog.eu/*

            -   name: Upload build via SCP
                uses: appleboy/scp-action@master
                with:
                    host: dw1.srv.domainwatchdog.eu
                    port: 2004
                    username: deploy
                    key: ${{ secrets.DEPLOYER_PRIVATE_KEY }}
                    source: "docs/dist/*"
                    target: /var/www/domainwatchdog.eu/
                    overwrite: true
                    debug: true
                    strip_components: 2
