variables:
  # Change this to use your fork of drm-ci
  DRM_CI_PROJECT_PATH: &drm-ci-project-path gfx-ci/drm-ci

  FDO_UPSTREAM_REPO: tomeu/linux   # The repo where the git-archive daily runs
  MESA_TEMPLATES_COMMIT: &ci-templates-commit 290b79e0e78eab67a83766f4e9691be554fc4afd
  DRM_CI_COMMIT_SHA: &drm-ci-commit-sha main
  DRM_CI_PROJECT_URL: https://gitlab.freedesktop.org/${DRM_CI_PROJECT_PATH}
  CI_PRE_CLONE_SCRIPT: |-
          set -o xtrace
          wget -q -O download-git-cache.sh ${DRM_CI_PROJECT_URL}/-/raw/${DRM_CI_COMMIT_SHA}/.gitlab-ci/download-git-cache.sh
          bash download-git-cache.sh
          rm download-git-cache.sh
          set +o xtrace
  MINIO_HOST: minio-packet.freedesktop.org
  # per-pipeline artifact storage on MinIO
  PIPELINE_ARTIFACTS_BASE: ${MINIO_HOST}/artifacts/${CI_PROJECT_PATH}/${CI_PIPELINE_ID}
  # per-job artifact storage on MinIO
  JOB_ARTIFACTS_BASE: ${PIPELINE_ARTIFACTS_BASE}/${CI_JOB_ID}

default:
  before_script:
    - echo -e "\e[0Ksection_start:$(date +%s):unset_env_vars_section[collapsed=true]\r\e[0KUnsetting vulnerable environment variables"
    - export CI_JOB_JWT_FILE="${CI_JOB_JWT_FILE:-$(mktemp)}"
    - echo -n "${CI_JOB_JWT}" > "${CI_JOB_JWT_FILE}"
    - unset CI_JOB_JWT
    - echo -e "\e[0Ksection_end:$(date +%s):unset_env_vars_section\r\e[0K"

    - cd $CI_PROJECT_DIR
    - curl --output - $DRM_CI_PROJECT_URL/-/archive/$DRM_CI_COMMIT_SHA/drm-ci-$DRM_CI_COMMIT_SHA.tar.gz | tar -xz
    - mv drm-ci-$DRM_CI_COMMIT_SHA/.gitlab-ci* .
    - rm -rf drm-ci-$DRM_CI_COMMIT_SHA/

  after_script:
    - >
      set +x

      test -e "${CI_JOB_JWT_FILE}" &&
      export CI_JOB_JWT="$(<${CI_JOB_JWT_FILE})" &&
      rm "${CI_JOB_JWT_FILE}"

include:
  - project: 'freedesktop/ci-templates'
    ref: 34f4ade99434043f88e164933f570301fd18b125
    file:
      - '/templates/ci-fairy.yml'
  - project: 'freedesktop/ci-templates'
    ref: *ci-templates-commit
    file:
      - '/templates/debian.yml'
      - '/templates/fedora.yml'
  - project: *drm-ci-project-path
    ref: *drm-ci-commit-sha
    file:
      - '.gitlab-ci/container/gitlab-ci.yml'
      - '.gitlab-ci/test/gitlab-ci.yml'
      - '.gitlab-ci/lava/lava-gitlab-ci.yml'
      - '.gitlab-ci/local/image-tags.yml'
      - '.gitlab-ci/local/container.yml'
      - '.gitlab-ci/local/static-checks.yml'
      - '.gitlab-ci/local/build.yml'
      - '.gitlab-ci/local/test.yml'


stages:
  - sanity
  - container
  - git-archive
  - build
  - amdgpu
  - i915
  - mediatek
  - meson
  - msm
  - rockchip
  - virtio-gpu

# Generic rule to not run the job during scheduled pipelines
# ----------------------------------------------------------
.scheduled_pipelines-rules:
  rules: &ignore_scheduled_pipelines
    if: &is-scheduled-pipeline '$CI_PIPELINE_SOURCE == "schedule"'
    when: never

