variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ''

stages:
  - build
  - deploy

build:
  image: docker:git
  services:
    - docker:dind
  stage: build
  tags:
    - build-push-to-registry
  only:
    - merge_requests
    - master
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t registry.forge.grandlyon.com/web-et-numerique/factory/llle_project/self-data-technical-doc .
    - docker push registry.forge.grandlyon.com/web-et-numerique/factory/llle_project/self-data-technical-doc

deploy_rec:
  stage: deploy
  tags:
    - ns-cpd-r01-syn
  only:
    - merge_requests
  needs:
    - build
  environment:
    name: Recette - Self-Data Technical Docs
    url: https://doc-self-data-rec.apps.grandlyon.com/
  before_script:
    - NAMESPACE=ns-cpd-r01-syn
    - echo $READ_REGISTRY_TOKEN
    - echo $KUBE_CONFIG_CPD_REC
    - export KUBECONFIG=$KUBE_CONFIG_CPD_REC
  script:
    - find k8s/ -name '*.yml' -exec sed -i "s/{{NAMESPACE}}/$NAMESPACE/g" {} \;
    - sed -i "s/{{HOSTNAME}}/doc-self-data-rec.apps.grandlyon.com/g" ./k8s/route.yml
    - CA=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_CERT_CA);
    - CRT=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_CERT_CRT);
    - KEY=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_TLS_PRIVATE_KEY);
    - sed -i -e "s|#CA#|$CA|" -e "s|#CRT#|$CRT|" -e "s|#KEY#|$KEY|" ./k8s/route.yml
    - oc create secret -n $NAMESPACE docker-registry forge-secret --docker-server=$CI_REGISTRY --docker-username=forge-secret --docker-password=$READ_REGISTRY_TOKEN --dry-run=client -o yaml | oc apply -f -
    - oc apply -f k8s/deployment.yml
    - oc apply -f k8s/service.yml
    - oc apply -f k8s/route.yml
    - oc delete pod -l app=self-data-doc

deploy_prod:
  stage: deploy
  tags:
    - ns-cpd-p01-syn
  only:
    - master
  needs:
    - build
  environment:
    name: Self-Data Technical Docs
    url: https://doc-self-data.apps.grandlyon.com/
  before_script:
    - NAMESPACE=ns-cpd-p01-syn
    - echo $READ_REGISTRY_TOKEN
    - echo $KUBE_CONFIG_CPD_PROD
    - export KUBECONFIG=$KUBE_CONFIG_CPD_PROD
  script:
    - find k8s/ -name '*.yml' -exec sed -i "s/{{NAMESPACE}}/$NAMESPACE/g" {} \;
    - sed -i "s/{{HOSTNAME}}/doc-self-data-rec.apps.grandlyon.com/g" ./k8s/route.yml
    - CA=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_CERT_CA);
    - CRT=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_CERT_CRT);
    - KEY=$(awk 'NF {sub(/\r/, ""); printf "%s\\\\r\\\\n",$0;}' $APPS_TLS_PRIVATE_KEY);
    - sed -i -e "s|#CA#|$CA|" -e "s|#CRT#|$CRT|" -e "s|#KEY#|$KEY|" ./k8s/route.yml
    - oc create secret -n $NAMESPACE docker-registry forge-secret --docker-server=$CI_REGISTRY --docker-username=forge-secret --docker-password=$READ_REGISTRY_TOKEN --dry-run=client -o yaml | oc apply -f -
    - oc apply -f k8s/deployment.yml
    - oc apply -f k8s/service.yml
    - oc apply -f k8s/route.yml
    - oc delete pod -l app=self-data-doc
