name: Build documentation
run-name: ${{ gitea.actor }} is building the Wiki documentation
on:
  push:
    branches:
      - develop
      - master
    paths:
    - Wiki/**
    - Wiki.Dockerfile

env:
  # Name of module and id separated by a slash
  INSTANCE: Wiki/wiki
  # Replace HI with the ID of the instance in capital letters
  ARTIFACT: webHelpWIKI2-all.zip
  # Writerside docker image version
  DOCKER_VERSION: 232.10165.1
  ALGOLIA_ARTIFACT: algolia-indexes-wiki.zip


jobs:
  preprocess:
    runs-on: ubuntu-latest
    outputs:
      sanitized_branch_name: ${{ steps.sanitize.outputs.sanitized_branch_name }}
    steps:
    - name: Sanitize branch name
      id: sanitize
      run: echo "::set-output name=sanitized_branch_name::$(echo ${{ github.ref_name }} | sed 's/\//-/g')"

  docs:
    runs-on: ubuntu-latest
    container: registry.jetbrains.team/p/writerside/builder/writerside-builder:${{env.DOCKER_VERSION}}
    steps:
    - name: Install basic dependencies
      run: |
        wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
        echo "::add-path::$HOME/.nvm"
        export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
        nvm install 18
        nvm use 18
        echo "::add-path::$(dirname $(which npm))"
        nvm --version
    - name: Check Node.js version
      run: |
        node -v
        npm -v
    - name: Checkout repository
      uses: https://github.com/actions/checkout@v3
    - name: Build docs
      run: |
        set -e
        export DISPLAY=:99
        Xvfb :99 &
        /opt/builder/bin/idea.sh helpbuilderinspect -source-dir . -product ${{env.INSTANCE}} -output-dir artifacts/ || true
        echo "Test existing of ${{ env.ARTIFACT }} artifact"
        test -e artifacts/${{ env.ARTIFACT }}
    - name: rename artifact
      run: |
        mv artifacts/${{ env.ARTIFACT }} artifacts/wiki.zip
    - name: Upload documentation
      uses: actions/upload-artifact@v3
      with:
        name: wiki.zip
        path: artifacts/wiki.zip
        retention-days: 14
    - name: Upload algolia-indexes
      uses: actions/upload-artifact@v4
      with:
        name: algolia-indexes.zip
        path: artifacts/${{ env.ALGOLIA_ARTIFACT }}
        retention-days: 14

  build-docs-container:
    runs-on: ubuntu-latest
    container: catthehacker/ubuntu:act-latest@sha256:986aebd60b14f6e79a4966c82266a95af5c56cd9adab6e98e8e5fcf595717e1b
    needs: [docs, preprocess]
    steps:
    - name: Checkout repository
      uses: https://github.com/actions/checkout@v3
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    - name: Login to Docker Hub
      uses: docker/login-action@v3
      with:
        registry: ${{ github.server_url }}
        username: ${{ github.actor }}
        password: ${{ secrets.REGISTRY_TOKEN }}
    - name: Retrieve docs artifact
      uses: actions/download-artifact@v4
      with:
        name: wiki.zip
        path: ${{ github.workspace }}
    - name: Unzip wiki.zip into .public
      run: |
        mkdir .public
        unzip -jo -qq ./wiki.zip/wiki.zip -d .public
    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        context: .
        file: Wiki.Dockerfile
        push: true
        tags: forge.rainote.dev/${{ github.repository }}:${{ needs.preprocess.outputs.sanitized_branch_name  }}-wiki
        platforms: linux/amd64,linux/arm64
    - name: Build and push to latest
      if: github.ref_name == 'master'
      uses: docker/build-push-action@v5
      with:
        context: .
        file: Wiki.Dockerfile
        push: true
        tags: forge.rainote.dev/${{ github.repository }}:latest-wiki
        platforms: linux/amd64, linux/arm64

  deploy-wiki:
    runs-on: ubuntu-latest
    container: catthehacker/ubuntu:act-latest@sha256:986aebd60b14f6e79a4966c82266a95af5c56cd9adab6e98e8e5fcf595717e1b
    needs: [build-docs-container, docs, preprocess]
    steps:
    - name: Deploy Image to CapRrover
      run: |
        docker run caprover/cli-caprover:2.2.3 caprover deploy --caproverUrl ${{ secrets.CAPROVER_SERVER }} --appToken ${{ secrets.WIKI_APP_TOKEN }} --imageName forge.rainote.dev/${{ github.repository }}:${{ needs.preprocess.outputs.sanitized_branch_name  }}-wiki -a ${{ secrets.WIKI_APP_NAME }}