variables:
  FDO_UPSTREAM_REPO: mesa/mesa
  MESA_TEMPLATES_COMMIT: &ci-templates-commit d5aa3941aa03c2f716595116354fb81eb8012acb
  CI_PRE_CLONE_SCRIPT: |-
          set -o xtrace
          wget -q -O download-git-cache.sh ${CI_PROJECT_URL}/-/raw/${CI_COMMIT_SHA}/.gitlab-ci/download-git-cache.sh
          bash download-git-cache.sh
          rm download-git-cache.sh
          set +o xtrace
  CI_JOB_JWT_FILE: /minio_jwt
  MINIO_HOST: s3.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}
  # reference images stored for traces
  PIGLIT_REPLAY_REFERENCE_IMAGES_BASE: "${MINIO_HOST}/mesa-tracie-results/$FDO_UPSTREAM_REPO"
  # Individual CI farm status, set to "offline" to disable jobs
  # running on a particular CI farm (ie. for outages, etc):
  FD_FARM: "online"
  COLLABORA_FARM: "online"
  MICROSOFT_FARM: "online"
  LIMA_FARM: "online"
  IGALIA_FARM: "online"
  ANHOLT_FARM: "online"
  VALVE_FARM: "online"

default:
  before_script:
    - echo -e "\e[0Ksection_start:$(date +%s):unset_env_vars_section[collapsed=true]\r\e[0KUnsetting vulnerable environment variables"
    - 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"

  after_script:
    - >
      set +x

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

  # Retry build or test jobs up to twice when the gitlab-runner itself fails somehow.
  retry:
    max: 2
    when:
      - runner_system_failure

include:
  - project: 'freedesktop/ci-templates'
    ref: ffe4d1b10aab7534489f0c4bbc4c5899df17d3f2
    file:
      - '/templates/ci-fairy.yml'
  - project: 'freedesktop/ci-templates'
    ref: *ci-templates-commit
    file:
      - '/templates/alpine.yml'
      - '/templates/debian.yml'
      - '/templates/fedora.yml'
  - local: '.gitlab-ci/image-tags.yml'
  - local: '.gitlab-ci/lava/lava-gitlab-ci.yml'
  - local: '.gitlab-ci/container/gitlab-ci.yml'
  - local: '.gitlab-ci/build/gitlab-ci.yml'
  - local: '.gitlab-ci/test/gitlab-ci.yml'
  - local: '.gitlab-ci/test-source-dep.yml'
  - local: 'src/amd/ci/gitlab-ci.yml'
  - local: 'src/broadcom/ci/gitlab-ci.yml'
  - local: 'src/etnaviv/ci/gitlab-ci.yml'
  - local: 'src/freedreno/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/crocus/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/d3d12/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/i915/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/lima/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/llvmpipe/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/nouveau/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/softpipe/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/virgl/ci/gitlab-ci.yml'
  - local: 'src/gallium/drivers/zink/ci/gitlab-ci.yml'
  - local: 'src/gallium/frontends/lavapipe/ci/gitlab-ci.yml'
  - local: 'src/intel/ci/gitlab-ci.yml'
  - local: 'src/microsoft/ci/gitlab-ci.yml'
  - local: 'src/panfrost/ci/gitlab-ci.yml'
  - local: 'src/virtio/ci/gitlab-ci.yml'

stages:
  - sanity
  - container
  - git-archive
  - build-x86_64
  - build-misc
  - lint
  - amd
  - intel
  - nouveau
  - arm
  - broadcom
  - freedreno
  - etnaviv
  - software-renderer
  - layered-backends
  - deploy


# 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


.docs-base:
  variables:
    BUILDER: html
  extends:
    - .fdo.ci-fairy
    - .build-rules
  script:
  - apk --no-cache add graphviz doxygen
  - pip3 install sphinx===5.1.1 breathe===4.34.0 mako===1.2.3 sphinx_rtd_theme===1.0.0
  - docs/doxygen-wrapper.py --out-dir=docs/doxygen_xml
  - sphinx-build -W -b $BUILDER docs public

pages:
  extends: .docs-base
  stage: deploy
  artifacts:
    paths:
    - public
  needs: []
  rules:
    - !reference [.no_scheduled_pipelines-rules, rules]
    - if: *is-mesa-main
      changes: &docs-or-ci
      - docs/**/*
      - .gitlab-ci.yml
      when: always
    # Other cases default to never

test-docs:
  extends: .docs-base
  # Cancel job if a newer commit is pushed to the same branch
  interruptible: true
  stage: deploy
  needs: []
  rules:
    - !reference [.no_scheduled_pipelines-rules, rules]
    - if: *is-forked-branch
      changes: *docs-or-ci
      when: manual
    # Other cases default to never

test-docs-mr:
  extends:
    - test-docs
  needs:
    - sanity
  artifacts:
    expose_as: 'Documentation preview'
    paths:
    - public/
  rules:
    - if: *is-pre-merge
      changes: *docs-or-ci
      when: on_success
    # Other cases default to never

lincheck-docs:
  extends: .docs-base
  # Cancel job if a newer commit is pushed to the same branch
  interruptible: true
  stage: deploy
  needs: []
  rules:
    - !reference [.scheduled_pipeline-rules, rules]
  allow_failure: true
  variables:
    BUILDER: linkcheck

# When to automatically run the CI for build jobs
.build-rules:
  rules:
    - !reference [.no_scheduled_pipelines-rules, rules]
    # If any files affecting the pipeline are changed, build/test jobs run
    # automatically once all dependency jobs have passed
    - changes: &all_paths
      - VERSION
      - bin/git_sha1_gen.py
      - bin/install_megadrivers.py
      - bin/meson_get_version.py
      - bin/symbols-check.py
      # GitLab CI
      - .gitlab-ci.yml
      - .gitlab-ci/**/*
      # Meson
      - meson*
      - build-support/**/*
      - subprojects/**/*
      # Source code
      - include/**/*
      - src/**/*
      when: on_success
    # Otherwise, build/test jobs won't run because no rule matched.


.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:
  rules:
    - !reference [.no_scheduled_pipelines-rules, rules]
    # 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
      changes:
        *all_paths
      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
      changes:
        *all_paths
      when: on_success
    # Allow triggering jobs manually in other cases if any files affecting the
    # pipeline were changed
    - changes:
        *all_paths
      when: manual
    # Otherwise, container jobs won't run because no rule matched.

# Git archive

make git archive:
  extends:
    - .fdo.ci-fairy
  stage: git-archive
  rules:
    - !reference [.scheduled_pipeline-rules, rules]
  # ensure we are running on packet
  tags:
    - packet.net
  script:
    # Compactify the .git directory
    - git gc --aggressive
    # compress the current folder
    - tar -cvzf ../$CI_PROJECT_NAME.tar.gz .

    - ci-fairy s3cp --token-file "${CI_JOB_JWT_FILE}" ../$CI_PROJECT_NAME.tar.gz https://$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 block merging, but should be available to
# optionally run with the "play" button in the UI in pre-merge non-marge
# pipelines.  This should appear in "extends:" after any includes of
# test-source-dep.yml rules, so that these rules replace those.
.test-manual-mr:
  rules:
    - !reference [.no_scheduled_pipelines-rules, rules]
    - if: *is-forked-branch-or-pre-merge-not-for-marge
      changes:
        *all_paths
      when: manual
  variables:
    JOB_TIMEOUT: 80