stages:
  - build
  - deploy
  - quality

default:
  services:
    - name: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:25-dind
      alias: docker
  before_script:
    - export IMAGE_TAG=$CI_COMMIT_REF_NAME
    - echo $IMAGE_TAG

variables:
  DEPENDENCY_PROXY: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/

build:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:25
  stage: build
  rules:
    # Always run the stage for the 'dev' branch
    - if: '$CI_COMMIT_REF_NAME == "dev"'
    # For Merge Requests, make the stage manual (and optional)
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: manual
      allow_failure: true
    # Otherwise, skip the stage
    - when: never    
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker login ${CI_DEPENDENCY_PROXY_SERVER} -u ${CI_DEPENDENCY_PROXY_USER} -p ${CI_DEPENDENCY_PROXY_PASSWORD}
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$IMAGE_TAG" --build-arg DEPENDENCY_PROXY="$DEPENDENCY_PROXY" .
    - docker push "$CI_REGISTRY_IMAGE:$IMAGE_TAG"

build-tag:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:25
  stage: build
  only:
    - tags
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker login ${CI_DEPENDENCY_PROXY_SERVER} -u ${CI_DEPENDENCY_PROXY_USER} -p ${CI_DEPENDENCY_PROXY_PASSWORD}
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$IMAGE_TAG" --build-arg DEPENDENCY_PROXY="$DEPENDENCY_PROXY" .
    - docker push "$CI_REGISTRY_IMAGE:$IMAGE_TAG"

build-storybook:
  variables:
    DOCKER_TLS_CERTDIR: ''
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/docker:25
  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 login ${CI_DEPENDENCY_PROXY_SERVER} -u ${CI_DEPENDENCY_PROXY_USER} -p ${CI_DEPENDENCY_PROXY_PASSWORD}
    - docker build --pull -t "$CI_REGISTRY_IMAGE/storybook:$IMAGE_TAG" -f .storybook/Dockerfile --build-arg DEPENDENCY_PROXY="$DEPENDENCY_PROXY" .
    - docker push "$CI_REGISTRY_IMAGE/storybook:$IMAGE_TAG"

sonarqube:
  stage: quality
  only:
    - dev
  needs: []
  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
  needs: []
  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:
  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 ..
    - if [ "$CI_ENVIRONMENT_NAME" != "dev" ]; then rm k8s/env/90-res-storybook.yml; fi
    - ls k8s/env/
    - oc whoami
    - oc apply -f k8s/env/
    - if [ "$CI_ENVIRONMENT_NAME" == "dev" ]; then oc delete pod -l app=res-client; fi
    - if [ "$CI_ENVIRONMENT_NAME" == "dev" ]; then oc delete pod -l app=res-storybook; fi
  tags:
    - ns-res-$NAMESPACE_ENV-syn

deploy-10-dev:
  stage: deploy
  inherit:
    default: [before_script]
  extends: .deploy
  environment:
    name: dev
  variables:
    NAMESPACE_ENV: "d01"
  rules:
    # Always run the stage for the 'dev' branch
    - if: '$CI_COMMIT_REF_NAME == "dev"'
    # For Merge Requests, make the stage manual (and optional)
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: manual
      allow_failure: true
    # Otherwise, skip the stage
    - when: never    

deploy-20-rec:
  stage: deploy
  inherit:
    default: [before_script]
  extends: .deploy
  environment:
    name: rec
  variables:
    NAMESPACE_ENV: "r01"
  when: manual
  only:
    - tags

deploy-30-pro:
  stage: deploy
  inherit:
    default: [before_script]
  extends: .deploy
  environment:
    name: pro
  variables:
    NAMESPACE_ENV: "p01"
  when: manual
  only:
    - tags
