# Exclude page pipeline to be run on "pages" branch
when:
  branch:
    exclude: pages

# Recursive cloning is used to fully clone the themes given as Git submodules
clone:
  git:
    image: woodpeckerci/plugin-git
    settings:
      recursive: true

steps:
  # Build zola static files
  build:
    image: alpine:edge
    commands:
      - apk add zola
      - zola --version
      - zola build
    when:
      event: [push, pull_request, manual]

  publish:
    image: bitnami/git
    # Must be set in Woodpecker configuration
    secrets: [mail, codeberg_token]
    commands:
      # Git configuration
      - git config --global user.email $MAIL
      - git config --global user.name "Woodpecker CI"
      # Clone output branch
      - git clone -b pages https://$CODEBERG_TOKEN@codeberg.org/$CI_REPO.git $CI_REPO_NAME
      # Enter output branch folder
      - cd $CI_REPO_NAME
      # Remove old files
      - git rm -r "*" || true # Ignore if it's empty
      # Copy build step output
      - cp -ar ../public/. .
      # Needed for custom domains
      - cp ../.domains . || true # Ignore if it doesn't exist
      # Commit and push all static files with source commit SHA
      - git add --all
      - git commit -m "Woodpecker CI ${CI_COMMIT_SHA} [SKIP CI]" || true
      - git push
    when:
      event: [push, manual]
