stages:
  - build
  - quality
  - deploy

default:
  services:
    - name: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:18.09-dind
      alias: docker

variables:
  DEPENDENCY_PROXY: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/

build_branch:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  only:
    - merge_requests
    - V2.0
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:18.09
  stage: build
  except:
    - master
    - recette
    - dev
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE/feat:$CI_COMMIT_REF_SLUG" --build-arg conf=prod .
    - docker push "$CI_REGISTRY_IMAGE/feat:$CI_COMMIT_REF_SLUG"

build:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:18.09
  stage: build
  only:
    - master
    - recette
  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-release:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:18.09
  stage: build
  only:
    - tags
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE/tags:$CI_COMMIT_TAG" --build-arg conf=prod .
    - docker push "$CI_REGISTRY_IMAGE/tags:$CI_COMMIT_TAG"

build_dev:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:18.09
  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"

deploy_dev:
  stage: deploy
  tags:
    - deploy
  only:
    # - dev
    - V2.0
  script:
    - cd /home/mps/ram
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker-compose pull web-app
    - docker-compose up -d web-app
    - docker system prune -a -f
  environment:
    name: dev
    url: https://resin-dev.grandlyon.com

deploy_rec:
  stage: deploy
  tags:
    - deploy
  only:
    - recette
  script:
    - cd /home/mps/ram
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker-compose pull web-app-rec
    - docker-compose up -d web-app-rec
    - docker system prune -a -f
  when: manual
  environment:
    name: rec
    url: https://resin-rec.grandlyon.com

sonarqube:
  stage: quality
  only:
    - dev
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/sonarsource/sonar-scanner-cli:4
  variables:
    SONAR_USER_HOME: '${CI_PROJECT_DIR}/.sonar' # Defines the location of the analysis task cache
    GIT_DEPTH: '0' # T
  cache:
    key: '${CI_JOB_NAME}'
    paths:
      - .sonar/cache
  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}
      -Dsonar.qualitygate.wait=true

sonarqube-mr:
  stage: quality
  only:
    - merge_requests
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/sonarsource/sonar-scanner-cli:4
  variables:
    SONAR_USER_HOME: '${CI_PROJECT_DIR}/.sonar' # Defines the location of the analysis task cache
    GIT_DEPTH: '0' # T
  cache:
    key: '${CI_JOB_NAME}'
    paths:
      - .sonar/cache
  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_MR_TOKEN}
      -Dsonar.qualitygate.wait=true
