image: docker:git

services:
  - docker:dind

stages:
  - test
  - sonar-analysis
  - build
  - deploy

build:
  image: docker:18.09
  services:
    - docker:18.09-dind
  only:
    - dev
    - rec
    - master
  stage: build
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"

deploy_dev:
  stage: deploy
  tags:
    - deploy
  only:
    - dev
  script:
    - cd /home/mps/ram
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker-compose pull service-ram
    - docker-compose up -d service-ram
    - docker system prune -a -f

test:
  stage: test
  image: node:14.15.4
  script:
    - npm i
    - npm run test:cov
  coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml
  only:
    - dev
    - merge_requests

code_analysis:
  image: skilldlabs/sonar-scanner:4.0.0
  services:
    - docker:18.09-dind
  stage: sonar-analysis
  only:
    - dev
  before_script:
    - export NODE_PATH=$NODE_PATH:`npm root -g`
    - npm install -g typescript
  script:
    - >
      sonar-scanner
      -Dsonar.projectName=${SONAR_PROJECT_KEY}
      -Dsonar.projectVersion=1.0
      -Dsonar.sourceEncoding=UTF-8
      -Dsonar.projectBaseDir=.
      -Dsonar.host.url=${SONAR_URL}
      -Dsonar.projectKey=${SONAR_PROJECT_KEY}
      -Dsonar.login=${SONAR_TOKEN}

mr:
  image: docker:18.09
  services:
    - docker:18.09-dind
  stage: build
  only:
    - merge_requests
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build .
