include:
 - project: 'guidog/meta-phosh'
   ref: 'a9fef9ad314e8b387fb9b293b9f064527ac0c023'
   file: '/ci/phosh-common-jobs.yml'
 - project: 'Infrastructure/Mirrors/lorry-mirrors/gitlab_freedesktop_org/freedesktop/ci-templates'
   ref: 'b791bd48996e3ced9ca13f1c5ee82be8540b8adb'
   file: '/templates/ci-fairy.yml'

stages:
  - build
  - tests
  - style-checks
  - package
  - deploy

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    # Don't trigger a branch pipeline if there is an open MR
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH

default:
  # Protect CI infra from rogue jobs
  timeout: 15 minutes
  # Allow jobs to be caneled on new commits
  interruptible: true
  # Retry on infra hickups automatically
  retry:
    max: 1
    when:
      - 'api_failure'
      - 'runner_system_failure'
      - 'scheduler_failure'
      - 'stuck_or_timeout_failure'

variables:
  # For ci-fairy
  FDO_UPSTREAM_REPO: World/Phosh/gmobile
  DEPS: >-
    ccache build-essential git gcovr
  DEBIAN_IMAGE: debian:trixie

.before: &before
  - echo "man-db man-db/auto-update boolean false" | debconf-set-selections
  - export DEBIAN_FRONTEND=noninteractive
  - apt-get -y update
  - apt-get -y install eatmydata
  - eatmydata apt-get -y install $DEPS
  - eatmydata apt-get -y build-dep .

.build: &build_steps
    echo "BUILD_OPTS=${BUILD_OPTS}" &&
    export LC_ALL=C.UTF-8 &&
    meson ${BUILD_OPTS} . _build  &&
    ninja -C _build

build-debian-gcc:
  stage: build
  image: $DEBIAN_IMAGE
  variables:
    BUILD_OPTS: -Db_coverage=true --werror
  before_script:
    - *before
  script:
    - *build_steps
  artifacts:
    when: always
    paths:
      - _build

# TODO: move to prebuilt container
doc:
  stage: build
  image: $DEBIAN_IMAGE
  variables:
    BUILD_OPTS: >-
      -Dtests=false
      -Dgtk_doc=true
  before_script:
    - *before
  script:
    - meson ${BUILD_OPTS} _build
    - ninja -C _build
    - mv _build/doc/gmobile-0 _doc/
  artifacts:
    paths:
      - _doc/

tests:
  stage: tests
  image: $DEBIAN_IMAGE
  needs: ['build-debian-gcc']
  before_script:
    - *before
  script:
    - meson ${BUILD_OPTS} _build
    - meson test -C _build
    - ninja -C _build coverage
  coverage: '/^lines:\s+([\d.]+\%)\s+/'
  artifacts:
    reports:
      junit: _build/meson-logs/testlog.junit.xml

# Sanity checks of MR settings and commit logs
sanity:
  extends:
    - .fdo.ci-fairy
  stage: style-checks
  variables:
    GIT_DEPTH: "100"
  needs: []
  script: |
    ci-fairy check-commits --junit-xml=commit-message-junit-report.xml
  artifacts:
    reports:
      junit: commit-message-junit-report.xml
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME'

format-check:
  stage: style-checks
  extends: .phosh-format-check
  allow_failure: true

check-meson:
  stage: style-checks
  extends:
    - .phosh-check-meson

package-deb-debian-trixie:arm64:
  stage: package
  variables:
    PHOSH_DOCKER_IMAGE: debian:trixie
  extends: .phosh-build-debian-package
  tags:
    - aarch64

render-display-info:
  stage: tests
  image: $DEBIAN_IMAGE
  needs: ['build-debian-gcc']
  before_script:
    - *before
  script:
    - tools/render-all
  artifacts:
    expose_as: 'Display panel html'
    paths:
      - _build/out/

pages:
  stage: deploy
  image: $DEBIAN_IMAGE
  before_script: []
  needs: ['doc']
  script:
    - mv _doc/ public/
  artifacts:
    paths:
      - public
  only:
    - main
