image: docker:git

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ''

stages:
  - test
  - build
  - build_release

build:
  stage: build
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - dev
    - master
    
build_release:
  stage: build
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"  .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
  only:
    - tags
    
build_stack:
  stage: build
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t "$CI_REGISTRY_IMAGE/cozy-env:latest"  ./docker/
    - docker push $CI_REGISTRY_IMAGE/cozy-env:latest
  only:
    - master
  when: manual

mr_sonarqube:
  stage: test
  only:
    - merge_requests
  image: skilldlabs/sonar-scanner:4.0.0
  script:
    - >
      sonar-scanner
      -Dsonar.projectName="ecolyo"
      -Dsonar.projectVersion=1.0
      -Dsonar.sourceEncoding=UTF-8
      -Dsonar.projectBaseDir=.
      -Dsonar.host.url=${SONAR_URL}
      -Dsonar.projectKey=${CI_PROJECT_PATH_SLUG}
      -Dsonar.login=${SONAR_TOKEN}

sonarqube:
  stage: test
  only:
    - dev
  image: skilldlabs/sonar-scanner:4.0.0
  script:
    - >
      sonar-scanner
      -Dsonar.projectName="ecolyo"
      -Dsonar.projectVersion=1.0
      -Dsonar.sourceEncoding=UTF-8
      -Dsonar.projectBaseDir=.
      -Dsonar.host.url=${SONAR_URL}
      -Dsonar.projectKey=${CI_PROJECT_PATH_SLUG}
      -Dsonar.login=${SONAR_TOKEN}
