variables:
  GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle"

  COMPLEX_MISSION_SERVICE_DIR: "services/pcp-complex-mission-service"
  COMPLEX_MISSION_SERVICE_HELM_CHART_DIR: "helm/pcp-complex-mission-service"
  COMPLEX_MISSION_SERVICE_HELM_RELEASE: "pcp-complex-mission-service"
  COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY: "$INTERNAL_REGISTRY/pcp-complex-mission-service"
  COMPLEX_MISSION_SERVICE_IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"
  COMPLEX_MISSION_SERVICE_K8S_NAMESPACE_DEV: "pcp-dev"
  COMPLEX_MISSION_SERVICE_K8S_NAMESPACE_MASTER: "pcp-master"

  TLE_MONITORING_SERVICE_DIR: "services/tle-monitoring-service"
  TLE_MONITORING_HELM_CHART_DIR: "helm/tle-monitoring-service"
  TLE_MONITORING_HELM_RELEASE: "tle-monitoring-service"
  TLE_MONITORING_IMAGE_REPOSITORY: "$INTERNAL_REGISTRY/tle-monitoring-service"
  TLE_MONITORING_IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"
  TLE_MONITORING_K8S_NAMESPACE_DEV: "pcp-dev"
  TLE_MONITORING_K8S_NAMESPACE_MASTER: "pcp-master"

.complex_mission_service_changes: &complex_mission_service_changes
  - services/pcp-complex-mission-service/**/*
  - libs/ballistics-lib/**/*
  - libs/pcp-types-lib/**/*
  - build.gradle.kts
  - settings.gradle.kts
  - .gitlab/ci/pcp-complex-mission-service-tle.yml
  - helm/pcp-complex-mission-service/**/*
  - deploy/**/*

.tle_monitoring_changes: &tle_monitoring_changes
  - services/tle-monitoring-service/**/*
  - libs/ballistics-lib/**/*
  - libs/pcp-types-lib/**/*
  - build.gradle.kts
  - settings.gradle.kts
  - .gitlab/ci/pcp-complex-mission-service-tle.yml
  - helm/tle-monitoring-service/**/*
  - deploy/**/*

.complex-mission-service:rules:build-and-image:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *complex_mission_service_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *complex_mission_service_changes
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *complex_mission_service_changes
    - when: never

.complex-mission-service:rules:test:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *complex_mission_service_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *complex_mission_service_changes
      when: manual
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *complex_mission_service_changes
    - when: never

.complex-mission-service:rules:deploy-dev:
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *complex_mission_service_changes
      when: manual
    - when: never

.complex-mission-service:rules:deploy-master:
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *complex_mission_service_changes
      when: manual
    - when: never

.tle-monitoring:rules:build-and-image:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tle_monitoring_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tle_monitoring_changes
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tle_monitoring_changes
    - when: never

.tle-monitoring:rules:test:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tle_monitoring_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tle_monitoring_changes
      when: manual
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tle_monitoring_changes
    - when: never

.tle-monitoring:rules:deploy-dev:
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tle_monitoring_changes
      when: manual
    - when: never

.tle-monitoring:rules:deploy-master:
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tle_monitoring_changes
      when: manual
    - when: never

.pcp-gradle-cache:
  cache:
    key: "gradle-$CI_DEFAULT_BRANCH"
    paths:
      - .gradle/wrapper
      - .gradle/caches
    policy: pull-push

.pcp-docker-build:
  image: repo.nstart.cloud/library/docker:26
  services:
    - name: repo.nstart.cloud/library/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

.pcp-helm-deploy:
  image: repo.nstart.cloud/dtzar/helm-kubectl:3.14.0
  before_script:
    - mkdir -p /root/.kube ~/.kube
    - helm version
  after_script:
    - rm -f /root/.kube/config ~/.kube/config

