kind: pipeline
type: docker
name: default

trigger:
  event:
    - push

steps:
  - name: build
    image: node:20
    commands:
      - cd cryptide_project
      - npm install --legacy-peer-deps
      - CI=false npm run build

  - name: code-analysis
    image: node:20
    environment:
      SONAR_TOKEN:
        from_secret: SECRET_SONAR_LOGIN_CRYPTIDE
    commands:
      - export SONAR_SCANNER_VERSION=4.7.0.2747
      - export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux
      - curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip
      - unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
      - export PATH=$SONAR_SCANNER_HOME/bin:$PATH
      - export SONAR_SCANNER_OPTS="-server"
      - sonar-scanner -D sonar.projectKey=Cryptid -D sonar.sources=. -D sonar.host.url=https://codefirst.iut.uca.fr/sonar
    secrets: [SECRET_SONAR_LOGIN_CRYPTIDE]
    settings:
      sonar_host: https://codefirst.iut.uca.fr/sonar/
      sonar_token:
        from_secret: SECRET_SONAR_LOGIN_CRYPTIDE

  - name: deploy-container-mysql
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    build:
      context: .
    environment:
        IMAGENAME: mysql:latest
        CONTAINERNAME: mysql
        COMMAND: create
        OVERWRITE: true
        PRIVATE: false
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_ROOT_PASSWORD:    
          from_secret: MYSQL_ROOT_PASSWORD
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_DATABASE:
          from_secret: MYSQL_DATABASE
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_USER:
          from_secret: MYSQL_USER
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_PASSWORD:
          from_secret: MYSQL_PASSWORD
        ADMINS: thomaschazot2,pierreferreira,baptistemarcel

  - name: container-web
    image: plugins/docker
    settings:
      dockerfile: ./cryptide_project/Dockerfile
      context: ./cryptide_project
      registry: hub.codefirst.iut.uca.fr
      repo: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/web
      username:
        from_secret: SECRET_REGISTRY_USERNAME
      password:
        from_secret: SECRET_REGISTRY_PASSWORD
    #depends_on: [ build ]
    when:
      branch:
      - CI/CD


#container deployment
  - name: deploy-container-web
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
        IMAGENAME: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/web:latest
        CONTAINERNAME: website
        COMMAND: create
        OVERWRITE: true
        #PRIVATE: true
        CODEFIRST_CLIENTDRONE_ENV_BASEPATH:
          from_secret: "/containers/Crypteam-website"
        ADMINS: thomaschazot2,pierreferreira,baptistemarcel
    depends_on: [ container-web ]
    when:
      branch:
      - CI/CD

  - name: container-api
    image: plugins/docker
    settings:
      dockerfile: ./cryptide_project/server/api/Dockerfile
      context: ./cryptide_project/server/api
      registry: hub.codefirst.iut.uca.fr
      repo: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/api
      username:
        from_secret: SECRET_REGISTRY_USERNAME
      password:
        from_secret: SECRET_REGISTRY_PASSWORD
    #depends_on: [ build ]
    when:
      branch:
      - CI/CD
  
  - name: deploy-api-containers
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
        IMAGENAME: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/api:latest
        CONTAINERNAME: api
        COMMAND: create
        OVERWRITE: true
        #PRIVATE: true
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_HOST:
          from_secret: MYSQL_HOST
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_USER:
          from_secret: MYSQL_USER
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_PASSWORD:
          from_secret: MYSQL_PASSWORD
        CODEFIRST_CLIENTDRONE_ENV_MYSQL_DATABASE:
          from_secret: MYSQL_DATABASE
        ADMINS: thomaschazot2,pierreferreira,baptistemarcel
    depends_on: [ container-api ]
    when:
      branch:
      - CI/CD
  
#
  - name: container-server
    image: plugins/docker
    settings:
      dockerfile: ./cryptide_project/server/socket_io/Dockerfile
      context: ./cryptide_project
      registry: hub.codefirst.iut.uca.fr
      repo: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/server
      username:
        from_secret: SECRET_REGISTRY_USERNAME
      password:
        from_secret: SECRET_REGISTRY_PASSWORD
    #depends_on: [ build ]
    when:
      branch:
      - CI/CD


#container deployment
  - name: deploy-server-containers
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
        IMAGENAME: hub.codefirst.iut.uca.fr/thomas.chazot2/cryptide/server:latest
        CONTAINERNAME: server
        COMMAND: create
        OVERWRITE: true
        #PRIVATE: true
        ADMINS: thomaschazot2,pierreferreira,baptistemarcel
    depends_on: [ container-server ]
    when:
      branch:
      - CI/CD
