# .gitlab-ci.yml — Build DOCX from markdown specs.
#
# Copy this file to the top level of your  specification repository and
# rename it to ".gitlab-ci.yml". Then configure the variables below in
# (Settings > CI/CD > Variables) or override them in your version of
# this file.
#
# The rendered DOCX is available as a downloadable job artifact.

variables:
  SPEC_INPUT_DIR: "specification" # path to markdown files, relative to repo root
  SPEC_ROOT: "specification" # specification root for section numbering (set to "" to disable)
  SPEC_NUMBER: "38XXX" # specification number (e.g., "38413", "23501")
  FRONT_PAGE_DATA: "" # path to front page JSON file (optional, leave empty to disable)
  SPECPRESS_REPO: "https://github.com/Ericsson/specpress.git" # Path to spec-press (not to your specification repo!)
  SPECPRESS_REF: "main" # Version of specpress (not of your specification!)

stages:
  - build

export-docx:
  stage: build
  image: node:20-alpine
  before_script:
    # Chromium is needed for mermaid diagram rendering (headless browser fallback)
    - apk add --no-cache git chromium
    - export CHROME_BIN=$(which chromium-browser)
  script:
    - git clone --depth 1 --branch $SPECPRESS_REF $SPECPRESS_REPO /tmp/specpress
    - cd /tmp/specpress && npm ci --omit=dev && cd -
    - mkdir -p output
    - COMMIT_DATE=$(git show -s --format=%ci HEAD | awk '{print $1"_"$2}' | tr ':' '-')
    - SHORT_SHA=$(git rev-parse --short HEAD)
    - BRANCH_NAME=$(echo $CI_COMMIT_REF_NAME | tr '/' '_')
    - DOCX_FILE="output/${COMMIT_DATE}_${SPEC_NUMBER}_${BRANCH_NAME}_${SHORT_SHA}.docx"
    # Check for CR cover page (CRxxxx.json in history folder)
    - |
      if [ -n "$SPEC_ROOT" ] && [ -f "${SPEC_ROOT}/history/CRxxxx.json" ]; then
        CR_COVER_PAGE="${SPEC_ROOT}/history/CRxxxx.json"
      else
        CR_COVER_PAGE=""
      fi
    - >
      node /tmp/specpress/lib/cli/export-docx.js
      "$SPEC_INPUT_DIR"
      "$DOCX_FILE"
      ${SPEC_ROOT:+--spec-root "$SPEC_ROOT"}
      ${CR_COVER_PAGE:+--cr-cover-page-data "$CR_COVER_PAGE"}
      ${FRONT_PAGE_DATA:+--front-page-data "$FRONT_PAGE_DATA"}
  artifacts:
    paths:
      - output/*.docx
    expire_in: 30 days
  only:
    - main
    - tags
