# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2020-2021 Huawei Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

stages:
  - compliance
  - build
  - test
  - report
  - deploy

include:
 - project: 'OSTC/infrastructure/pipelines'
   file:
    - 'dco.yaml'
    - 'reuse.yaml'
 - local: '/.oniro-ci/build-generic.yaml'
 - local: '/.oniro-ci/test-generic.yaml'
 - local: '/.oniro-ci/machines-and-flavours.yaml'

dco:
  extends: .dco

reuse:
  extends: .reuse
  # FIXME: https://git.ostc-eu.org/distro/oniro/-/issues/19
  allow_failure: true

build-docs:
  extends: .build-docs

aggregate-docs:
  extends: .aggregate-docs
  needs: [build-docs]

# Customize the .workspace job to set the path of the git repository to deviate
# from what the git-repo prepares. This effectively allows testing incoming
# changes that match the repository holding this CI pipeline.
.workspace:
  variables:
    CI_ONIRO_GIT_REPO_PATH: oniro

# Customize the .bitbake-workspace job to set the rules governing when a build
# is attempted to: modifications (changes) to the pipeline, meta-layers,
# flavours and assets OR to placement of a tag on a commit OR when a job is
# scheduled. In addition, draft merge requests will no longer start the heavy
# build jobs automatically, giving an option to the developer, to start the
# desired jobs manually.
.bitbake-workspace:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TITLE =~ /^(wip|draft):.*/i'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - assets/**/*
        - flavours/**/*
        - meta-*/**/*
        - .oniro-ci/*
    - if: '$CI_COMMIT_TAG'

# Mimic the updated rules for lava-test from the bitbake-workspace.
.lava-test:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TITLE =~ /^(wip|draft):.*/i'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - assets/**/*
        - flavours/**/*
        - meta-*/**/*
        - .oniro-ci/*
    - if: '$CI_COMMIT_TAG'

# Mimic the updated rules for lava-report from the bitbake-workspace.
.lava-report:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TITLE =~ /^(wip|draft):.*/i'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - assets/**/*
        - flavours/**/*
        - meta-*/**/*
        - .oniro-ci/*
    - if: '$CI_COMMIT_TAG'

##
## Submit jobs to LAVA
##
lava-linux-avenger96:
  needs: [linux-stm32mp1-av96]
  stage: test
  extends: .lava-test
  variables:
    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/avenger96-acts.yaml"
    CI_BUILD_JOB_NAME: linux-stm32mp1-av96
    CI_REPORT_JOB_NAME: lava-report
  rules:
    - when: never

lava-qemu-x86_64:
  needs: [linux-qemu-x86_64]
  stage: test
  extends: .lava-test
  variables:
    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86_64-acts.yaml"
    CI_BUILD_JOB_NAME: linux-qemu-x86_64
    CI_REPORT_JOB_NAME: lava-report
  rules:
    - when: never

lava-qemu-x86:
  needs: [linux-qemu-x86]
  stage: test
  extends: .lava-test
  variables:
    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86-acts.yaml"
    CI_BUILD_JOB_NAME: linux-qemu-x86
    CI_REPORT_JOB_NAME: lava-report
  rules:
    - when: never

lava-report:
  needs: [lava-linux-avenger96, lava-qemu-x86_64, lava-qemu-x86]
  extends: .lava-report
  rules:
    - when: never
