stages:
    - install_dependencies
    - sonar
    - build
    - dockerize

variables:
    VERSION: "3.7.0"
    SONARQUBE_URL: https://sonarqube.lam.fr

install_dependencies:
    image: node:16-slim
    stage: install_dependencies
    cache: 
        key: ${CI_COMMIT_REF_SLUG}_client
        paths:
            - client/node_modules
        policy: pull-push
    script:
        - cd client
        - yarn install

sonar_scanner:
    image: sonarsource/sonar-scanner-cli:latest
    stage: sonar
    cache: 
        key: ${CI_COMMIT_REF_SLUG}_client
        paths:
            - client/node_modules
        policy: pull
    script:
        - cd client
        - sonar-scanner
            -Dsonar.projectKey=anis-client
            -Dsonar.sources=src
            -Dsonar.projectVersion=$VERSION 
            -Dsonar.host.url=$SONARQUBE_URL 
            -Dsonar.login=$SONAR_TOKEN_CLIENT
            -Dsonar.exclusions=**.spec.ts
            -Dsonar.typescript.lcov.reportPaths=./coverage/anis-next/lcov.info

build:
    image: node:16-slim
    stage: build
    cache: 
        key: ${CI_COMMIT_REF_SLUG}_client
        paths:
            - client/node_modules
        policy: pull
    script:
        - cd client
        - yarn global add @angular/cli@latest
        - ng build
    artifacts:
        paths:
            - client/dist

dockerize:
    image: docker:stable
    stage: dockerize
    cache: {}
    script:
        - cd client
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        - docker pull $CI_REGISTRY/anis/anis-next/client:latest || true
        - docker build --cache-from $CI_REGISTRY/anis/anis-next/client:latest -t $CI_REGISTRY/anis/anis-next/client:latest .
        - docker push $CI_REGISTRY/anis/anis-next/client:latest
