---
kind: pipeline
type: docker
name: build-and-deploy-docs

platform:
  os: linux
  arch: arm64

workspace:
  path: /drone/src

volumes:
  - name: dockersock
    host:
      path: /var/run/docker.sock

steps:
#  # 🏗️ Step 1: Build static docs for all projects
#  - name: build-docs
#    image: squidfunk/mkdocs-material
#    commands:
#      - echo "🧱 Building documentation for all projects..."
#      - cd mongo-ops && mkdocs build --clean && cd ..
#      - echo "✅ All docs built successfully."

  # 🐳 Step 2: Build Docker image for docs site
  - name: build-image
    image: docker:24
    volumes:
      - name: dockersock
        path: /var/run/docker.sock
    commands:
      - echo "🔨 Building Docker image for docs..."
      - docker build -t aetos/docs:latest .

  # 📤 Step 3: Push image to private registry
  - name: push-image
    image: docker:24
    environment:
      REGISTRY_HOST:
        from_secret: REGISTRY_HOST
      REGISTRY_USER:
        from_secret: REGISTRY_USER
      REGISTRY_PASS:
        from_secret: REGISTRY_PASS
    volumes:
      - name: dockersock
        path: /var/run/docker.sock
    commands:
      - echo "🔑 Logging into registry $REGISTRY_HOST ..."
      - echo "$REGISTRY_PASS" | docker login $REGISTRY_HOST -u "$REGISTRY_USER" --password-stdin
      - docker tag aetos/docs:latest $REGISTRY_HOST/aetos/docs:latest
      - echo "📦 Pushing image to $REGISTRY_HOST..."
      - docker push $REGISTRY_HOST/aetos/docs:latest

  - name: stop-old
    image: docker:24
    volumes:
      - name: dockersock
        path: /var/run/docker.sock
    commands:
      - echo "🛑 Stopping old container..."
      - docker rm -f docs || true

  - name: run-container
    image: docker:24
    volumes:
      - name: dockersock
        path: /var/run/docker.sock
    commands:
      - echo "🚀 Starting container aetos/docs:latest ..."
      - |
        docker run -d \
          --name docs \
          -p 6007:80 \
          -e NODE_ENV=production \
          --restart always \
          aetos/docs:latest

trigger:
  event:
    - push
