---
variables:
  SEMANTIC_RELEASE_RELEASERC_FILENAME: .releaserc.cjs

.fix:GL_TOKEN:
  stage: .pre
  image: docker.io/alpine:3
  variables:
    GIT_STRATEGY: none
    JOB_NAME: project automation
  script:
    - echo "[!] For the ${JOB_NAME} job, you need to provide a Protected and
      Masked variable named 'GL_TOKEN' with a {Project, Group, Personal} Access
      Token with 'api' scope."
    - echo "[+] For details, see the README at
      https://gitlab.com/just-ci/templates#how-to-use"
    - exit 1
  allow_failure: true
  rules:
    - if:
        "$CI_COMMIT_REF_PROTECTED == 'true' && $GL_TOKEN == null &&
        $DISABLE_GL_TOKEN_CHECK == null"

.release:rules:
  rules:
    - if:
        "$CI_COMMIT_REF_PROTECTED == 'true' && $GL_TOKEN && $CI_COMMIT_TAG ==
        null"

.release:install:
  script:
    # fallback to npm in case bun fails to run on old hardware
    - bun add --global ${NPM_PACKAGES} || npm install --global ${NPM_PACKAGES}
    # Expose globally-installed modules to require() in user `.releaserc.cjs`
    # (cosmiconfig resolves the file from the project dir, so Node won't
    # otherwise reach bun's / npm's global node_modules path).
    - export
      NODE_PATH="${BUN_INSTALL:-$HOME/.bun}/install/global/node_modules:$(npm
      root -g 2>/dev/null)"

# Fetches the default `releaserc-default.cjs` when the file pointed at by
# `SEMANTIC_RELEASE_RELEASERC_FILENAME` is not present in the repo. To
# commit your own config, set `SEMANTIC_RELEASE_RELEASERC_FILENAME` to its
# path (e.g. `.releaserc.json`) — the wget is skipped. The default file
# auto-loads `.releaserc.files.{yaml,yml,json}` and merges
# `semantic-release-replace-plugin` + extra git assets at semantic-release
# runtime — no shell merge logic, debuggable locally with
# `npx semantic-release --dry-run --no-ci`.
.release:configure:
  script:
    - |
      if ! test -f "${SEMANTIC_RELEASE_RELEASERC_FILENAME}"; then
        echo "[+] fetching ${SEMANTIC_RELEASE_RELEASERC} to ${SEMANTIC_RELEASE_RELEASERC_FILENAME}"
        wget -qO "${SEMANTIC_RELEASE_RELEASERC_FILENAME}" "${SEMANTIC_RELEASE_RELEASERC}"
      fi
