variables:
  ONLY:
    value: ""
    description: "List of components to build: common core calculs api-expositiondonneesentrees api-referentiel api-event-donneesentrees api-event-calculs"
  MODE:
    value: "build"
    options: 
      - "build"
      - "dependency-check"
    description: "Mode: build or dependency-check"
     
.maven-components: &maven-components
  matrix:
    - COMPONENT: [ api-expositiondonneesentrees, api-referentiel, api-event-donneesentrees, api-event-calculs ]

.libraries: &libraries
  matrix:
    - COMPONENT: [ common, calculs ]

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH =~ /doc.*/
      when: never
    - if: $CI_COMMIT_REF_NAME =~ /g4it.*/
      when: always
    - if: $CI_COMMIT_REF_NAME =~ /main|develop|\d+\.\d+\.\d+/
      when: always

clear cache maven:
  stage: .pre
  script:
    - rm -rf .m2/
  cache:
    paths:
      - .m2/

core:
  stage: .pre
  variables:
    COMPONENT: $COMPONENT
    MODE: $MODE
  trigger:
    include: "services/common/.gitlab-ci-library.yml"
    strategy: depend
  rules:
    - if: $COMPONENT =~ $ONLY
      when: always
    - if: $ONLY == ""
      changes:
        - services/$COMPONENT/**/*
  parallel:
    matrix:
      - COMPONENT: [ core ]


child-lib:
  stage: build
  variables:
    COMPONENT: $COMPONENT
    MODE: $MODE
    DISABLE_DEP_CHECK: "true"
    DISABLE_SONAR: "true"
  trigger:
    include: "services/common/.gitlab-ci-library.yml"
    strategy: depend
  rules:
    - if: $COMPONENT =~ $ONLY
      when: always
    - if: $ONLY == ""
      changes:
        - services/$COMPONENT/**/*
  parallel: *libraries

child-mvn:
  stage: deploy
  variables:
    COMPONENT: $COMPONENT
    MODE: $MODE
  trigger:
    include: "services/common/.gitlab-ci-maven.yml"
    strategy: depend
  rules:
    - if: $COMPONENT =~ $ONLY
      when: always
    - if: $ONLY == ""
      changes:
        - services/$COMPONENT/**/*
  parallel: *maven-components


#e2e:
#  stage: deploy
#  image: docker:latest
#  services:
#    - docker:dind
#  only:
#    - main
#    - develop
#    - tags
#  before_script:
#    - apk add --update curl && rm -rf /var/cache/apk/*
#  script:
#    - cd $CI_PROJECT_DIR/assets/docker-compose
#    - docker-compose up -d postgresdb
#    - docker-compose ps -a
#    - cd $CI_PROJECT_DIR/e2e
#    - cat .env
