stages:
  - .pre
  - build
  - test
  - deploy

variables:
  GIT_DEPTH: 0
  GIT_STRATEGY: clone
  GIT_SUBMODULE_STRATEGY: recursive

include:
  - project: 'hive/common-ci-configuration'
    ref: 1536b0b84b4a4fcdab43be8f8552323b54a1fc54
    file:
      - '/templates/npm_projects.gitlab-ci.yml'

default:
  tags:
    - public-runner-docker

lint:
  stage: .pre
  extends: .npm_based_job_base
  script:
    - npm run lint-ci

# Builds and supplements wiki URL so it is available in Wiki and published package README
build:
  stage: build
  extends: .npm_build_template
  variables:
    DIST_DIR: "$CI_PROJECT_DIR/dist"
    NPM_PACKAGE_SCOPE: "@hiveio"
    NPM_PACKAGE_NAME: "workerbee"
    REPLACE_DOC_URL_ENV: "GEN_DOC_URL"
    REPLACE_FILE_PATH: "${CI_PROJECT_DIR}/README.md"
  needs:
    - job: lint

# Generates documentation
generate_docs:
  stage: build
  extends: .npm_based_job_base
  variables:
    DIST_DIR: "$CI_PROJECT_DIR/dist"
  script:
    - scripts/generate_api_docs.sh "${CI_PROJECT_URL}" "${CI_COMMIT_SHA}"
  needs:
    - job: build
      artifacts: true
  artifacts:
    paths:
      - "${DIST_DIR}/docs"
    when: always
    expire_in: 1 week

test:
  stage: test
  extends: .npm_test_template
  variables:
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
  needs:
    - job: build
      artifacts: true

push_to_wiki:
  extends: .npm_push_doc_template
  stage: deploy
  variables:
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    WIKI_PUSH_TOKEN: "$WIKI_PUSH_TOKEN"
    DIST_DIR: "$CI_PROJECT_DIR/dist"

  needs:
    - job: build
      artifacts: true
    - job: generate_docs
      artifacts: true

deploy_dev_package:
  stage: deploy
  extends: .npm_deploy_package_template
  variables:
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
    NPM_PACKAGE_SCOPE: "@hiveio"
  needs:
    - job: test
    - job: push_to_wiki
    - job: build
      artifacts: true

deploy_production_public_npm:
  stage: deploy
  extends: .registry_npmjs_org_deploy_package_template
  id_tokens:
    SIGSTORE_ID_TOKEN:
      aud: sigstore
  variables:
    NPM_PUBLISH_TOKEN: "$INTERNAL_HIDDEN_PUBLISH_TOKEN"
    NPM_PACKAGE_NAME: "workerbee"
    NPM_PROVENANCE_ENABLE: "0" # XXX: Temporarly disable as it is not working - we have to find a way to get it working
    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
  needs:
    - job: deploy_dev_package
    - job: test
    - job: push_to_wiki
    - job: build
      artifacts: true
