---
# --------------------------------------------------------------------------------------
# Global
# --------------------------------------------------------------------------------------
include:
  - local: common.yml

stages:
  - retrigger
  - infrastructure

variables:
  # Default image and distro
  CI_PARAM_IMAGE: guf-image
  CI_PARAM_DISTRO: guf-wayland
  # Flash-N-Go image and distro
  #
  # In the past, the buildfng job overwrote the image and distro itself.
  # Because of the transition to the new seconorth names, the image and
  # distro for the buildfng must be settable from outside of the job.
  CI_PARAM_IMAGE_FNG: fngsystem-image
  CI_PARAM_DISTRO_FNG: guf-fngsystem
  # List of machines to build images for:
  CI_PARAM_MACHINES: imx6guf imx6ullguf imx8mguf imx8mpguf
  # The id of the gitlab project used in the rules section
  # to not run pipelines in forked projects
  # Using variable here, to allow override in other projects
  # including this file
  MANIFEST_PROJECT_ID: 1725

workflow:
  rules:
    # Explicitly allow externally triggered pipelines in every case
    - if: $CI_PIPELINE_SOURCE == "pipeline" || $CI_PIPELINE_SOURCE == "api"
    # Do not run pipelines for merge requests
    - if: $CI_MERGE_REQUEST_IID
      when: never
    # Do not run pipelines on forked projects
    # (use id instead of name because of rename)
    - if: $CI_PROJECT_ID != $MANIFEST_PROJECT_ID
      when: never
    # Do not run pipelines on integration branches
    - if: $CI_COMMIT_REF_NAME =~ /^integrate\/.*/
      when: never
    # In all other cases, run the pipeline automatically
    - when: always

# --------------------------------------------------------------------------------------
# Stage: retrigger
# --------------------------------------------------------------------------------------
retrigger:
  extends: .infrastructure
  stage: retrigger
  rules:
    - if: $CI_COMMIT_REF_NAME == $MASTER_BRANCH_MANIFEST && $CI_PIPELINE_SOURCE != "api"
  script:
    - PROJECTS=$(
        .gitlab-ci/scripts/get_manifest_projects.py
        --manifest=default.xml
        --remote=seco-ne
        --concat-namespaces
      )
    # Add the gitlab-ci project
    - PROJECTS="$PROJECTS ${CI_PROJECT_ROOT_NAMESPACE}/yocto/infrastructure/gitlab-ci"
    # TODO retrigger gitlab-ci integration also
    # Retrigger also project in SRCREV
    - echo -e "Projects:\n${PROJECTS}"
    - for PROJECT in ${PROJECTS}; do
        .gitlab-ci/scripts/retrigger_mr_pipeline_jobs.py
          --gitlab-url=${CI_SERVER_URL}
          --token=${GITBOT_TOKEN}
          --project=${PROJECT}
          --state=opened
          --target-branch=${MASTER_BRANCH_MANIFEST}
          --job=check
        ;
      done

generate-build-jobs:
  extends: .infrastructure
  rules:
    - if: $CI_COMMIT_REF_NAME != $MASTER_BRANCH_MANIFEST || $CI_PIPELINE_SOURCE == "api"
  script:
    - .gitlab-ci/scripts/generate_job_from_template.py
              --template=.gitlab-ci/build-jobs.jinja2
              --distro=$CI_PARAM_DISTRO
              --yocto_image=$CI_PARAM_IMAGE
              --distro_fng=$CI_PARAM_DISTRO_FNG
              --fngsystem_image=$CI_PARAM_IMAGE_FNG
              --gitlab_ci_current_rev=$GITLAB_CI_CURRENT_REV
              --ci_project_root_namespace=$CI_PROJECT_ROOT_NAMESPACE
              --master_branch_manifest=$MASTER_BRANCH_MANIFEST
              $CI_PARAM_MACHINES
              > build-jobs.yml
  artifacts:
    expire_in: 4 weeks
    paths:
      - build-jobs.yml

trigger-build-jobs:
  stage: infrastructure
  needs: ["generate-build-jobs"]
  rules:
    - if: $CI_COMMIT_REF_NAME != $MASTER_BRANCH_MANIFEST || $CI_PIPELINE_SOURCE == "api"
  trigger:
    include:
      - artifact: build-jobs.yml
        job: generate-build-jobs
    strategy: depend
