# Generated from xtask::workflows::deploy_docs
# Rebuild with `cargo xtask workflows`.
name: deploy_docs
on:
  push:
    branches:
    - main
    - preview
    - stable
  workflow_dispatch:
    inputs:
      channel:
        description: 'Docs channel to deploy: nightly, preview, or stable'
        type: string
        default: nightly
jobs:
  deploy_docs:
    if: github.repository_owner == 'zed-industries'
    name: Build and Deploy Docs
    runs-on: namespace-profile-16x32-ubuntu-2204
    env:
      DOCS_AMPLITUDE_API_KEY: ${{ secrets.DOCS_AMPLITUDE_API_KEY }}
      MDBOOK_BOOK__SITE_URL: ${{ steps.resolve-channel.outputs.site_url }}
      DOCS_CHANNEL: ${{ steps.resolve-channel.outputs.channel }}
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - id: resolve-channel
      name: deploy_docs::resolve_channel_step
      run: |
        if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            CHANNEL="${{ inputs.channel }}"
        else
            case "${{ github.ref }}" in
                "refs/heads/main")
                    CHANNEL="nightly"
                    ;;
                "refs/heads/preview")
                    CHANNEL="preview"
                    ;;
                "refs/heads/stable")
                    CHANNEL="stable"
                    ;;
                *)
                    echo "::error::Unsupported ref for docs deploy: ${{ github.ref }}"
                    exit 1
                    ;;
            esac
        fi

        case "$CHANNEL" in
            "nightly")
                SITE_URL="/docs/nightly/"
                PROJECT_NAME="docs-nightly"
                ;;
            "preview")
                SITE_URL="/docs/preview/"
                PROJECT_NAME="docs-preview"
                ;;
            "stable")
                SITE_URL="/docs/"
                PROJECT_NAME="docs"
                ;;
            *)
                echo "::error::Invalid docs channel '$CHANNEL'. Expected one of: nightly, preview, stable."
                exit 1
                ;;
        esac

        echo "channel=$CHANNEL" >> "$GITHUB_OUTPUT"
        echo "site_url=$SITE_URL" >> "$GITHUB_OUTPUT"
        echo "project_name=$PROJECT_NAME" >> "$GITHUB_OUTPUT"
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
        path: ~/.rustup
    - name: steps::setup_linux
      run: ./script/linux
    - name: steps::install_mold
      run: ./script/install-mold
    - name: steps::download_wasi_sdk
      run: ./script/download-wasi-sdk
    - name: ./script/generate-action-metadata
      run: ./script/generate-action-metadata
    - name: deploy_docs::lychee_link_check
      uses: lycheeverse/lychee-action@82202e5e9c2f4ef1a55a3d02563e1cb6041e5332
      with:
        args: --no-progress --exclude '^http' './docs/src/**/*'
        fail: true
        jobSummary: false
    - name: deploy_docs::install_mdbook
      uses: peaceiris/actions-mdbook@ee69d230fe19748b7abf22df32acaa93833fad08
      with:
        mdbook-version: 0.4.37
    - name: deploy_docs::build_docs_book
      run: |
        mkdir -p target/deploy
        mdbook build ./docs --dest-dir=../target/deploy/docs/
    - name: deploy_docs::lychee_link_check
      uses: lycheeverse/lychee-action@82202e5e9c2f4ef1a55a3d02563e1cb6041e5332
      with:
        args: --no-progress --exclude '^http' 'target/deploy/docs'
        fail: true
        jobSummary: false
    - name: deploy_docs::pages_deploy_step
      uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        command: pages deploy target/deploy --project-name=${{ steps.resolve-channel.outputs.project_name }}
    - name: deploy_docs::deploy_install_script
      uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        command: r2 object put -f script/install.sh zed-open-source-website-assets/install.sh
    - name: deploy_docs::deploy_docs_worker
      uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        command: deploy .cloudflare/docs-proxy/src/worker.js
    - name: deploy_docs::upload_wrangler_logs
      if: always()
      uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
      with:
        name: wrangler_logs
        path: /home/runner/.config/.wrangler/logs/
    timeout-minutes: 60
defaults:
  run:
    shell: bash -euxo pipefail {0}
