#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
#  - 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

Backend:
  stage: build  
  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 git://gitlab.labranet.jamk.fi/conduitmiau/te-backend/
      --dockerfile Dockerfile
      --destination $CI_REGISTRY_IMAGE:backend-$CI_COMMIT_SHORT_SHA

Frontend:
  stage: build  
  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 git://gitlab.labranet.jamk.fi/conduitmiau/te-frontend/
      --dockerfile Dockerfile
      --destination $CI_REGISTRY_IMAGE:frontend-$CI_COMMIT_SHORT_SHA

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 https://gitlab.labranet.jamk.fi/conduitmiau/te-backend/-/blob/main/te-backend-service.yaml
         - kubectl apply -f https://gitlab.labranet.jamk.fi/conduitmiau/te-backend/-/blob/main/te-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 https://gitlab.labranet.jamk.fi/conduitmiau/te-frontend/-/blob/main/te-front-service.yaml
         - kubectl apply -f https://gitlab.labranet.jamk.fi/conduitmiau/te-frontend/-/blob/main/te-front-deployment.yaml
         - kubectl -n default rollout restart deployment front 