# YAML anchors for rule conditions
# --------------------------------
.rules-anchors:
  rules:
    # Pipeline for forked project branch
    - if: &is-forked-branch '$CI_COMMIT_BRANCH && $CI_PROJECT_NAMESPACE != "mesa"'
      when: manual
    # Forked project branch / pre-merge pipeline not for Marge bot
    - if: &is-forked-branch-or-pre-merge-not-for-marge '$CI_PROJECT_NAMESPACE != "mesa" || ($GITLAB_USER_LOGIN != "marge-bot" && $CI_PIPELINE_SOURCE == "merge_request_event")'
      when: manual
    # Pipeline runs for the main branch of the upstream Mesa project
    - if: &is-mesa-main '$CI_PROJECT_NAMESPACE == "mesa" && $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_COMMIT_BRANCH'
      when: always
    # Post-merge pipeline
    - if: &is-post-merge '$CI_PROJECT_NAMESPACE == "mesa" && $CI_COMMIT_BRANCH'
      when: on_success
    # Post-merge pipeline, not for Marge Bot
    - if: &is-post-merge-not-for-marge '$CI_PROJECT_NAMESPACE == "mesa" && $GITLAB_USER_LOGIN != "marge-bot" && $CI_COMMIT_BRANCH'
      when: on_success
    # Pre-merge pipeline
    - if: &is-pre-merge '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: on_success
    # Pre-merge pipeline for Marge Bot
    - if: &is-pre-merge-for-marge '$GITLAB_USER_LOGIN == "marge-bot" && $CI_PIPELINE_SOURCE == "merge_request_event"'
      when: on_success

# When to automatically run the CI
.ci-run-policy:
  rules:
    - *ignore_scheduled_pipelines
    # Run these jobs always for now 
    - when: on_success
  retry:
    max: 2
    when:
      - runner_system_failure


.ci-deqp-artifacts:
  artifacts:
    name: "mesa_${CI_JOB_NAME}"
    when: always
    untracked: false
    paths:
      # Watch out!  Artifacts are relative to the build dir.
      # https://gitlab.com/gitlab-org/gitlab-ce/commit/8788fb925706cad594adf6917a6c5f6587dd1521
      - artifacts
      - _build/meson-logs/*.txt
      - _build/meson-logs/strace


.container-rules:
  extends:
    - .ci-run-policy
  rules:
    - *ignore_scheduled_pipelines
    # Run pipeline by default in the main project if any CI pipeline
    # configuration files were changed, to ensure docker images are up to date
    - if: *is-post-merge
      changes:
      - .gitlab-ci.yml
      - .gitlab-ci/**/*
      when: on_success
    # Run pipeline by default if it was triggered by Marge Bot, is for a
    # merge request, and any files affecting the pipeline were changed
    - if: *is-pre-merge-for-marge
      when: on_success
    # Run pipeline by default in the main project if it was not triggered by
    # Marge Bot, and any files affecting the pipeline were changed
    - if: *is-post-merge-not-for-marge
      when: on_success
    # Allow triggering jobs manually in other cases
    - when: on_success

# Git archive

make git archive:
  extends:
    - .fdo.ci-fairy
  stage: git-archive
  rules:
    - if: *is-scheduled-pipeline
      when: on_success
  # ensure we are running on packet
  tags:
    - packet.net
  script:
    # Remove drm-ci files we just added
    - rm -rf .gitlab-ci.*

    # Compactify the .git directory
    - git gc --aggressive
    # compress the current folder
    - tar -cvzf ../$CI_PROJECT_NAME.tar.gz .

    # login with the JWT token file
    - ci-fairy minio login --token-file "${CI_JOB_JWT_FILE}"
    - ci-fairy minio cp ../$CI_PROJECT_NAME.tar.gz minio://$MINIO_HOST/git-cache/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME/$CI_PROJECT_NAME.tar.gz


# Sanity checks of MR settings and commit logs
sanity:
  extends:
    - .fdo.ci-fairy
  stage: sanity
  rules:
    - if: *is-pre-merge
      when: on_success
    # Other cases default to never
  variables:
    GIT_STRATEGY: none
  script:
    # ci-fairy check-commits --junit-xml=check-commits.xml
    - ci-fairy check-merge-request --require-allow-collaboration --junit-xml=check-merge-request.xml
  artifacts:
    when: on_failure
    reports:
      junit: check-*.xml

# Rules for tests that should not be present in MRs or the main
# project's pipeline (don't block marge or report red on
# mesa/mesa main) but should be present on pipelines in personal
# branches (so you can opt in to running the flaky test when you want
# to).
.test-manual:
  rules:
    - *ignore_scheduled_pipelines
    - if: *is-forked-branch
      when: manual
    - when: never
  variables:
    GIT_STRATEGY: none
    JOB_TIMEOUT: 80

# The above .test-manual rules doesn't allow the jobs to be available for MRs
# but we should have an option to have manual jobs in MRs as well.
.test-manual-mr:
  rules:
    - *ignore_scheduled_pipelines
    - if: *is-forked-branch-or-pre-merge-not-for-marge
      when: manual
    - when: never
  variables:
    GIT_STRATEGY: none
    JOB_TIMEOUT: 80