stages:
  - deploy_ocf_staging
  - delete_ocf_staging
  - deploy_ocf_dev
  - delete_ocf_dev

variables:
  INGRESS: "10.43.32.232"
  NAMESPACE_DEV: "ocf-dev-$CI_JOB_USER"
  NAMESPACE_STAGING: "ocf-staging"
  DOMAIN_STAGING: staging.int
  DOAMIN_DEV: developer.int
  CI_JOB_TOKEN: $CI_JOB_TOKEN
  IMAGE_TAG_DEV: $CI_COMMIT_REF_SLUG
  IMAGE_TAG_STAGING: $CI_COMMIT_REF_SLUG

.staging_common: &staging_common
  only:
    - merge_requests
  except:
    variables:
      - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "staging"
  tags:
    - shell

.dev_common: &dev_common
  tags:
    - shell

deploy_ocf_staging:
  stage: deploy_ocf_staging
  <<: *staging_common
  environment:
    name: review/staging
    url: https://$NAMESPACE_STAGING.$DOMAIN_STAGING
    on_stop: delete_ocf_staging
    auto_stop_in: 3 day
#  rules:
#    - if: $CI_COMMIT_BRANCH == "main"
#      when: never
#    - if: $CI_COMMIT_BRANCH == "staging"
  script:
#    - echo "### git clone OCF repo ###" 
#    - git clone https://oauth2:${CI_JOB_TOKEN}@labs.etsi.org/rep/ocf/capif.git
    - echo "### install helm###"
    - curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    - chmod 700 get_helm.sh
    - ./get_helm.sh
    - helm version
    - echo "### install kubectl###" 
    - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    - chmod +x kubectl
    - sudo mv kubectl /usr/local/bin
    - kubectl version --output=yaml
    - echo "### setting kubeconfig###"
#    - echo $KUBECONFIG | base64 -d > ~/cluster.kubeconfig
#    - kubectl get nodes --kubeconfig ~/cluster.kubeconfig
#    - kubectl cluster-info --kubeconfig ~/cluster.kubeconfig #comment that when is working
#    - echo "### install yq###"
#    - sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
#    - sudo chmod a+x /usr/local/bin/yq
#    - yq --version
#    - yq e -i '.version = "$IMAGE_TAG_STAGING"' helm/capif/Chart.yaml
#    - yq e -i '.appVersion = "$IMAGE_TAG_STAGING"' helm/capif/Chart.yaml
#    - cat helm/capif/Chart.yaml
#    - echo "### download dependencies###"
#    - helm dependency build helm/capif
#    - echo "### updating capif###"
#    - helm uninstall -n $NAMESPACE_STAGING ocf --kubeconfig ~/cluster.kubeconfig || true
#    - helm upgrade --install -n $NAMESPACE_STAGING ocf helm/capif/ \
#      --set nginx.nginx.env.capifHostname=capif.$DOMAIN_STAGING \
#      --set ingress_ip.oneke="$INGRESS" --atomic \
#      --set monitoring.prometheus.enable="" \
#      --set monitoring.grafana.ingress.hosts[0].host="grafana.$DOMAIN_STAGING" \
#      --set monitoring.grafana.ingress.hosts[0].paths[0].path="/" \
#      --set monitoring.grafana.ingress.hosts[0].paths[0].pathType="Prefix" \
#      --wait --timeout=10m \
#      --create-NAMESPACE_STAGING --kubeconfig ~/cluster.kubeconfig


delete_ocf_staging:
  stage: delete_ocf_staging
  <<: *staging_common
  script:
    - echo "### deleting environment $NAMESPACE_STAGING###"
    - helm uninstall -n $NAMESPACE_STAGING ocf --kubeconfig ~/cluster.kubeconfig
  when: manual
  environment:
    name: review/staging
    action: stop

## dev ###
deploy_ocf_dev:
  stage: deploy_ocf_dev
  <<: *dev_common
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    url: https://$NAMESPACE_DEV.$DOMAIN_DEV
    on_stop: delete_ocf_dev
    auto_stop_in: 3 day
#  rules:
#    - if: $CI_COMMIT_BRANCH == "main"
#      when: never
#    - if: $CI_COMMIT_BRANCH == "staging"
#      when: never      
#    - if: $CI_COMMIT_BRANCH
  script:
#    - echo "### git clone OCF repo###" 
#    - git clone https://oauth2:${CI_JOB_TOKEN}@labs.etsi.org/rep/ocf/capif.git
    - echo "### install helm###"
    - curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    - chmod 700 get_helm.sh
    - ./get_helm.sh
    - helm version
    - echo "### install kubectl###" 
    - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    - chmod +x kubectl
    - sudo mv kubectl /usr/local/bin
    - kubectl version --output=yaml
    - echo "### setting kubeconfig###"
#    - echo $KUBECONFIG | base64 -d > ~/cluster.kubeconfig
#    - kubectl get nodes --kubeconfig ~/cluster.kubeconfig
#    - kubectl cluster-info --kubeconfig ~/cluster.kubeconfig #comment that when is working
#    - echo "### install yq###"
#    - sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
#    - sudo chmod a+x /usr/local/bin/yq
#    - yq --version
#    - yq e -i '.version = "$IMAGE_TAG_STAGING"' helm/capif/Chart.yaml
#    - yq e -i '.appVersion = "$IMAGE_TAG_STAGING"' helm/capif/Chart.yaml
#    - cat helm/capif/Chart.yaml
#    - echo "### download dependencies###"
#    - helm dependency build helm/capif
#    - echo "### updating capif###"
#    - helm uninstall -n $NAMESPACE_STAGING ocf --kubeconfig ~/cluster.kubeconfig || true
#    - helm upgrade --install -n $NAMESPACE_STAGING ocf helm/capif/ \
#      --set nginx.nginx.env.capifHostname=capif.$DOMAIN_STAGING \
#      --set ingress_ip.oneke="$INGRESS" --atomic \
#      --set monitoring.prometheus.enable="" \
#      --set monitoring.grafana.ingress.hosts[0].host="grafana.$DOMAIN_STAGING" \
#      --set monitoring.grafana.ingress.hosts[0].paths[0].path="/" \
#      --set monitoring.grafana.ingress.hosts[0].paths[0].pathType="Prefix" \
#      --wait --timeout=10m \
#      --create-NAMESPACE_STAGING --kubeconfig ~/cluster.kubeconfig