name: Deploy documentation
on:
  push:
    branches:
      - main
      - docs-snapshot
      - docs-ci-test
  workflow_dispatch: {}
  release:
    types:
      - released

concurrency:
  group: "pages-cf"
  cancel-in-progress: true

jobs:
  snapshot-docs:
    name: Snapshot documentation
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        # Run on releases only
        if: ${{ github.event.release }}
        with:
          ref: ${{ github.ref }}
          fetch-depth: 0
          fetch-tags: true
      - name: Merge with existing branch
        # Run on releases only
        if: ${{ github.event.release }}
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "github-actions@nadena.dev"
          git merge -s ours origin/docs-snapshot
          git push origin HEAD:docs-snapshot
  
  build-docs:
    name: Build documentation (latest release)
    uses: bdunderscore/modular-avatar/.github/workflows/build-test-docs.yml@main
    needs:
      - snapshot-docs
    with:
      ref: docs-snapshot
    
  build-docs-dev:
    name: Build documentation (main branch)
    uses: bdunderscore/modular-avatar/.github/workflows/build-test-docs.yml@main
    with:
      ref: main
      path: dev
      artifact: docs-dev

  deploy-docs:
    name: Deploy documentation
    needs: 
      - build-docs
      - build-docs-dev
    runs-on: ubuntu-latest

    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: 18

      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref || github.ref }}

      - name: Download artifact (tagged)
        uses: actions/download-artifact@v4
        with:
          name: docs
          path: docs/build

      - name: Download artifact (latest)
        uses: actions/download-artifact@v4
        with:
          name: docs-dev
          path: docs/build/dev

      - name: Dump file listing
        run: |
          ls -lR docs/build

      - name: Unpack documentation
        run: |
          mkdir -p docs-site~/public/dev
          tar -xf docs/build/docs.tar -C docs-site~/public
          tar -xf docs/build/dev/docs.tar -C docs-site~/public/dev
      
      - name: Setup yarn 2.0
        run: |
          corepack enable
          corepack prepare yarn@stable --activate
          
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "dir=$(cd docs-site~; yarn config get cacheFolder)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-site-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-site-
          
      - name: Build CF site
        run: |
          cd docs-site~
          YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install
          
      - name: Deploy to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          command: deploy --name modular-avatar-docs
          workingDirectory: docs-site~
          
      - name: Purge cache
        uses: nathanvaughn/actions-cloudflare-purge@992cc4e96422fb8ddf077281678373fe41e7736c
        continue-on-error: true
        with:
          cf_zone: ${{ secrets.CF_ZONE_ID }}
          cf_auth: ${{ secrets.CF_API_TOKEN }}