kind: pipeline
type: docker
name: API
 
trigger:
  branch:
  - WebAPI
  event:
    - push

steps:
  - name: build
    image: mcr.microsoft.com/dotnet/sdk:8.0
    commands:
      - cd Project/EntityFramework/
      - dotnet restore webAPI.sln
      - dotnet build webAPI.sln -c Release --no-restore
      - dotnet publish webAPI.sln -c Release --no-restore -o $CI_PROJECT_DIR/build/release

  - name: tests
    image: mcr.microsoft.com/dotnet/sdk:8.0
    commands:
      -  cd Project/EntityFramework/
      - dotnet restore webAPI.sln
      - dotnet test webAPI.sln --no-restore
    depends_on: [build]

  - name: code-analysis
    image: hub.codefirst.iut.uca.fr/marc.chevaldonne/codefirst-dronesonarplugin-dotnet8
    environment:
      project_key: SAE_2A_Anglais_Blazor
      sonar_host: https://codefirst.iut.uca.fr/sonar/
      sonar_token:
        from_secret: SECRET_SONAR_LOGIN
    commands:
      - cd Project/EntityFramework/
      - dotnet restore webAPI.sln
      - dotnet sonarscanner begin /k:$${project_key} /d:sonar.host.url=$${sonar_host} /d:sonar.login=$${sonar_token} /d:sonar.coverage.exclusions="TU/**, TU_EF/**, TestConsole/**, TestConsole_API/**" /d:sonar.cpd.exclusions="Tests/**, StubbedContextLib/**, StubAPI/**" /d:sonar.coverageReportPaths="coveragereport/SonarQube.xml"
      - dotnet build webAPI.sln -c Release --no-restore
      - dotnet test webAPI.sln --logger trx --no-restore /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --collect "XPlat Code Coverage"
      - reportgenerator -reports:"**/coverage.cobertura.xml" -reporttypes:SonarQube -targetdir:"coveragereport"
      - dotnet publish webAPI.sln -c Release --no-restore -o $CI_PROJECT_DIR/build/release
      - dotnet sonarscanner end /d:sonar.login=$${sonar_token}
    depends_on: [tests]

  - name: swagger
    image: mcr.microsoft.com/dotnet/sdk:8.0
    failure: ignore
    volumes:
      - name: docs
        path: /docs
    environment:
        CODEFIRST_CLIENTDRONE_ENV_DOTNET_ROLL_FORWARD: LatestMajor
        CODEFIRST_CLIENTDRONE_ENV_DOTNET_ROLL_FORWARD_TO_PRERELEASE: 1
    commands:
      - cd Project/EntityFramework
      - dotnet restore webAPI.sln
      - cd API
      - dotnet new tool-manifest
      - dotnet tool install -g --version 6.5.0 Swashbuckle.AspNetCore.Cli 
      - cd ../
      - dotnet build webAPI.sln -c Release --no-restore
      - dotnet publish webAPI.sln -c Release --no-restore -o $CI_PROJECT_DIR/build/release
      - export PATH="$PATH:/root/.dotnet/tools"
      - swagger tofile --output /docs/swagger.json API/bin/Release/net8.0/API.dll v1
    depends_on: [build,tests]

  - name: generate-and-deploy-docs
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-docdeployer
    failure: ignore
    volumes:
      - name: docs
        path: /docs
    commands:
      - /entrypoint.sh
    when:
      branch:
        - WebAPI
    depends_on: [ build ]

volumes:
- name: docs
  temp: {}

---

kind: pipeline
type: docker
name: API-CD

trigger:
  event:
    - push
steps: 

  - name: docker-build-and-push
    image: plugins/docker
    settings:
       dockerfile: Project/EntityFramework/API/Dockerfile
       context: Project/EntityFramework/
       registry: hub.codefirst.iut.uca.fr
       repo: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais
       username:
         from_secret: SECRET_REGISTRY_USERNAME
       password:
         from_secret: SECRET_REGISTRY_PASSWORD
    
  - name: deploy-container-mysql
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
        IMAGENAME: mariadb:10
        CONTAINERNAME: mysql
        COMMAND: create
        OVERWRITE: true
        PRIVATE: true
        CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD:
          from_secret: db_root_password
        CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE:
          from_secret: db_database
        CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER:
          from_secret: db_user
        CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD:
          from_secret: db_password
        ADMINS: antoinejourdain,patrickbrugiere,luciegoigoux,marcchevaldonne

         
  - name: deploy-container
    image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
    environment:
        IMAGENAME: hub.codefirst.iut.uca.fr/antoine.jourdain/sae_2a_anglais:latest
        CONTAINERNAME: api_container
        COMMAND: create
        OVERWRITE: true
        CODEFIRST_CLIENTDRONE_ENV_DB_SERVER:
          from_secret: db_server
        CODEFIRST_CLIENTDRONE_ENV_DB_USER:
          from_secret: db_user
        CODEFIRST_CLIENTDRONE_ENV_DB_PASSWORD:
          from_secret: db_password
        CODEFIRST_CLIENTDRONE_ENV_DB_DATABASE:
          from_secret: db_database
        ADMINS: antoinejourdain,patrickbrugiere,luciegoigoux,marcchevaldonne
    depends_on: [ deploy-container-mysql, docker-build-and-push ]