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

stages:
  - manifest-pipeline
  - retrigger
  - build

workflow:
  rules:
    # Explicitly allow externally triggered pipelines in every case
    - if: $CI_PIPELINE_SOURCE == "api"
    - if: $CI_PIPELINE_SOURCE == "pipeline"
    - if: $CI_PIPELINE_SOURCE == "web"
    # 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
    # Do not run pipeline if the "skip build" label is set on the merge request
    - if: $CI_MERGE_REQUEST_LABELS =~ /skip build/
      when: never
    # Run pipelines for merge requests
    - if: $CI_MERGE_REQUEST_IID
    # Run pipelines on the master branch
    - if: $CI_COMMIT_REF_NAME == $MASTER_BRANCH
    # Run pipelines on tags
    - if: $CI_COMMIT_TAG

.full_build_pipeline:
  rules:
    # Run the full build pipeline on merge requests and tags
    # or if explicitly triggered by the API or the web button.
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_PIPELINE_SOURCE == "api"
    - if: $CI_PIPELINE_SOURCE == "pipeline"
    - if: $CI_PIPELINE_SOURCE == "web"
  stage: manifest-pipeline

.short_master_pipeline:
  rules:
    # The short master pipeline does not execute a full build, but only mirrors the
    # build result from the merge request. Run it on the master branch per default if
    # not explicitly triggered.
    - if: $CI_PIPELINE_SOURCE == "api"
      when: never
    - if: $CI_PIPELINE_SOURCE == "pipeline"
      when: never
    - if: $CI_PIPELINE_SOURCE == "web"
      when: never
    - if: $CI_COMMIT_REF_NAME == $MASTER_BRANCH

# --------------------------------------------------------------------------------------
# Full build pipeline (runs in merge requests, and on master if manually triggered)
# --------------------------------------------------------------------------------------
generate-build-jobs:
  extends:
    - .infrastructure
    - .full_build_pipeline
  script:
    # The job generation script implicitly passes the OS environment to the template, so
    # that the template has access to all GitLab CI variables. Hence there is no need
    # to explicitly pass any of them as command line arguments.
    - .gitlab-ci/scripts/generate_job_from_template.py
              --template=.gitlab-ci/${BUILD_JOBS_TEMPLATE}
              > build-jobs.yml
  artifacts:
    expire_in: 4 weeks
    paths:
      - build-jobs.yml

build-jobs:
  extends:
    - .full_build_pipeline
  needs: ["generate-build-jobs"]
  trigger:
    include:
      - artifact: build-jobs.yml
        job: generate-build-jobs
    strategy: depend

yamllint:
  extends:
    - .yamllint
    - .full_build_pipeline

# --------------------------------------------------------------------------------------
# Short master pipeline (runs on master after merging a merge request)
# --------------------------------------------------------------------------------------
retrigger:
  extends:
    - .infrastructure
    - .short_master_pipeline
  stage: retrigger
  script:
    - .gitlab-ci/scripts/retrigger_integrating_projects.py
        --gitlab-url=${CI_SERVER_URL}
        --token=${GITBOT_TOKEN}
        --manifest-project=${CI_PROJECT_PATH}
        --manifest-branch=${MASTER_BRANCH}
        --group=${RETRIGGER_GROUP}

build:merge_request:
  extends:
    - .infrastructure
    - .short_master_pipeline
  stage: build
  # For some reason we cannot use a variable for the timeout, the CI linter reports
  # 'jobs:build:merge_request:timeout config should be a duration' then.
  # Hence we use GitLab's 'reference' syntax instead.
  timeout: !reference [variables, BUILD_TIMEOUT]
  script:
    - cd ${CI_PROJECT_DIR}
    - .gitlab-ci/scripts/mirror_mr_pipeline.py
        --gitlab-url=${CI_SERVER_URL}
        --token=${GITBOT_TOKEN}
        --project=${CI_PROJECT_PATH}
        --commit=${CI_COMMIT_SHA}
        --ref=${MASTER_BRANCH}