name: Publish

on:
  schedule:
    - cron:  '0 20 * * *'

  push:
    branches:
      - main

jobs:
  Dockerhub:
    if: github.repository == 'vala-lang/valadoc-org'
    runs-on: ubuntu-latest
    steps:
      - name: Clone
        uses: actions/checkout@v4

      - name: Install the dependencies
        run: |
          export DEBIAN_FRONTEND=noninteractive
          curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
          sudo apt-get update -qq
          sudo apt-get install -qq --no-install-recommends software-properties-common
          sudo add-apt-repository -y ppa:vala-team/daily
          sudo apt-get update -qq
          curl -O https://repo.manticoresearch.com/manticore-repo.noarch.deb
          sudo dpkg -i manticore-repo.noarch.deb
          sudo apt-get update -qq
          sudo apt-get install \
          -qq \
          --no-install-recommends \
          gcc \
          libgee-0.8-dev \
          git \
          libguestfs-gobject-dev \
          libvaladoc-0.58-dev \
          php \
          php-curl \
          unzip \
          valac \
          valadoc \
          wget \
          xsltproc \
          make \
          nodejs \
          manticore \
          manticore-extra

      - name: Build the documentation
        run: |
          make build-docs GENERATOR_OPTS=""

      - name: Build search index
        run: |
          make configgen \
          && ./configgen ./valadoc.org/ \
          && mkdir ./sphinx/storage \
          && indexer --config ./sphinx.conf --all

      - name: Build website assets
        run: |
          npm ci && make build-data

      - name: Build image
        run: |
          docker build -t valalang/docs-tmp .
          CONTAINER_ID=$(sudo docker create -v "$(pwd)"/valadoc.org:/opt/valadoc.org --name valadoc valalang/docs-tmp:latest)
          docker start $CONTAINER_ID
          docker exec $CONTAINER_ID sh -c 'mv /opt/valadoc.org/* /var/www/html/'
          docker stop $CONTAINER_ID
          docker commit $CONTAINER_ID valalang/docs:latest

      - name: Build image and publish
        run: |
          docker login -u "${{ secrets.DockerUsername }}" -p "${{ secrets.DockerPassword }}"
          docker push valalang/docs:latest
