stages:
    - install
    - test
    - sonar
    - build-coverage
    - build
    - deploy
    - deploy-coverage

variables:
    VERSION: "3.0"
    SONARQUBE_URL: https://sonarqube.lam.fr
    CONTAINER_IMAGE: portus.lam.fr/anis/anis-client
    COVERAGE_IMAGE: portus.lam.fr/anis/anis-client-coverage

install:
    image: node:11
    stage: install
    script:
        - yarn global add @angular/cli
        - yarn install
        - ng build --prod
    cache:
        paths:
            - dist
            - node_modules
        policy: push
    only:
        refs:
            - develop

test:
    image: portus.lam.fr/ci-tools/angular-testing:latest
    stage: test
    script:
        - ng test --no-watch
    cache:
        paths:
            - dist
            - node_modules
            - var
        policy: pull-push
    only:
        refs:
            - develop


sonar_scanner:
    image: portus.lam.fr/ci-tools/sonar-scanner:latest
    stage: sonar
    script:
        - sonar-scanner
         -Dsonar.projectKey=anis-client
         -Dsonar.sources=src
         -Dsonar.projectVersion=$VERSION
         -Dsonar.host.url=$SONARQUBE_URL
         -Dsonar.login=$SONAR_TOKEN
         -Dsonar.exclusions=**.spec.ts
         -Dsonar.typescript.lcov.reportPaths=./var/coverage/lcov.info
    cache:
        paths:
            - node_modules
            - var
        policy: pull
    only:
        refs:
            - develop

build-coverage:
    image: docker:stable
    stage: build-coverage
    script:
        - echo "FROM nginx" > var/Dockerfile
        - echo "COPY coverage /usr/share/nginx/html" >> var/Dockerfile
        - docker login -u fagneray -p $PORTUS_TOKEN portus.lam.fr
        - docker pull $COVERAGE_IMAGE:latest || true
        - docker build --cache-from $COVERAGE_IMAGE:latest -t $COVERAGE_IMAGE:latest var
        - docker push $COVERAGE_IMAGE:latest
    cache:
        paths:
            - var
        policy: pull
    only:
        refs:
            - develop

build:
    image: docker:stable
    stage: build
    script:
        - docker login -u fagneray -p $PORTUS_TOKEN portus.lam.fr
        - docker pull $CONTAINER_IMAGE:latest-dev || true
        - docker build --cache-from $CONTAINER_IMAGE:latest-dev -t $CONTAINER_IMAGE:latest-dev .
        - docker push $CONTAINER_IMAGE:latest-dev
    cache:
        paths:
            - dist
        policy: pull
    only:
        refs:
            - develop

deploy:
    image: alpine
    stage: deploy
    script:
        - apk add --update curl
        - curl -XPOST $DEV_WEBHOOK
    only:
        refs:
            - develop

deploy-coverage:
    image: alpine
    stage: deploy-coverage
    script:
        - apk add --update curl
        - curl -XPOST $COVERAGE_WEBHOOK
    only:
        refs:
            - develop