default:
  services:
    - name: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:20.10.9-dind
      alias: docker
  tags:
    - build-push-to-registry

variables:
  DEPENDENCY_PROXY: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/

stages:
  - quality
  - build
  - build-push
  - deploy

build-test:
  stage: build
  image: node:14.20-alpine
  before_script:
    - apk add git
    - apk add bash
  script:
    - yarn
    - yarn build
  only:
    - dev
    - merge_requests

build-push:
  image: docker:18.09
  services:
    - docker:18.09-dind
  stage: build-push
  only:
    - master
    - dev
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" --build-arg conf=prod .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"

sonarqube:
  stage: quality
  only:
    - merge_requests
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/sonarsource/sonar-scanner-cli:4
  variables:
    SONAR_USER_HOME: '${CI_PROJECT_DIR}/.sonar' # Defines the location of the analysis task cache
    GIT_DEPTH: '0' # T
  cache:
    key: '${CI_JOB_NAME}'
    paths:
      - .sonar/cache
  script:
    - >
      sonar-scanner
      -Dsonar.projectName="ecolyo-backoffice-front"
      -Dsonar.projectVersion=1.0
      -Dsonar.sourceEncoding=UTF-8
      -Dsonar.projectBaseDir=.
      -Dsonar.host.url=${SONAR_URL}
      -Dsonar.projectKey=${CI_PROJECT_PATH_SLUG}
      -Dsonar.login=${SONAR_TOKEN}
      -Dsonar.cpd.exclusions=tests/**,src/**/*.spec.ts*
      -Dsonar.qualitygate.wait=true

deploy_rec:
  stage: deploy
  before_script:
    - NAMESPACE=ns-ecolyo-bo-r01-syn
    - sed -i "s/{{IMAGE_TAG}}/dev/" ./k8s/deployments/ecolyo-agent-client-deployment.yml
    - export KUBECONFIG=$KUBE_CONFIG_REC
  script:
    - find k8s/ -name '*.yml' -exec sed -i "s/{{NS}}/$NAMESPACE/g" {} \;
    - sed -i "s/{{HOSTNAME}}/ecolyo-agent-rec.apps.grandlyon.com/g" ./k8s/routes/ecolyo-agent-client-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/routes/ecolyo-agent-client-route.yml
    - oc create secret -n $NAMESPACE docker-registry llle-project --docker-server=$CI_REGISTRY --docker-username=llle-project --docker-password=$TOKEN_PULL_IMAGE --dry-run=client -o yaml | oc apply -f -

    - oc apply -f k8s/deployments
    - oc apply -f k8s/services
    - oc apply -f k8s/routes
  tags:
    - ns-ecolyo-bo-r01-syn
  only:
    - dev

deploy_prod:
  stage: deploy
  before_script:
    - NAMESPACE=ns-ecolyo-bo-p01-syn
    - sed -i "s/{{IMAGE_TAG}}/master/" ./k8s/deployments/ecolyo-agent-client-deployment.yml
    - export KUBECONFIG=$KUBE_CONFIG_PROD
  script:
    - find k8s/ -name '*.yml' -exec sed -i "s/{{NS}}/$NAMESPACE/g" {} \;
    - sed -i "s/{{HOSTNAME}}/ecolyo-agent.apps.grandlyon.com/g" ./k8s/routes/ecolyo-agent-client-route.yml

    - oc create secret -n $NAMESPACE docker-registry llle-project --docker-server=$CI_REGISTRY --docker-username=llle-project --docker-password=$TOKEN_PULL_IMAGE --dry-run=client -o yaml | oc apply -f -

    - oc apply -f k8s/deployments
    - oc apply -f k8s/services
    - oc apply -f k8s/routes
  tags:
    - ns-ecolyo-bo-p01-syn
  only:
    - master
