image: docker:git

services:
  - docker:dind

stages:
  - sonar-analysis
  - build

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

build_dev:
  image: docker:18.09
  services:
    - docker:18.09-dind
  stage: build
  only:
    - dev
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE:dev" --build-arg conf=dev .
    - docker push "$CI_REGISTRY_IMAGE:dev"

code_analysis:
  image: skilldlabs/sonar-scanner:3.4.0
  services:
    - docker:18.09-dind
  stage: sonar-analysis
  only:
    - dev
  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}
