name: Publish docs to Start9 Pages

# Publishes docs.start9.com on live-docs's behalf until a product tag carries
# the publish step in docs-deploy.yml onto that branch: a pull request to
# live-docs may not carry workflow files, so this one lives on master, follows
# every "Build and deploy docs" run, and checks live-docs out itself. It stands
# down by itself once the copy on live-docs publishes, and can then be deleted.

on:
  workflow_run:
    workflows: ['Build and deploy docs']
    types: [completed]
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: 'deploy-docs-pages'
  cancel-in-progress: false

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: projects/start-docs
    steps:
      - uses: actions/checkout@v6
        with:
          ref: live-docs

      # A failed run is followed too: its VPS steps fail once the VPS is gone,
      # and the build below is this workflow's own gate.
      - name: Stand down if live-docs publishes on its own
        id: live
        run: |
          if grep -q nextexplorer-publish ../../.github/workflows/docs-deploy.yml; then
            echo "publishes=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Setup Node.js
        if: steps.live.outputs.publishes != 'true'
        uses: actions/setup-node@v6
        with:
          node-version: '22'

      - name: Install mdBook
        if: steps.live.outputs.publishes != 'true'
        run: |
          MDBOOK_VERSION='v0.5.2'
          curl -sSL https://github.com/rust-lang/mdBook/releases/download/${MDBOOK_VERSION}/mdbook-${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz | tar -xz
          sudo mv mdbook /usr/local/bin/mdbook

      - name: Install mdbook-tabs
        if: steps.live.outputs.publishes != 'true'
        run: |
          cargo install mdbook-tabs --version 0.3.4
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Build the site
        if: steps.live.outputs.publishes != 'true'
        run: ./build.sh

      # The checkout is live-docs, so the action is taken from master by reference.
      # 0.3.5.x is the retired Sphinx site, seeded by hand and built by nothing.
      - name: Publish to NextExplorer
        if: steps.live.outputs.publishes != 'true'
        uses: Start9Labs/start-technologies/.github/actions/nextexplorer-publish@master
        with:
          url: https://${{ vars.NEXT_EXPLORER_EVELYN_URL }}
          email: ${{ vars.NEXT_EXPLORER_EVELYN_USER }}
          password: ${{ secrets.NEXT_EXPLORER_EVELYN_PASS }}
          dir: projects/start-docs/docs
          folder: docs.start9.com
          keep: 0.3.5.x