stages:
  - build
  - quality
  - deploy

default:
  services:
    - name: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:24.0.2-dind
      alias: docker
  before_script:
    - export IMAGE_TAG=$CI_COMMIT_REF_NAME
    - if [ "$CI_COMMIT_REF_NAME" == "master" ]; then export IMAGE_TAG="stable"; fi
    - echo $IMAGE_TAG

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
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:24.0.2
  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:24.0.2
  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:24.0.2
  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:24.0.2
  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"

build-storybook:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:24.0.2
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == "dev"
      changes:
        - .gitlab-ci.yml
        - .storybook/**/*
        - src/app/shared/components/**/*
        - src/assets/scss
        - src/styles.scss
      allow_failure: true
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE/storybook:$IMAGE_TAG" -f .storybook/Dockerfile .
    - docker push "$CI_REGISTRY_IMAGE/storybook:$IMAGE_TAG"

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 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

.deploy:
  # Use extended image with envsubst installed
  image: registry.forge.grandlyon.com/openshift-as-code/reposit-gl/oc-client:4.10_extended
  script:
    - if [ "$CI_ENVIRONMENT_NAME" == "dev" ]; then export KUBECONFIG=$KUBECONFIG_DEV; fi
    - if [ "$CI_ENVIRONMENT_NAME" == "rec" ]; then export KUBECONFIG=$KUBECONFIG_REC; fi
    - if [ "$CI_ENVIRONMENT_NAME" == "pro" ]; then export KUBECONFIG=$KUBECONFIG_PRO; fi
    - export URL_ENV_SUFFIX="-$CI_ENVIRONMENT_NAME"
    - export URL_APPS_SUFFIX=".apps"
    - if [ "$CI_ENVIRONMENT_NAME" == "pro" ]; then export URL_ENV_SUFFIX=""; URL_APPS_SUFFIX=""; fi
    - echo ${URL_ENV_SUFFIX}${URL_APPS_SUFFIX}
    - mkdir -p k8s/env
    # Use envsubst to substitute env variables in all deployment/*.yml files
    - cd k8s ; for f in *.yml; do envsubst < $f > env/$f ; done ; cd ..
    - ls k8s/env/
    - oc whoami
    - oc apply -f k8s/env/
    - oc delete pod -l app=res-storybook
  tags:
    - ns-res-$NAMESPACE_ENV-syn

deploy_10_dev:
  stage: deploy
  inherit:
    default: [before_script]
    variables: false
  extends: .deploy
  environment:
    name: dev
  variables:
    NAMESPACE_ENV: "d01"
  only:
    - dev
