name: Docs
on:
  schedule:
    - cron: '0 9 * * 1'
  workflow_dispatch:
    inputs:
      version-number:
        type: string
        description: What is the new version number (ex. v0.28.0) for the website? If updating dev, leave this blank.
        default: master
        required: false

permissions:
  id-token: write
  contents: read

jobs:
  documentation:
    if: github.repository == 'deepjavalibrary/djl'
    runs-on: ubuntu-latest
    steps:
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'corretto'
          java-version: 17

      - name: Set up Python3
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Install CN fonts
        run: sudo apt-get update && sudo apt-get install fonts-arphic-uming

      - name: Install Python Dependencies
        run: pip3 install nbconvert mkdocs==1.4.3 mkdocs-exclude mknotebooks mkdocs-material jupyter Pygments Markdown mike

      - name: Cache IJava Kernel
        id: ijava-cache
        uses: actions/cache@v4
        with:
          path: ~/.local/share/jupyter/kernels/java
          key: ${{ runner.os }}-ijava-kernel

      - name: Install IJava kernel
        if: steps.ijava-cache.outputs.cache-hit != 'true'
        run: |
          git clone https://github.com/frankfliu/IJava.git
          cd IJava/
          ./gradlew installKernel

      - name: Checkout DJL Repo
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.version-number }}

      - name: Checkout DJL-Serving
        uses: actions/checkout@v4
        with:
          repository: 'deepjavalibrary/djl-serving'
          ref: ${{ github.event.inputs.version-number }}
          path: docs/serving

      - name: Checkout DJL-Demo
        uses: actions/checkout@v4
        with:
          repository: 'deepjavalibrary/djl-demo'
          ref: ${{ github.event.inputs.version-number }}
          path: docs/demos

      - name: Cache Jupyter Notebooks
        id: jupyter-notebooks-cache
        uses: actions/cache@v4
        with:
          path: docs/demos/jupyter
          key: ${{ runner.os }}-jupyter-${{ hashFiles('docs/demos/jupyter/*') }}

      - name: Run Notebooks
        if: steps.jupyter-notebooks-cache.outputs.cache-hit != 'true'
        working-directory: docs/demos/jupyter
        env:
          DJL_DISABLE_PROGRESS_BAR: true
        run: bash test_notebook.sh

      - name: Build docs website
        run: tools/scripts/build-website.sh "${{ github.event.inputs.version-number || 'master' }}"

      - name: Configure Deployment AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::425969335547:role/djl-ci-publish-role
          aws-region: us-east-2

      - name: Copy files to S3 with the AWS CLI
        env:
          DJL_VERSION: ${{ github.event.inputs.version-number || 'master' }}
        run: |
          aws s3 sync $DJL_VERSION s3://djl-ai/documentation/$DJL_VERSION --delete
          aws s3 cp versions.json s3://djl-ai/documentation/versions.json
          aws cloudfront create-invalidation --distribution-id E733IIDCG0G5U --paths "/*"


