stages:
  - get chart
  - build prereqs
  - deploy rabbit
  - deploy storage
  - deploy GRB
  - deploy G2G
  - deploy WMS
  - deploy WMTS

variables:
  DOCKER_TLS_CERTDIR: ""
  DOCKER_HOST: "tcp://localhost:2375"
  DOCKER_DRIVER: overlay2
  HELM_EXPERIMENTAL_OCI: "1"
  CICHART_IMAGE: "$CI_REGISTRY_IMAGE/cichart:latest"


include:
  - local: "/helpers/build_image.yaml"
  - local: "/helpers/docker_base.yaml"
  - local: "/helpers/helm_base.yaml"


build ci:
  stage: .pre
  extends: .docker_based_job
  script:
    - cd cichart
    - docker build --tag $CICHART_IMAGE .
    - docker push $CICHART_IMAGE
  rules:
    - changes:
        - cichart/Dockerfile
      when: always
    - if: $BUILD_CI_IMAGE
      when: always

build sidecar rabbit init:
  stage: build prereqs
  extends: .build_image
  variables:
    IMAGE_NAME: cspp-geo-rabbit-init
    IMAGE_DIR: sidecars/cspp-geo-rabbit-init
  rules:
    - changes:
        - sidecars/cspp-geo-rabbit-init/Dockerfile
        - sidecars/cspp-geo-rabbit-init/declare_exchange.py
      when: always
    - if: $BUILD_PREREQS
      when: always


.get_chart_tmpl:
  extends: .helm_based_job
  stage: get chart
  variables:
    SUBCOMP_REPOS_BASE: "https://gitlab.ssec.wisc.edu/cspp_geo/geosphere"
    # required:
    SUBCOMP_REPOS: ""
    SUBCOMP_CHART_DIR: ""
  script:
    - repos_url="${SUBCOMP_REPOS_BASE}/${SUBCOMP_REPOS}"
    - git clone --depth 1 $repos_url
    - cd ${SUBCOMP_REPOS}
    # most recent docker image should be tagged with most recent SHA
    - docker_tag=$(git rev-parse --short HEAD)
    - echo $docker_tag
    - cd chart
    # make the docker tag available in later stages
    - echo export docker_tag=$docker_tag >${SUBCOMP_CHART_DIR}/cibuild.env
    - |-
      sed -i "s/^appVersion: .*\$/appVersion: ${docker_tag}/g" ${SUBCOMP_CHART_DIR}/Chart.yaml
    # debug:
    - helm template ${SUBCOMP_CHART_DIR} ${SUBCOMP_CHART_DIR}
  artifacts:
    paths:
      - ${SUBCOMP_REPOS}/chart/${SUBCOMP_CHART_DIR}
    expire_in: 6 hours
# XXX: Do we always want to clone the subcomponent
#  rules:
#    - if: '$GEOSPHERE_SUBCOMP == ""'
#      when: always
#    - if: '$GEOSPHERE_SUBCOMP == $SUBCOMP_NAME'
#      when: always
#    - when: never

get_chart_grb:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-grb"
    SUBCOMP_CHART_DIR: "cspp-geo-grb"


deploy_prod_rabbit:
  extends: .helm_based_job
  stage: deploy rabbit
  script:
    - if [ -n "$CI_COMMIT_TAG" ]; then
        ns="geosphere";
      else
        ns="geosphere-test";
      fi
    # copy secret kubeconfig to the mounted (pwd) directory
    - cp $kubekorner_k3s_config .
    - kubeconfig=$(basename $kubekorner_k3s_config)
    # install third-party rabbitmq server
    - helm repo add bitnami https://charts.bitnami.com/bitnami
    - helm upgrade -v 2 --install --kubeconfig $kubeconfig --namespace $ns geosphere-rabbit bitnami/rabbitmq
  rules:
    - if: '$kubekorner_k3s_config == ""'
      when: never
    - when: on_success

deploy_g16_grb:
  environment:
    name: production
    url: http://geosphere.ssec.wisc.edu
  extends: .helm_based_job
  stage: deploy GRB
  script:
    - if [ -n "$CI_COMMIT_TAG" ]; then
        ns="geosphere";
      else
        ns="geosphere-test";
      fi
    - cd geosphere-grb/chart
    - source cspp-geo-grb/cibuild.env
    # copy secret kubeconfig to the mounted (pwd) directory
    - cp $kubekorner_k3s_config .
    - kubeconfig=$(basename $kubekorner_k3s_config)
    - echo "Deploying version $docker_tag to cluster namespace $ns"
    # copy extra values files to the local directory (where helm has access via docker mount)
    - cp ../../production/values-g16-grb.yaml .
    # namespace names are the same as domain names
    - helm upgrade -v 2 --install --kubeconfig $kubeconfig --namespace $ns --set grbNotifier.image.dockerTag=$docker_tag -f values-g16-grb.yaml cspp-geo-grb cspp-geo-grb/
  needs:
    - job: get_chart_grb
      artifacts: true
  rules:
    - if: '$kubekorner_k3s_config == ""'
      when: never
    - when: on_success
