variables:
    DOCKER_HOST: "tcp://docker-dind.gitlab-suma:2375"
    AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS: "--network host"
    POSTGRES_ENABLED: "false"
    CODE_QUALITY_DISABLED: "true"
    CONTAINER_SCANNING_DISABLED: "true"
    DAST_DISABLED: "true"
    DEPENDENCY_SCANNING_DISABLED: "true"
    LICENSE_MANAGEMENT_DISABLED: "true"
    PERFORMANCE_DISABLED: "true"
    SAST_DISABLED: "true"
    TEST_DISABLED: "true"
    AUTO_DEVOPS_BUILD_IMAGE_FORWARDED_CI_VARIABLES: "AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,S3_HOST,S3_BUCKETNAME"

include:
  - template: Jobs/Build.gitlab-ci.yml
  - template: Jobs/Deploy.gitlab-ci.yml

.auto-deploy:
  image: "registry.gitlab.com/gitlab-org/cluster-integration/auto-deploy-image:v2.12.0"

stages:
  - integrationtest
  - build
  - deploy  # dummy stage to follow the template guidelines
  - review
  - dast
  - staging
  - canary
  - development
  - production
  - incremental rollout 10%
  - incremental rollout 25%
  - incremental rollout 50%
  - incremental rollout 100%
  - performance
#  - integrationtest
  - cleanup

build:
  services:

review:
  variables:
    HELM_UPGRADE_VALUES_FILE: .gitlab/review-apps-values.yaml
    ROLLOUT_RESOURCE_TYPE: deployment
  environment:
    name: review/$CI_COMMIT_REF_NAME
    on_stop: stop_review
    auto_stop_in: 2 days
  rules:
    - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
      when: never
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: never
    - if: '$CI_COMMIT_BRANCH == "development"'
      when: never
    - if: '$REVIEW_DISABLED'
      when: never
    - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'

stop_review:
  variables:
    GIT_STRATEGY: none
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  rules:
    - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
      when: never
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: never
    - if: '$CI_COMMIT_BRANCH == "development"'
      when: never
    - if: '$REVIEW_DISABLED'
      when: never
    - if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
      when: manual



.development: &development_template
  extends: .auto-deploy
  stage: development
  script:
    - auto-deploy check_kube_domain
    - auto-deploy download_chart
    - auto-deploy ensure_namespace
    - auto-deploy initialize_tiller
    - auto-deploy create_secret
    - auto-deploy deploy
    - auto-deploy delete canary
    - auto-deploy delete rollout
    - auto-deploy persist_environment_url
  variables:
    ADDITIONAL_HOSTS: "www.metager3.de,test.metager.de"
    HELM_UPGRADE_VALUES_FILE: .gitlab/development-values.yaml
    ROLLOUT_RESOURCE_TYPE: deployment
  environment:
    name: development
    url: https://metager3.de
  artifacts:
    paths: [environment_url.txt]

development:
  <<: *development_template
  rules:
    - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
      when: never
    - if: '$STAGING_ENABLED'
      when: never
    - if: '$CANARY_ENABLED'
      when: never
    - if: '$INCREMENTAL_ROLLOUT_ENABLED'
      when: never
    - if: '$INCREMENTAL_ROLLOUT_MODE'
      when: never
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'



production:
  variables:
    ADDITIONAL_HOSTS: "www.metager.de,metager.org,www.metager.org,metager.es,www.metager.es,klassik.metager.org"
    HELM_UPGRADE_VALUES_FILE: .gitlab/production-values.yaml
    ROLLOUT_RESOURCE_TYPE: deployment
  environment:
    url: https://metager.de
  rules:
    - if: '$CI_KUBERNETES_ACTIVE == null || $CI_KUBERNETES_ACTIVE == ""'
      when: never
    - if: '$STAGING_ENABLED'
      when: never
    - if: '$CANARY_ENABLED'
      when: never
    - if: '$INCREMENTAL_ROLLOUT_ENABLED'
      when: never
    - if: '$INCREMENTAL_ROLLOUT_MODE'
      when: never
    - if: '$CI_COMMIT_BRANCH == "master"'


integrationtest:
  stage: integrationtest
  environment: integrationtest
  image: 
    name: registry.metager.de/open-source/metager/1140-fix-ci-pipelines #$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
    entrypoint: ["/bin/sh"]
  script:
    # Install Dev Dependencies
    - echo $K8S_SECRET_env > .env
    - tail -f /dev/null
    - echo "WEBDRIVER_USER=\"$WEBDRIVER_KEY\"" >> .env
    - echo "WEBDRIVER_URL=\"$WEBDRIVER_URL\"" >> .env
    - echo "WEBDRIVER_KEY=\"$WEBDRIVER_USER\"" >> .env
    - php artisan key:generate
    - URL=$(cat environment_url.txt | tr -d '\n')
    - sed -i "s#^APP_URL=.*#APP_URL=$URL#g" .env
    - sed -i "s#^BRANCH_NAME=.*#BRANCH_NAME=$CI_COMMIT_REF_NAME#g" .env
    - sed -i "s#^COMMIT_NAME=.*#COMMIT_NAME=$CI_COMMIT_REF_SLUG#g" .env
    - php artisan dusk
  except:
    refs: 
      - master