image: node:20.19.5

variables:
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - test
  - build_base_with_plugins
  - upload_build

test:
  stage: test
  script:
    - yarn --frozen-lockfile
    - yarn test

build_base_with_plugins:
  stage: build_base_with_plugins
  cache:
    key: "$CI_COMMIT_REF_NAME"
  artifacts:
    when: always
    paths:
      - sbom.json
      - sulu-*.tar.gz
  script:
    - make pull
    - VERSION="v$CI_COMMIT_SHORT_SHA" CORE_DIR="./core/sulu" make
    - apt update
    - apt install -y rsync
    - npm install -g @cyclonedx/cdxgen
    - cd build && cdxgen -o ../sbom.json && cd ..
    - make tar

upload_build:
  stage: upload_build
  dependencies:
    - build_base_with_plugins
  script:
    - ls
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get -y update -qq
    - apt-get -y install rsync -qq
    - echo "$FIRMWARE_SERVER_PRIVATE_KEY"
    - chmod 600 "$FIRMWARE_SERVER_PRIVATE_KEY"
    - rsync -v -e "ssh -o 'StrictHostKeyChecking no' -i $FIRMWARE_SERVER_PRIVATE_KEY" sulu-*.tar.gz "$FIRMWARE_SERVER_ADDRESS:$FIRMWARE_SERVER_UPLOAD_PATH/"
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: on_success
    - if: $CI_COMMIT_REF_PROTECTED == "true"
      when: manual
