image: docker:latest

cache:
  paths:
    - ccache

variables:
  DOCKER_DRIVER: overlay2
# When running CI jobs, don't allow
# HW backed renderes, due to the
# variability it introduces.
#  RENDER_DEVICE: /dev/dri/renderD128

.docker-setup: &docker-setup
    - mkdir -p ccache
    - mkdir -p results
    - export DOCKER_IMAGE_DIGEST=$(cat results/docker_image_digest.txt)
    - echo $DOCKER_IMAGE_DIGEST
    - echo core > /proc/sys/kernel/core_pattern || true
    - echo 0 > /proc/sys/kernel/core_uses_pid || true
    - if [[ -n "$RENDER_DEVICE" ]]; then RD_CONFIG="--device=$RENDER_DEVICE -e RENDER_DEVICE=$RENDER_DEVICE"; fi
    - echo RD_CONFIG=$RD_CONFIG

stages:
  - build
  - sanity test
  - test

build docker image:
  stage: build
  services:
    - docker:dind
  before_script:
    - mkdir -p results
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - time docker pull $CI_REGISTRY_IMAGE:latest || true

    - docker build
             --build-arg KNOWN_GOOD_MESA=$MESA_COMMIT
             --build-arg MESA_REPO=$MESA_REPO
             -t $CI_REGISTRY_IMAGE
             --cache-from $CI_REGISTRY_IMAGE:latest ci

    - docker history $CI_REGISTRY_IMAGE:latest

    - time docker push $CI_REGISTRY_IMAGE:latest 2>&1 | tee results/docker_push_log.txt

    - "grep -o 'digest: sha256:[0-9a-f]\\+' results/docker_push_log.txt |
       cut -f 2 -d ' ' |
       tee results/docker_image_digest.txt"
  only:
    - branches
    - tags
    - merge_requests
    - schedules

  artifacts:
    when: always
    paths:
      - results/

make check:
  stage: sanity test
  services:
    - docker:dind
  before_script:
    *docker-setup
  script:
    - docker run
             --ulimit core=99999999999:99999999999
             $RD_CONFIG
             -v $PWD:/virglrenderer
             $CI_REGISTRY_IMAGE@$DOCKER_IMAGE_DIGEST
             bash -c "/virglrenderer/ci/run_tests.sh --make-check"
    - echo -e "\nThat's all folks\n"
  only:
    - branches
    - tags
    - merge_requests
    - schedules

  artifacts:
    when: always
    paths:
      - results/

.tests:
  stage: test
  services:
    - docker:dind
  before_script:
    *docker-setup
  script:
    - docker run
             --ulimit core=99999999999:99999999999
             $RD_CONFIG
             -v $PWD:/virglrenderer
             $CI_REGISTRY_IMAGE@$DOCKER_IMAGE_DIGEST
             bash -c "/virglrenderer/ci/run_tests.sh $TEST_SUITE"
    - echo -e "\nThat's all folks\n"
  only:
    - merge_requests
    - schedules

  artifacts:
    when: always
    paths:
      - results/

piglit - gl host:
  variables:
    TEST_SUITE: '--piglit-gl'
  extends: .tests

piglit - gles host:
  variables:
    TEST_SUITE: '--piglit-gles'
  extends: .tests

cts gl - gl host:
  variables:
    TEST_SUITE: '--deqp-gl-gl-tests'
  extends: .tests

cts gles2 - gl host:
  variables:
    TEST_SUITE: '--deqp-gl-gles2-tests'
  extends: .tests

cts gles3 - gl host:
  variables:
    TEST_SUITE: '--deqp-gl-gles3-tests'
  extends: .tests

cts gles31 - gl host:
  variables:
    TEST_SUITE: '--deqp-gl-gles31-tests'
  extends: .tests


cts gl - gles host:
  variables:
    TEST_SUITE: '--deqp-gles-gl-tests'
  extends: .tests

cts gles2 - gles host:
  variables:
    TEST_SUITE: '--deqp-gles-gles2-tests'
  extends: .tests

cts gles3 - gles host:
  variables:
    TEST_SUITE: '--deqp-gles-gles3-tests'
  extends: .tests

cts gles31 - gles host:
  variables:
    TEST_SUITE: '--deqp-gles-gles31-tests'
  extends: .tests