# pcp-tgu-ui-service — статический фронт ТГУ (Node/Vite → nginx). В отличие от
# остальных сервисов группы (gradle/Spring), собирается на Node: test = vitest,
# build = npm run build (tsc+vite), package = docker по своему Dockerfile.
variables:
  TGU_UI_SERVICE_DIR: "services/pcp-tgu-ui-service"
  TGU_UI_HELM_CHART_DIR: "helm/pcp-tgu-ui-service"
  TGU_UI_HELM_RELEASE: "pcp-tgu-ui-service"
  TGU_UI_IMAGE_REPOSITORY: "$INTERNAL_REGISTRY/pcp-tgu-ui-service"
  TGU_UI_IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"
  TGU_UI_K8S_NAMESPACE_DEV: "pcp-dev"
  TGU_UI_K8S_NAMESPACE_MASTER: "pcp-master"
  TGU_UI_NODE_IMAGE: "repo.nstart.cloud/nstart/node:24-trixie-slim"

.tgu_ui_changes: &tgu_ui_changes
  - services/pcp-tgu-ui-service/**/*
  - .gitlab/ci/pcp-tgu-ui-service.yml
  - helm/pcp-tgu-ui-service/**/*
  - deploy/**/*

.tgu-ui:rules:build-and-image:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tgu_ui_changes
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_ui_changes
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      changes: *tgu_ui_changes
    - when: never

.tgu-ui:rules:test:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tgu_ui_changes
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_ui_changes
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      changes: *tgu_ui_changes
    - when: never

.tgu-ui:rules:deploy-dev:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_ui_changes
      when: manual
    - when: never

.tgu-ui:rules:deploy-master:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      changes: *tgu_ui_changes
      when: manual
    - when: never

.tgu-ui:npm-cache:
  cache:
    key: "npm-tgu-ui-$CI_DEFAULT_BRANCH"
    paths:
      - services/pcp-tgu-ui-service/.npm
    policy: pull-push

.tgu-ui:docker-build:
  image: docker:26
  services:
    - name: docker:26-dind
      alias: docker
      command: ["--tls=false", "--insecure-registry=registry.k8s.265.nstart.local"]
  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
    DOCKER_BUILDKIT: "1"
  before_script:
    - docker version
    - docker info

.tgu-ui:helm-deploy:
  image:
    name: alpine/helm:3.16.4
    entrypoint: [""]
  before_script:
    - mkdir -p ~/.kube
    - helm version

tgu-ui:test:
  stage: test
  image: $TGU_UI_NODE_IMAGE
  extends:
    - .tgu-ui:rules:test
    - .tgu-ui:npm-cache
  script:
    - cd "$TGU_UI_SERVICE_DIR"
    - npm ci --cache .npm --prefer-offline
    - npm run test

tgu-ui:build:
  stage: build
  image: $TGU_UI_NODE_IMAGE
  extends:
    - .tgu-ui:rules:build-and-image
    - .tgu-ui:npm-cache
  script:
    - cd "$TGU_UI_SERVICE_DIR"
    - npm ci --cache .npm --prefer-offline
    - npm run build
  artifacts:
    when: always
    paths:
      - services/pcp-tgu-ui-service/dist/
    expire_in: 7 days

# Образ собирает сам Dockerfile (multi-stage: npm ci + build внутри). Артефакт
# tgu-ui:build — быстрый gate на tsc+vite перед более тяжёлым docker build.
tgu-ui:package:
  stage: package
  extends:
    - .tgu-ui:rules:build-and-image
    - .tgu-ui:docker-build
  needs:
    - job: tgu-ui:build
      artifacts: false
  script:
    - 'echo "TGU_UI_IMAGE_REPOSITORY=$TGU_UI_IMAGE_REPOSITORY"'
    - docker build --file "$TGU_UI_SERVICE_DIR/Dockerfile" --tag "$TGU_UI_IMAGE_REPOSITORY:$TGU_UI_IMAGE_TAG" "$TGU_UI_SERVICE_DIR"
    - docker push "$TGU_UI_IMAGE_REPOSITORY:$TGU_UI_IMAGE_TAG"
    - printf "TGU_UI_IMAGE=%s\n" "$TGU_UI_IMAGE_REPOSITORY:$TGU_UI_IMAGE_TAG" > tgu-ui-image.env
  artifacts:
    reports:
      dotenv: tgu-ui-image.env
    expire_in: 7 days

tgu-ui:deploy:dev:
  stage: deploy
  extends:
    - .tgu-ui:rules:deploy-dev
    - .tgu-ui:helm-deploy
  needs:
    - job: tgu-ui:package
      artifacts: true
  environment:
    name: dev/pcp-tgu-ui-service
  script:
    - 'echo "$KUBE_CONFIG_DEV_B64" | base64 -d > ~/.kube/config'
    - chmod 600 ~/.kube/config
    - 'export IMAGE_REPOSITORY="${TGU_UI_IMAGE%:*}"'
    - 'export IMAGE_TAG="${TGU_UI_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$TGU_UI_HELM_RELEASE"
      "$TGU_UI_HELM_CHART_DIR"
      --namespace "$TGU_UI_K8S_NAMESPACE_DEV"
      --create-namespace
      --wait
      --atomic
      --timeout 10m
      -f "$TGU_UI_HELM_CHART_DIR/values.yaml"
      -f "$TGU_UI_HELM_CHART_DIR/values-dev.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"

tgu-ui:deploy:master:
  stage: deploy
  extends:
    - .tgu-ui:rules:deploy-master
    - .tgu-ui:helm-deploy
  needs:
    - job: tgu-ui:package
      artifacts: true
  environment:
    name: master/pcp-tgu-ui-service
  script:
    - 'echo "$KUBE_CONFIG_MASTER_B64" | base64 -d > ~/.kube/config'
    - chmod 600 ~/.kube/config
    - 'export IMAGE_REPOSITORY="${TGU_UI_IMAGE%:*}"'
    - 'export IMAGE_TAG="${TGU_UI_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$TGU_UI_HELM_RELEASE"
      "$TGU_UI_HELM_CHART_DIR"
      --namespace "$TGU_UI_K8S_NAMESPACE_MASTER"
      --create-namespace
      --wait
      --atomic
      --timeout 10m
      -f "$TGU_UI_HELM_CHART_DIR/values.yaml"
      -f "$TGU_UI_HELM_CHART_DIR/values-master.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"
