image: node:18


stages:
  - test
  - release
  - deploy


# === Caches ===
.yarn-cache: &yarn-cache
  key: yarn-$CI_JOB_IMAGE
  paths:
    - .yarn


# === Proxy ===
# Description: sets up the proxy needed for the runner inside of the PTB. Produces a build.env that is used in the next jobs
proxy:
  stage: .pre
  script:
    - touch build.env
    - if [[ "$CI_RUNNER_TAGS" == *"behind_proxy"* ]]; then
    -   echo "behind_proxy=true" >> build.env
    -   echo "http_proxy=http://webproxy.bs.ptb.de:8080/" >> build.env
    -   echo "https_proxy=http://webproxy.bs.ptb.de:8080/" >> build.env
    - fi
  artifacts:
    reports:
      dotenv: build.env


# === Yarn Install ===
# Description: installs and caches dependencies
.yarn-install: &yarn-install
  - corepack enable
  # proxy settings
  - if [[ "$behind_proxy" == true ]]; then
  -   npm config set proxy $http_proxy
  -   npm config set https-proxy $https_proxy
  -   yarn config set -H httpProxy $http_proxy
  -   yarn config set -H httpsProxy $https_proxy
  - fi
  # install deps
  - yarn install --immutable


# === Job: Unit Tests ===
# Description: this job runs the unit tests
unit_tests:
  stage: test
  needs: ["proxy"]
  script:
    - *yarn-install
    - yarn run test:unit --coverage
  coverage: '/Lines\s*: \d+\.\d+/'
  cache:
    - <<: *yarn-cache
  artifacts:
    when: always
    paths:
      - junit.xml
      - coverage/cobertura-coverage.xml
    reports:
      junit:
        - junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml


# === Job: ESLint and Prettier ===
# Description: this job runs ESLint and Prettier
lint:
  stage: test
  needs: ["proxy"]
  script:
    - *yarn-install
    - yarn run test:lint
    - yarn run test:prettier
  cache:
    - <<: *yarn-cache


# === Job: Prepare Release
# Description: this job prepares an env file for an upcoming release
prepare_release:
  stage: .pre
  rules:
    - if: $CI_COMMIT_TAG =~ /v.*/
  script:
    - ./scripts/createReleaseEnv.sh >> release.env
  artifacts:
    paths:
      - release.description
    reports:
      dotenv: release.env


# === Job: Release
# Description: this job creates a new release using the provided env file
# === Variables ===
# NEXT_VER: the version of the upcoming release
# === Files ===
# release.description: multi-line description of the release
release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare_release
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG =~ /v.*/
  script:
    - echo "running release_job for $NEXT_VER"
  release:
    name: '$NEXT_VER'
    description: ./release.description
    tag_name: '$NEXT_VER'
    ref: '$CI_COMMIT_SHA'


# === Job: Deploy
# Description: this job creates publishes the package to the gitlab package registry
# === Variables ===
# NPM_AUTH_TOKEN: a deploy token with read_package_registry and write_package_registry scopes
deploy:
  stage: deploy
  needs: ["proxy"]
  rules:
    - if: $CI_COMMIT_TAG =~ /v.*/
  script:
    - *yarn-install
    - yarn npm publish
