variables:
  TGU_SERVICE_DIR: "services/pcp-tgu-service"
  TGU_HELM_CHART_DIR: "helm/pcp-tgu-service"
  TGU_HELM_RELEASE: "pcp-tgu-service"
  TGU_IMAGE_REPOSITORY: "$INTERNAL_REGISTRY/pcp-tgu-service"
  TGU_IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"
  TGU_K8S_NAMESPACE_DEV: "pcp-dev"
  TGU_K8S_NAMESPACE_MASTER: "pcp-master"

.tgu_changes: &tgu_changes
  - services/pcp-tgu-service/**/*
#  - libs/pcp-types-lib/**/*
  - build.gradle.kts
  - settings.gradle.kts
  - .gitlab/ci/pcp-tgu-service.yml
  - helm/pcp-tgu-service/**/*
  - deploy/**/*

.tgu:rules:build-and-image:
  rules:
    - if: '$RUN_ALL == "true"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tgu_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_changes
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tgu_changes
    - when: never

.tgu:rules:test:
  rules:
    - if: '$RUN_ALL == "true"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes: *tgu_changes
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_changes
      when: manual
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tgu_changes
    - when: never

.tgu:rules:deploy-dev:
  rules:
    - if: '$RUN_ALL == "true"'
      when: manual
    - if: '$CI_COMMIT_BRANCH == "dev"'
      changes: *tgu_changes
      when: manual
    - when: never

.tgu:rules:deploy-master:
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes: *tgu_changes
      when: manual
    - when: never

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

.tgu: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:helm-deploy:
  image:
    name: alpine/helm:3.16.4
    entrypoint: [""]
  before_script:
    - mkdir -p ~/.kube
    - helm version

tgu:test:
  stage: test
  image: repo.nstart.cloud/library/gradle:jdk21-ubi10
  extends:
    - .tgu:rules:test
    - .tgu:gradle-cache
  script:
    - gradle --no-daemon :services:pcp-tgu-service:test
  artifacts:
    when: always
    reports:
      junit:
        - services/pcp-tgu-service/build/test-results/test/*.xml
    paths:
      - services/pcp-tgu-service/build/reports/tests/
    expire_in: 7 days

tgu:build:
  stage: build
  image: repo.nstart.cloud/library/gradle:jdk21-ubi10
  extends:
    - .tgu:rules:build-and-image
    - .tgu:gradle-cache
  needs:
    - job: tgu:test
      artifacts: true
  script:
    - gradle --no-daemon :services:pcp-tgu-service:bootJar
  artifacts:
    when: always
    paths:
      - services/pcp-tgu-service/build/libs/*.jar
    expire_in: 7 days

tgu:package:
  stage: package
  extends:
    - .tgu:rules:build-and-image
    - .tgu:docker-build
  needs:
    - job: tgu:build
      artifacts: true
  script:
    - 'echo "TGU_IMAGE_REPOSITORY=$TGU_IMAGE_REPOSITORY"'
    - mkdir -p docker-context/build/libs
    - cp "$TGU_SERVICE_DIR/Dockerfile" docker-context/Dockerfile
    - cp "$TGU_SERVICE_DIR"/build/libs/*.jar docker-context/build/libs/
    - docker build --file docker-context/Dockerfile --tag "$TGU_IMAGE_REPOSITORY:$TGU_IMAGE_TAG" docker-context
    - docker push "$TGU_IMAGE_REPOSITORY:$TGU_IMAGE_TAG"
    - printf "TGU_IMAGE=%s\n" "$TGU_IMAGE_REPOSITORY:$TGU_IMAGE_TAG" > tgu-image.env
  artifacts:
    reports:
      dotenv: tgu-image.env
    expire_in: 7 days

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

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