stages:
  - get chart
  - build prereqs
  - test
  - create storage
  - deploy infrastructure
  - deploy GRB
  - deploy G2G
  - deploy tile gen
  - deploy WMS
  - deploy WMTS
  - deploy Client

variables:
  DOCKER_TLS_CERTDIR: ""
  DOCKER_HOST: "tcp://localhost:2375"
  DOCKER_DRIVER: overlay2
  HELM_EXPERIMENTAL_OCI: "1"
  CICHART_IMAGE: "$CI_REGISTRY_IMAGE/cichart:latest"
  GEOSPHERE_TAG_REGEX: "^r[0-9]+_[0-9]+"
  GEOSPHERE_TEST_TAG_REGEX: "^test-r[0-9]+_[0-9]+"
  GCP_TAG_REGEX: "^gcp-r[0-9]+_[0-9]+"
#  TESTS_BRANCH_REGEX: "^master"
  # only run tests for branches in the main repository
  TESTS_PROJECT_NAMESPACE: "cspp_geo/geosphere"
  GEOSPHERE_NAMESPACE: "geosphere"
  GEOSPHERE_TEST_NAMESPACE: "geosphere-test"
  GCP_NAMESPACE: "default"
  TESTS_NAMESPACE: "geosphere-ci-tests"


include:
  - local: "/helpers/build_image.yaml"
  - local: "/helpers/docker_base.yaml"
  - local: "/helpers/helm_base.yaml"
  - local: "/helpers/get_chart.yaml"
  - local: "/ci_tests/gitlab-ci.yaml"
  - local: "/ci_geosphere/gitlab-ci.yaml"
  - local: "/ci_geosphere-test/gitlab-ci.yaml"
  - local: "/ci_gcp/gitlab-ci.yaml"


build ci:
  stage: .pre
  extends: .docker_based_job
  script:
    - cd cichart
    - docker build --tag $CICHART_IMAGE .
    - docker push $CICHART_IMAGE
  rules:
    # no need to build if another project triggered us
    - if: $CI_PIPELINE_SOURCE == "pipeline"
      when: never
    # no need to build with tags or non-master...probably
    - if: '$CI_COMMIT_BRANCH != "master"'
      when: never
    - 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:
    # no need to build if another project triggered us
    - if: $CI_PIPELINE_SOURCE == "pipeline"
      when: never
    # no need to build with tags or non-master...probably
    - if: '$CI_COMMIT_BRANCH != "master"'
      when: never
    - changes:
        - sidecars/cspp-geo-rabbit-init/Dockerfile
        - sidecars/cspp-geo-rabbit-init/declare_exchange.py
      when: always
    - if: $BUILD_PREREQS
      when: always

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

get_chart_geo2grid:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-geo2grid"
    SUBCOMP_CHART_DIR: "cspp-geo-geo2grid"

get_chart_tilegen:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-tile-gen"
    SUBCOMP_CHART_DIR: "geosphere-tile-gen"

get_chart_mapserver:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-mapserver"
    SUBCOMP_CHART_DIR: "geosphere-mapserver"

get_chart_mapcache:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-mapcache"
    SUBCOMP_CHART_DIR: "geosphere-mapcache"

get_chart_client:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-client"
    SUBCOMP_CHART_DIR: "geosphere-client"

get_chart_client_test:
  extends: .get_chart_tmpl
  variables:
    SUBCOMP_REPOS: "geosphere-client"
    SUBCOMP_CHART_DIR: "geosphere-client"
    SUBCOMP_BRANCH: "develop"

.deploy_geo2grid:
#  environment:
#    name: geosphere-test
#    url: http://geosphere-test.ssec.wisc.edu
  variables:
    DEPLOY_SUFFIX: ""
    VALUES_DIR: ""
  extends: .helm_based_job
  stage: deploy G2G
  script:
    - ./helpers/deploy_geo2grid.sh "${VALUES_DIR}" "${DEPLOY_SUFFIX}"
  dependencies:
    - get_chart_geo2grid

.deploy_tile_gen:
#  environment:
#    name: geosphere-test
#    url: http://geosphere-test.ssec.wisc.edu
  variables:
    DEPLOY_SUFFIX: ""
    VALUES_DIR: ""
  extends: .helm_based_job
  stage: deploy tile gen
  script:
    - ./helpers/deploy_tile_gen.sh "${VALUES_DIR}" "${DEPLOY_SUFFIX}"
  dependencies:
    - get_chart_tilegen

.deploy_mapserver:
  variables:
    VALUES_DIR: ""
  extends: .helm_based_job
  stage: deploy WMS
  script:
    - ./helpers/deploy_mapserver.sh "${VALUES_DIR}"
  dependencies:
    - get_chart_mapserver

.deploy_mapcache:
  variables:
    VALUES_DIR: ""
  extends: .helm_based_job
  stage: deploy WMTS
  script:
    - ./helpers/deploy_mapcache.sh "${VALUES_DIR}"
  dependencies:
    - get_chart_mapcache

.deploy_client:
  variables:
    VALUES_DIR: ""
  extends: .helm_based_job
  stage: deploy Client
  script:
    - ./helpers/deploy_client.sh "${VALUES_DIR}"
  dependencies:
    - get_chart_client

