# Default image for jobs - contains Hugo Extended
image: 
  name: hugomods/hugo:node
  entrypoint: [""]

variables:
  HUGO_ENV: production
  # Tells GitLab Runner to initialize and update submodules recursively
  GIT_SUBMODULE_STRATEGY: recursive
  SURFER_SERVER: "https://protocol.ecologies.info/"

stages:
  - build # Added build stage
  - deploy

# Job to build the Hugo site
build_site:
  stage: build
  before_script:
    - echo "installing NPM packages"
    - npm install
  script:
    - echo "Starting Hugo build..."
    - hugo version
    - hugo --minify --gc --cleanDestinationDir # Build the site
    - echo "Build completed. Public directory contents:"
    - ls -la public/
  artifacts:
    paths:
      - public/ # Pass the 'public' directory to the next stage
    expire_in: 1 hour # Optional: Set artifact expiry
  # Define when this job runs (e.g., only on the main branch)
  # Adjust 'only' or 'rules' as needed for your workflow
  only:
    - main # builds on commit to main branch

# Job to deploy the built site using cloudron-surfer
deploy_site:
  stage: deploy
  image: node:18 # Use Node.js image for cloudron-surfer
  needs: # Ensure 'build_site' job completes successfully first
    - job: build_site
      artifacts: true # Download artifacts from 'build_site'
  # No need for GIT_SUBMODULE_STRATEGY here if GIT_STRATEGY is none or repo isn't needed
  # If you need the repo source in this job, add GIT_SUBMODULE_STRATEGY here too.
  variables:
     GIT_STRATEGY: none # Optimization: Don't fetch repo source code for deploy job
  before_script:
    - echo "Installing cloudron-surfer..."
    - npm install -g cloudron-surfer
  script:
    - 'echo "Deploying to: $SURFER_SERVER"'
    # Verify artifact downloaded correctly
    - echo "Contents of downloaded artifact:"
    - ls -la public/ # Artifacts are extracted to the root of the job workspace
    - echo "Uploading files to server using surfer put..."
    - >-
      surfer put
      --token $SURFER_TOKEN
      --server $SURFER_SERVER
      public/* /
    - echo "Deployment completed successfully"
  # Define when this job runs (e.g., only on the main branch after build)
  # Adjust 'only' or 'rules' as needed for your workflow
  only:
    - main # publishes on commit to main branch

