#mlöm
 
#  stages:
#  #   - build robot
#     - test

#  test:
#     stage: test
#     image: gitlab.labranet.jamk.fi:4567/conduitmiau/kubetest:robot
#     script:
#        - echo "huuhaa puuhaa"
#      - export ROBOT_TESTS=PATH_TO_TESTS_DIRECTORY/
#      - export OUTPUT_DIR=DIRECTORY_TO_SAVE_OUTPUT_TO
#      - run.sh
#    artifacts:
#      paths:
#        - DIRECTORY_TO_SAVE_OUTPUT_TO/


#  build robot:
#    stage: build robot

#    image:
#     name: gcr.io/kaniko-project/executor:debug
#     entrypoint: [""]

#    script:

#   #Build robot
   
#     - echo "Building robot"
#     - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
#     - /kaniko/executor --context $CI_PROJECT_DIR/robot-framework --dockerfile $CI_PROJECT_DIR/robot-framework/Dockerfile --destination $CI_REGISTRY_IMAGE:robot




# #include:
# #  - template: Dependency-Scanning.gitlab-ci.yml
# #  - template: Security/License-Scanning.gitlab-ci.yml
# #  - template: Security/Secret-Detection.gitlab-ci.yml
# #  - template: Security/SAST.gitlab-ci.yml
# #  - template: DAST.gitlab-ci.yml

stages:
  - build backend
  - build frontend
#  - test
  - deploy mongodb
  - deploy backend
  - deploy frontend
#  - dast

#variables:
#  DAST_WEBSITE: "http://vm3733.kaj.pouta.csc.fi:30100/"
#  DAST_FULL_SCAN_ENABLED: "true"
#  DAST_AUTH_URL: "http://vm3733.kaj.pouta.csc.fi:30100/login"
#  DAST_AUTH_USERNAME: “DAST”
#  DAST_AUTH_PASSWORD: “DAST”
#  SAST_GOSEC_LEVEL: 2

build backend:
  stage: build backend
  
  when: manual 

  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
 
  script:

  #Build Backend
   
    - echo "Building Backend"
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR/conduit-backend --dockerfile $CI_PROJECT_DIR/conduit-backend/Dockerfile --destination $CI_REGISTRY_IMAGE:backend

build frontend:
  stage: build frontend
  
  when: manual 

  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
 
  script:

  # Build Frontend

    - echo "Building Frontend"
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR/conduit-front --dockerfile $CI_PROJECT_DIR/conduit-front/Dockerfile --destination $CI_REGISTRY_IMAGE:front

deploy mongodb:

#    tags:
#      - example

    stage: deploy mongodb

    when: manual

#    script: echo "Deploying"
    image: bitnami/kubectl:latest
    script:
         
#         - kubectl apply -k $CI_PROJECT_DIR/conduit-db/kustomization.yaml

         - kubectl apply -f $CI_PROJECT_DIR/conduit-db/mongo-deployment.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-db/mongo-persistent-volume.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-db/mongo-pv-claim.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-db/mongo-service.yaml  
         - kubectl -n default rollout restart deployment mongo-deployment       

deploy backend:

#    tags:
#      - example

    stage: deploy backend

    when: manual

#    script: echo "Deploying"
    image: bitnami/kubectl:latest
    script:

         - kubectl apply -f $CI_PROJECT_DIR/conduit-backend/backend-configmap.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-backend/backend-service.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-backend/backend-deployment.yaml
         - kubectl -n default rollout restart deployment backend

deploy frontend:

#    tags:
#      - example

    stage: deploy frontend

    when: manual

#    script: echo "Deploying"
    image: bitnami/kubectl:latest
    script:      
         - kubectl apply -f $CI_PROJECT_DIR/conduit-front/front-ingress.yaml  
         - kubectl apply -f $CI_PROJECT_DIR/conduit-front/front-configmap.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-front/front-service.yaml
         - kubectl apply -f $CI_PROJECT_DIR/conduit-front/front-deployment.yaml
         - kubectl -n default rollout restart deployment front