complex-mission-service:test:
  stage: test
  image: repo.nstart.cloud/library/gradle:jdk17-ubi10
  extends:
    - .complex-mission-service:rules:test
    - .pcp-gradle-cache
  before_script:
    - chmod +x gradlew
  script:
    - ./gradlew --no-daemon :services:pcp-complex-mission-service:test
  artifacts:
    when: always
    reports:
      junit:
        - services/pcp-complex-mission-service/build/test-results/test/*.xml
    paths:
      - services/pcp-complex-mission-service/build/reports/tests/
    expire_in: 7 days

complex-mission-service:build:
  stage: build
  image: repo.nstart.cloud/library/gradle:jdk17-ubi10
  extends:
    - .complex-mission-service:rules:build-and-image
    - .pcp-gradle-cache
  needs:
    - job: complex-mission-service:test
      artifacts: true
  before_script:
    - chmod +x gradlew
  script:
    - ./gradlew --no-daemon :services:pcp-complex-mission-service:bootJar
  artifacts:
    when: always
    paths:
      - services/pcp-complex-mission-service/build/libs/*.jar
    expire_in: 7 days

complex-mission-service:package:
  stage: package
  extends:
    - .complex-mission-service:rules:build-and-image
    - .pcp-docker-build
  needs:
    - job: complex-mission-service:build
      artifacts: true
  script:
    - 'echo "COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY=$COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY"'
    - |
      JAR_COUNT="$(find "$COMPLEX_MISSION_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' | wc -l)"

      if [ "$JAR_COUNT" -ne 1 ]; then
        echo "Expected exactly one executable jar, found: $JAR_COUNT"
        find "$COMPLEX_MISSION_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' -print || true
        exit 1
      fi

      JAR_FILE="$(find "$COMPLEX_MISSION_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' | head -n 1)"
    - rm -rf docker-context
    - mkdir -p docker-context/build/libs
    - cp "$COMPLEX_MISSION_SERVICE_DIR/Dockerfile" docker-context/Dockerfile
    - cp "$JAR_FILE" docker-context/build/libs/app.jar
    - find docker-context -maxdepth 4 -type f -print -exec ls -la {} \;
    - docker build --file docker-context/Dockerfile --tag "$COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY:$COMPLEX_MISSION_SERVICE_IMAGE_TAG" docker-context
    - docker push "$COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY:$COMPLEX_MISSION_SERVICE_IMAGE_TAG"
    - printf "COMPLEX_MISSION_SERVICE_IMAGE=%s\n" "$COMPLEX_MISSION_SERVICE_IMAGE_REPOSITORY:$COMPLEX_MISSION_SERVICE_IMAGE_TAG" > complex-mission-service-image.env
  artifacts:
    reports:
      dotenv: complex-mission-service-image.env
    expire_in: 7 days

complex-mission-service:deploy:dev:
  stage: deploy
  extends:
    - .complex-mission-service:rules:deploy-dev
    - .pcp-helm-deploy
  needs:
    - job: complex-mission-service:package
      artifacts: true
  environment:
    name: dev/pcp-complex-mission-service
  script:
    - |
      if [ -n "${KUBE_CONFIG_DEV:-}" ]; then
        printf '%s' "$KUBE_CONFIG_DEV" > /root/.kube/config
      elif [ -n "${KUBE_CONFIG_DEV_B64:-}" ]; then
        if printf '%s' "$KUBE_CONFIG_DEV_B64" | grep -qE '(^apiVersion:|^kind: Config|^clusters:)'; then
          printf '%s' "$KUBE_CONFIG_DEV_B64" > /root/.kube/config
        elif ! printf '%s' "$KUBE_CONFIG_DEV_B64" | base64 -d > /root/.kube/config; then
          echo "KUBE_CONFIG_DEV_B64 is not valid base64. Set KUBE_CONFIG_DEV to raw kubeconfig or KUBE_CONFIG_DEV_B64 to base64 kubeconfig."
          exit 1
        fi
      else
        echo "KUBE_CONFIG_DEV or KUBE_CONFIG_DEV_B64 must be set."
        exit 1
      fi
    - chmod 600 /root/.kube/config
    - export KUBECONFIG=/root/.kube/config
    - 'export IMAGE_REPOSITORY="${COMPLEX_MISSION_SERVICE_IMAGE%:*}"'
    - 'export IMAGE_TAG="${COMPLEX_MISSION_SERVICE_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$COMPLEX_MISSION_SERVICE_HELM_RELEASE"
      "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR"
      --namespace "$COMPLEX_MISSION_SERVICE_K8S_NAMESPACE_DEV"
      --create-namespace
      --wait
      --atomic
      --timeout 10m
      -f "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR/values.yaml"
      -f "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR/values-dev.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"
      --set app.profile=dev
      --set app.configLabel=dev

complex-mission-service:deploy:master:
  stage: deploy
  extends:
    - .complex-mission-service:rules:deploy-master
    - .pcp-helm-deploy
  needs:
    - job: complex-mission-service:package
      artifacts: true
  environment:
    name: master/pcp-complex-mission-service
  script:
    - |
      if [ -n "${KUBE_CONFIG_MASTER:-}" ]; then
        printf '%s' "$KUBE_CONFIG_MASTER" > /root/.kube/config
      elif [ -n "${KUBE_CONFIG_MASTER_B64:-}" ]; then
        if printf '%s' "$KUBE_CONFIG_MASTER_B64" | grep -qE '(^apiVersion:|^kind: Config|^clusters:)'; then
          printf '%s' "$KUBE_CONFIG_MASTER_B64" > /root/.kube/config
        elif ! printf '%s' "$KUBE_CONFIG_MASTER_B64" | base64 -d > /root/.kube/config; then
          echo "KUBE_CONFIG_MASTER_B64 is not valid base64. Set KUBE_CONFIG_MASTER to raw kubeconfig or KUBE_CONFIG_MASTER_B64 to base64 kubeconfig."
          exit 1
        fi
      else
        echo "KUBE_CONFIG_MASTER or KUBE_CONFIG_MASTER_B64 must be set."
        exit 1
      fi
    - chmod 600 /root/.kube/config
    - export KUBECONFIG=/root/.kube/config
    - 'export IMAGE_REPOSITORY="${COMPLEX_MISSION_SERVICE_IMAGE%:*}"'
    - 'export IMAGE_TAG="${COMPLEX_MISSION_SERVICE_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$COMPLEX_MISSION_SERVICE_HELM_RELEASE"
      "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR"
      --namespace "$COMPLEX_MISSION_SERVICE_K8S_NAMESPACE_MASTER"
      --create-namespace
      --wait
      --atomic
      --timeout 10m
      -f "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR/values.yaml"
      -f "$COMPLEX_MISSION_SERVICE_HELM_CHART_DIR/values-master.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"
      --set app.profile=master
      --set app.configLabel=master

tle-monitoring:test:
  stage: test
  image: repo.nstart.cloud/library/gradle:jdk17-ubi10
  services:
    - name: repo.nstart.cloud/postgis/postgis:16-3.4
      alias: postgres
  variables:
    POSTGRES_DB: "pcp_tle"
    POSTGRES_USER: "postgres"
    POSTGRES_PASSWORD: "password"
    SPRING_DATASOURCE_URL: "jdbc:postgresql://postgres:5432/pcp_tle"
    SPRING_DATASOURCE_USERNAME: "postgres"
    SPRING_DATASOURCE_PASSWORD: "password"
    SPRING_TASK_SCHEDULING_ENABLED: "false"
  extends:
    - .tle-monitoring:rules:test
    - .pcp-gradle-cache
  before_script:
    - chmod +x gradlew
  script:
    - ./gradlew --no-daemon :services:tle-monitoring-service:test
  artifacts:
    when: always
    reports:
      junit:
        - services/tle-monitoring-service/build/test-results/test/*.xml
    paths:
      - services/tle-monitoring-service/build/reports/tests/
    expire_in: 7 days

tle-monitoring:build:
  stage: build
  image: repo.nstart.cloud/library/gradle:jdk17-ubi10
  extends:
    - .tle-monitoring:rules:build-and-image
    - .pcp-gradle-cache
  needs:
    - job: tle-monitoring:test
      artifacts: true
  before_script:
    - chmod +x gradlew
  script:
    - ./gradlew --no-daemon :services:tle-monitoring-service:bootJar
  artifacts:
    when: always
    paths:
      - services/tle-monitoring-service/build/libs/*.jar
    expire_in: 7 days

tle-monitoring:package:
  stage: package
  extends:
    - .tle-monitoring:rules:build-and-image
    - .pcp-docker-build
  needs:
    - job: tle-monitoring:build
      artifacts: true
  script:
    - 'echo "TLE_MONITORING_IMAGE_REPOSITORY=$TLE_MONITORING_IMAGE_REPOSITORY"'
    - |
      JAR_COUNT="$(find "$TLE_MONITORING_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' | wc -l)"

      if [ "$JAR_COUNT" -ne 1 ]; then
        echo "Expected exactly one executable jar, found: $JAR_COUNT"
        find "$TLE_MONITORING_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' -print || true
        exit 1
      fi

      JAR_FILE="$(find "$TLE_MONITORING_SERVICE_DIR/build/libs" -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' | head -n 1)"
    - rm -rf docker-context
    - mkdir -p docker-context/build/libs
    - cp "$TLE_MONITORING_SERVICE_DIR/Dockerfile" docker-context/Dockerfile
    - cp "$JAR_FILE" docker-context/build/libs/app.jar
    - find docker-context -maxdepth 4 -type f -print -exec ls -la {} \;
    - docker build --file docker-context/Dockerfile --tag "$TLE_MONITORING_IMAGE_REPOSITORY:$TLE_MONITORING_IMAGE_TAG" docker-context
    - docker push "$TLE_MONITORING_IMAGE_REPOSITORY:$TLE_MONITORING_IMAGE_TAG"
    - printf "TLE_MONITORING_IMAGE=%s\n" "$TLE_MONITORING_IMAGE_REPOSITORY:$TLE_MONITORING_IMAGE_TAG" > tle-monitoring-image.env
  artifacts:
    reports:
      dotenv: tle-monitoring-image.env
    expire_in: 7 days

tle-monitoring:deploy:dev:
  stage: deploy
  extends:
    - .tle-monitoring:rules:deploy-dev
    - .pcp-helm-deploy
  needs:
    - job: tle-monitoring:package
      artifacts: true
  environment:
    name: dev/tle-monitoring-service
  script:
    - |
      if [ -n "${KUBE_CONFIG_DEV:-}" ]; then
        printf '%s' "$KUBE_CONFIG_DEV" > /root/.kube/config
      elif [ -n "${KUBE_CONFIG_DEV_B64:-}" ]; then
        if printf '%s' "$KUBE_CONFIG_DEV_B64" | grep -qE '(^apiVersion:|^kind: Config|^clusters:)'; then
          printf '%s' "$KUBE_CONFIG_DEV_B64" > /root/.kube/config
        elif ! printf '%s' "$KUBE_CONFIG_DEV_B64" | base64 -d > /root/.kube/config; then
          echo "KUBE_CONFIG_DEV_B64 is not valid base64. Set KUBE_CONFIG_DEV to raw kubeconfig or KUBE_CONFIG_DEV_B64 to base64 kubeconfig."
          exit 1
        fi
      else
        echo "KUBE_CONFIG_DEV or KUBE_CONFIG_DEV_B64 must be set."
        exit 1
      fi
    - chmod 600 /root/.kube/config
    - export KUBECONFIG=/root/.kube/config
    - 'export IMAGE_REPOSITORY="${TLE_MONITORING_IMAGE%:*}"'
    - 'export IMAGE_TAG="${TLE_MONITORING_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$TLE_MONITORING_HELM_RELEASE"
      "$TLE_MONITORING_HELM_CHART_DIR"
      --namespace "$TLE_MONITORING_K8S_NAMESPACE_DEV"
      --create-namespace
      --wait
      --atomic
      --timeout 15m
      -f "$TLE_MONITORING_HELM_CHART_DIR/values.yaml"
      -f "$TLE_MONITORING_HELM_CHART_DIR/values-dev.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"
      --set app.profile=dev
      --set app.configLabel=dev

tle-monitoring:deploy:master:
  stage: deploy
  extends:
    - .tle-monitoring:rules:deploy-master
    - .pcp-helm-deploy
  needs:
    - job: tle-monitoring:package
      artifacts: true
  environment:
    name: master/tle-monitoring-service
  script:
    - |
      if [ -n "${KUBE_CONFIG_MASTER:-}" ]; then
        printf '%s' "$KUBE_CONFIG_MASTER" > /root/.kube/config
      elif [ -n "${KUBE_CONFIG_MASTER_B64:-}" ]; then
        if printf '%s' "$KUBE_CONFIG_MASTER_B64" | grep -qE '(^apiVersion:|^kind: Config|^clusters:)'; then
          printf '%s' "$KUBE_CONFIG_MASTER_B64" > /root/.kube/config
        elif ! printf '%s' "$KUBE_CONFIG_MASTER_B64" | base64 -d > /root/.kube/config; then
          echo "KUBE_CONFIG_MASTER_B64 is not valid base64. Set KUBE_CONFIG_MASTER to raw kubeconfig or KUBE_CONFIG_MASTER_B64 to base64 kubeconfig."
          exit 1
        fi
      else
        echo "KUBE_CONFIG_MASTER or KUBE_CONFIG_MASTER_B64 must be set."
        exit 1
      fi
    - chmod 600 /root/.kube/config
    - export KUBECONFIG=/root/.kube/config
    - 'export IMAGE_REPOSITORY="${TLE_MONITORING_IMAGE%:*}"'
    - 'export IMAGE_TAG="${TLE_MONITORING_IMAGE##*:}"'
    - 'echo "Deploy image: $IMAGE_REPOSITORY:$IMAGE_TAG"'
    - >
      helm upgrade --install "$TLE_MONITORING_HELM_RELEASE"
      "$TLE_MONITORING_HELM_CHART_DIR"
      --namespace "$TLE_MONITORING_K8S_NAMESPACE_MASTER"
      --create-namespace
      --wait
      --atomic
      --timeout 15m
      -f "$TLE_MONITORING_HELM_CHART_DIR/values.yaml"
      -f "$TLE_MONITORING_HELM_CHART_DIR/values-master.yaml"
      --set image.repository="$IMAGE_REPOSITORY"
      --set image.tag="$IMAGE_TAG"
      --set app.profile=master
      --set app.configLabel=master
