kind: pipeline
type: docker
name: Backend CI/CD

trigger:
  event:
    - push

steps:
  - name: deploy-database
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
      IMAGENAME: mongo:8.0.3-noble
      CONTAINERNAME: mongodb
      COMMAND: create
      OVERWRITE: true
      CODEFIRST_CLIENTDRONE_ENV_MONGO_INITDB_ROOT_USERNAME:
        from_secret: SECRET_MONGODB_USERNAME
      CODEFIRST_CLIENTDRONE_ENV_MONGO_INITDB_ROOT_PASSWORD:
        from_secret: SECRET_MONGODB_PASSWORD
      ADMINS: alixjeudi--lemoine,alexisferon,mathisframit,maxencejouannet

  - name: deploy-database-viewer
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
      IMAGENAME: mongo-express:latest
      CONTAINERNAME: dbviewer
      COMMAND: create
      OVERWRITE: true
      CODEFIRST_CLIENTDRONE_ENV_ME_CONFIG_SITE_BASEURL: "/"
      CODEFIRST_CLIENTDRONE_ENV_ME_CONFIG_MONGODB_URL:
        from_secret: SECRET_MONGODB_FULLURL
      CODEFIRST_CLIENTDRONE_ENV_ME_CONFIG_BASICAUTH_USERNAME:
        from_secret: SECRET_MONGODB_USERNAME
      CODEFIRST_CLIENTDRONE_ENV_ME_CONFIG_BASICAUTH_PASSWORD:
        from_secret: SECRET_MONGODB_PASSWORD
      ADMINS: alixjeudi--lemoine,alexisferon,mathisframit,maxencejouannet
    depends_on: [deploy-database]

  - name: code-analysis
    image: hub.codefirst.iut.uca.fr/alix.jeudi--lemoine/codefirst-dronesonarplugin-python312:latest
    environment:
      MONGODB_URL: "mongodb://SAE3A_MemoryMap-mongodb:27017/"
      MONGODB_USERNAME:
        from_secret: SECRET_MONGODB_USERNAME
      MONGODB_PASSWORD:
        from_secret: SECRET_MONGODB_PASSWORD
      MONGODB_DATABASE: memorymap_tests
    commands:
      - apt-get update && apt-get install -y libmagic1
      - pip install -r tests/requirements.txt
      - pytest --cov=app --cov-config=.coveragerc
      - /opt/sonar-scanner/bin/sonar-scanner -Dsonar.login=$PLUGIN_SONAR_TOKEN -Dsonar.projectKey=SAE3A_MemoryMap-api -Dsonar.coverage.exclusions="tests/**" -Dsonar.python.version=3 -Dsonar.python.coverage.reportPaths="coverage.xml"
    settings:
      sonar_token:
        from_secret: SECRET_SONAR_TOKEN
    depends_on: [deploy-database]

  - name: docker-build-and-push
    image: plugins/docker
    settings:
      dockerfile: docker/Dockerfile
      context: .
      registry: hub.codefirst.iut.uca.fr
      mirror: https://proxy.iut.uca.fr:8443
      repo: hub.codefirst.iut.uca.fr/alix.jeudi--lemoine/memorymap_api
      username:
        from_secret: SECRET_REGISTRY_USERNAME
      password:
        from_secret: SECRET_REGISTRY_PASSWORD
    depends_on: [code-analysis]

  - name: deploy-api
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
      IMAGENAME: hub.codefirst.iut.uca.fr/alix.jeudi--lemoine/memorymap_api:latest
      CONTAINERNAME: api
      COMMAND: create
      OVERWRITE: true
      CODEFIRST_CLIENTDRONE_ENV_MONGODB_URL: "mongodb://SAE3A_MemoryMap-mongodb:27017/"
      CODEFIRST_CLIENTDRONE_ENV_MONGODB_USERNAME:
        from_secret: SECRET_MONGODB_USERNAME
      CODEFIRST_CLIENTDRONE_ENV_MONGODB_PASSWORD:
        from_secret: SECRET_MONGODB_PASSWORD
      CODEFIRST_CLIENTDRONE_ENV_MONGODB_DATABASE:
        from_secret: SECRET_MONGODB_DATABASE
      CODEFIRST_CLIENTDRONE_ENV_JWT_SECRET_KEY:
        from_secret: SECRET_JWT_SECRET_KEY
      CODEFIRST_CLIENTDRONE_ENV_JWT_ALGORITHM:
        from_secret: SECRET_JWT_ALGORITHM
      CODEFIRST_CLIENTDRONE_ENV_JWT_ACCESS_TOKEN_EXPIRE_MINUTES:
        from_secret: SECRET_JWT_ACCESS_TOKEN_EXPIRE_MINUTES
      ADMINS: alixjeudi--lemoine,alexisferon,mathisframit,maxencejouannet
    depends_on: [docker-build-and-push]
