stages:
  - test
  - build
  - publish
  - create-vars
  - trigger-build-ee

include:
  - project: mattermost/ci/mattermost-server
    ref: master
    file: private.yml

variables:
  BUILD: "yes"
  IMAGE_BUILD_SERVER: $CI_REGISTRY/mattermost/ci/images/mattermost-build-server:20230118_golang-1.19.5
  IMAGE_BUILD_DOCKER: $CI_REGISTRY/mattermost/ci/images/mattermost-build-docker:19.03.14-1
  IMAGE_DIND: $CI_REGISTRY/mattermost/ci/images/docker-dind:19.03.14-1

empty:
  stage: create-vars
  script:
    - echo "empty"

.lint: # Commenting this job, because it is run in CircleCI
  image: $CI_REGISTRY/mattermost/ci/images/golangci-lint:v1.39.0-1
  stage: test
  script:
    - make config-reset
    - make check-style #todo MM_VET_OPENSPEC_PATH='$CI_PROJECT_DIR/mattermost-api-reference/v4/html/static/mattermost-openapi-v4.yaml'
  timeout: 60 mins
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'

.layers: # Commenting this job, because it is run in CircleCI
  image: $IMAGE_BUILD_SERVER
  stage: test
  script:
    - make store-layers
    - if [[ -n $(git status --porcelain) ]]; then echo "Please update the store layers using make store-layers"; exit 1; fi
    - git reset --hard
    - make app-layers
    - if [[ -n $(git status --porcelain) ]]; then echo "Please update the app layers using make app-layers"; exit 1; fi
    - git reset --hard
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'

.test-mysql: # Commenting this job, because it is run in CircleCI
  stage: test
  image: $IMAGE_BUILD_DOCKER
  services:
    - name: $IMAGE_DIND
      alias: docker
  variables:
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
    DOCKER_DRIVER: overlay2
    DOCKER_CONTENT_TRUST: 0
    DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE: ""
  before_script:
    - docker login ${CI_REGISTRY} --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD}
  script:
    - export COMPOSE_PROJECT_NAME="${CI_PIPELINE_IID}mysql"
    - export RACE_MODE=""
    - time cat .gitlab-ci/scripts/test/mysql.sh | /bin/bash
  artifacts:
    when: always
    reports:
      junit: report.xml
    paths:
      - build/logs
  timeout: 2 hours
  tags:
    - k8s-memory
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'

.test-postgres: # Commenting this job, because it is run in CircleCI
  stage: test
  image: $IMAGE_BUILD_DOCKER
  services:
    - name: $IMAGE_DIND
      alias: docker
  variables:
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
    DOCKER_DRIVER: overlay2
    DOCKER_CONTENT_TRUST: 0
    DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE: ""
  before_script:
    - docker login ${CI_REGISTRY} --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD}
  script:
    - export COMPOSE_PROJECT_NAME="${CI_PIPELINE_IID}postgres"
    - export RACE_MODE=""
    - time cat .gitlab-ci/scripts/test/postgres.sh | /bin/bash
  artifacts:
    when: always
    reports:
      junit: report.xml
    paths:
      - build/logs
  timeout: 2 hours
  tags:
    - k8s-memory
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'

test-schema-mysql:
  stage: test
  image: $IMAGE_BUILD_DOCKER
  services:
    - name: $IMAGE_DIND
      alias: docker
  variables:
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
    DOCKER_DRIVER: overlay2
    DOCKER_CONTENT_TRUST: 0
    DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE: ""
  before_script:
    - docker login ${CI_REGISTRY} --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD}
  script:
    - export COMPOSE_PROJECT_NAME="${CI_PIPELINE_IID}schemamysql"
    - time cat .gitlab-ci/scripts/test-schema/mysql.sh | /bin/bash
  artifacts:
    paths:
      - build/logs
  tags:
    - k8s-memory
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'
    - if: '$CI_COMMIT_REF_NAME == "cloud"'
    - if: '$CI_COMMIT_REF_NAME =~ /^release-/'

test-schema-postgres:
  stage: test
  image: $IMAGE_BUILD_DOCKER
  services:
    - name: $IMAGE_DIND
      alias: docker
  variables:
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
    DOCKER_DRIVER: overlay2
    DOCKER_CONTENT_TRUST: 0
    DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE: ""
  before_script:
    - docker login ${CI_REGISTRY} --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD}
  script:
    - export COMPOSE_PROJECT_NAME="${CI_PIPELINE_IID}schemapostgres"
    - time cat .gitlab-ci/scripts/test-schema/postgres.sh | /bin/bash
  artifacts:
    paths:
      - build/logs
  tags:
    - k8s-memory
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'
    - if: '$CI_COMMIT_REF_NAME == "cloud"'
    - if: '$CI_COMMIT_REF_NAME =~ /^release-/'
