stages:
  - setup
  - test
  - merge
  - upload

variables:
 # Define all required secrets in GitLab CI/CD variables:
 # SPARTIFY_KEY, PROJECT_ID
  ORDINO_KEY: "$ORDINO_KEY"
  PROJECT_ID: "$PROJECT_ID"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm
    - .cache
    - /root/.cache/ms-playwright 
    - node_modules/ 
setup:
  stage: setup
  image: node:20-alpine
  script:
    - npm run initialize --legacy-peer-deps --omit=optional
  artifacts:
    paths:
      - node_modules
      - .npmrc
      - .npm
      - /root/.cache/ms-playwright 
      - node_modules/
    expire_in: 1 hour

run-tests:
  stage: test
  image: mcr.microsoft.com/playwright:v1.40.0-focal
  needs:
    - job: setup
      artifacts: true
  before_script:
    - export PLAYWRIGHT_RETRIES=2
    - npx playwright install --with-deps
  script:
    - npm run oi:run:test || true 
  artifacts:
    paths:
      - ordino-report/mochawesome
    when: always
    name: "playwright-report"

upload-report:
  stage: upload
  image: curlimages/curl:latest
  needs:
    - job: run-tests
      artifacts: true
  script:
    - set -a; [ -f .env ] && source .env; set +a # by deafault, load .env file if it exists 
    - |
      API_URL="{{OrdinoURL}}/api/v1/test-report-external"
      curl --fail-with-body -X POST \
        -H "Spartify-Key: ${ORDINO_KEY}" \
        -F "file=@ordino-report/mochawesome/mochawesome.json" \
        -F "ProjectId=${PROJECT_ID}" \
        -F "ExecutionId=${CI_PIPELINE_ID}" \
        "${API_URL}"