stages:
  - build
  - deploy

include:
  - template: Workflows/Branch-Pipelines.gitlab-ci.yml
  - project: 'hive/common-ci-configuration'
    ref: 19b15e2e0ea83de72ce3552a44ca59ab2201de73
    file:
      - '/templates/docker_image_jobs.gitlab-ci.yml'
      - '/templates/npm_projects.gitlab-ci.yml'

variables:
  GIT_DEPTH: 0
  GIT_STRATEGY: clone
  GIT_SUBMODULE_STRATEGY: recursive

default:
  tags:
    - public-runner-docker

build:
  extends: .npm_based_job_base
  stage: build
  script:
    - pnpm build
  artifacts:
    paths:
      - dist
    when: always
    expire_in: 1 week

.build_app_image_base:
  extends: .docker_image_builder_job_template
  stage: deploy

  needs:
    - job: build
      artifacts: true

  variables:
    GIT_SUBMODULE_STRATEGY: normal
    GIT_DEPTH: 1
    IMAGE_TAG: ""
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
    - ./scripts/ci-helpers/build_instance.sh --push "$IMAGE_TAG" "$CI_PROJECT_DIR" "$CI_REGISTRY_IMAGE" --progress="plain"

  artifacts:
    reports:
      dotenv: app_docker_image_name.env

build_app_image:
  extends: .build_app_image_base

  variables:
    IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"

push_protected_app_image:
  extends: .build_app_image_base
  stage: deploy
  variables:
    IMAGE_TAG: "$CI_COMMIT_REF_NAME"
  rules:
    - if: '$CI_COMMIT_REF_PROTECTED == "true"'
      when: on_success
    - when: never

pages:
  stage: deploy
  script:
    - echo "The site will be deployed to $CI_PAGES_URL"
  publish: dist
  artifacts:
    paths:
      - dist
  only:
    - main
