---
include:
  - https://salsa.debian.org/salsa-ci-team/pipeline/raw/master/salsa-ci.yml
  - https://salsa.debian.org/salsa-ci-team/pipeline/raw/master/pipeline-jobs.yml

variables:
  # don't bother with irrelevant tests
  SALSA_CI_DISABLE_BUILD_PACKAGE_I386: 1
  SALSA_CI_DISABLE_BLHC: 1
  SALSA_CI_DISABLE_CROSSBUILD_ARM64: 1
  # this ensures we don't exceed salsa's artifact limit when building in CI
  DEB_BUILD_PROFILES: pkg.debian-installer.miniiso

  # Reprotest should check our images tarball as well
  SALSA_CI_REPROTEST_ARTIFACT_PATTERN: 'debian-installer-images_*.tar.gz *.deb'

# In the normal pipeline, we run `build` as usual, and then provide a
# `Test with openQA` job for launching some openQA tests of the mini-ISO
# result. (on salsa, building anything more than a mini-ISO exceeds limits)
#
# If this pipeline is triggered from a `branch2repo` based job, the mini-ISO we
# build should include udebs found in the parent pipeline's `aptly` job. The job
# that does this is called `mini-ISO` and conditionally replaces `build` in the
# pipeline.
#
# `$CI_PIPELINE_SOURCE` is checked to see which scenario we're in, as it should
# only be set to `"parent_pipeline"` when triggered by a branch2repo parent
# pipeline.

.kludge-kernel-abi-issues-script: &kludge-kernel-abi-issues-script
    - |
      set -x
      source ${CI_PROJECT_DIR}/salsa.env

      arch=$(dpkg-architecture -qDEB_HOST_ARCH)
      cfg_abi=$(sed -ne 's/^LINUX_KERNEL_ABI\s*[?]=\s*\(.*\)\s*$/\1/p' build/config/common)
      # use available rather than configured ABI when things are skewed (allowing salsa builds to continue working)
      avail_abi=$(apt-cache show linux-headers-${arch}/${RELEASE_FROM_CHANGELOG} | sed -ne 's/Depends:.* linux-headers-\([^,]*\)-'"${arch}"'.*$/\1/p')
      if [ "${avail_abi}" != "${cfg_abi}" ]; then
        printf "warning: Using available ABI (%s) instead of the one set in the source (%s)\n" "$avail_abi" "$cfg_abi"
        sed -i -e "1 s/)/+ABI~${avail_abi/-/\~})/;1 a \ \n  [ Salsa-CI ]\n  * WARNING: automatically modified to use ABI = ${avail_abi}\n    This needs to be updated in the source to work for normal builds" debian/changelog
        sed -i -e "s/^\(\s*linux-image-\)${cfg_abi}/\1${avail_abi}/" debian/control
        sed -i -e "s/^\(LINUX_KERNEL_ABI\s*[?]=\s*\).*$/\1${avail_abi}/" build/config/common
        grep LINUX_KERNEL_ABI build/config/common
        echo "LINUX_KERNEL_ABI=${avail_abi}" >> ${CI_PROJECT_DIR}/salsa.env
        # check for unsigned version of configured ABI
        if apt-cache show linux-image-${avail_abi}-${arch} | grep -q 'No packages found'; then
          printf "warning: Using unsigned kernel, as the signed kernel has not yet been released\n"
          sed -i -e "1 s/)/+unsigned)/;/Salsa-CI/ a \  * WARNING: automatically modified to build-depend upon the unsigned kernel while we await the signed version" debian/changelog
          sed -i -e "s/^\(\s*linux-image-${avail_abi}-\S*\b\)/\1-unsigned/" debian/control
          echo "LINUX_KERNEL_UNSIGNED=1" >> ${CI_PROJECT_DIR}/salsa.env
        fi
      fi

.local-build-script: &local-build-script
  - !reference [.build-script-setup-environment]
  - !reference [.build-script-extract-source]
  - !reference [.build-script-setup-build-command]
  - *kludge-kernel-abi-issues-script
  - |
    if [ "$APTLY_REPO" ]; then
      # add the aptly repo settings to sbuild's build environment
      mkdir -p ~salsa-ci/.config/sbuild
      cat <<-EOF >> ~salsa-ci/.config/sbuild/config.pl
    		\$build_environment->{"APTLY_REPO"} = "$APTLY_REPO" ;
    		\$build_environment->{"APTLY_REPO_KEY_BASE64"} = "$APTLY_REPO_KEY_BASE64" ;
    		EOF
    fi
    head -v ~salsa-ci/.config/sbuild/config.pl || true
  - !reference [.build-script-run-build-command]
  - |
    for BUILD_JOB_ISO_FULL in $WORKING_DIR/*.iso; do break; done
    cat >> ${CI_PROJECT_DIR}/salsa.env <<-EOF
    	BUILD_JOB_URL=$CI_JOB_URL
    	BUILD_JOB_ISO=${BUILD_JOB_ISO_FULL#$CI_PROJECT_DIR}
    	BUILD_JOB_DEBIAN_BUILD_ID=$(echo $BUILD_JOB_ISO|sed -nE 's%.*/debian-(.*)-gtkmini.iso%\1%p')
    	EOF

.di-script-build-definition: &di-script-build-definition
  - !reference [.get-target-distribution]
  - !reference [.build-before-script]
  - *local-build-script
  - head -v ${CI_PROJECT_DIR}/salsa.env

.di-build-definition:
  extends: .build-definition-common
  variables:
    SALSA_CI_SBUILD_ARGS: "--enable-network"
    # gen-sources.list.udeb does not grok deb822 format (as used on salsa),
    # so we need this to tell the build from where to get udebs:
    MIRROR: "http://deb.debian.org/debian"
  script:
    - *di-script-build-definition

build:
  extends: .di-build-definition
  rules:
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"
      when: never
    - when: on_success

# no need for us to build source or aptly when running under branch2repo
aptly:
  extends: .publish-aptly
  rules:
    - if: $APTLY_REPO
      when: never
    - !reference [.publish-aptly, rules]

build source:
  extends: .build-source-only
  rules:
    - if: $APTLY_REPO == null

mini-ISO:
  extends: .di-build-definition
  rules:
    - if: $CI_PIPELINE_SOURCE != "parent_pipeline"
      when: never
    - when: on_success
  script:
    - |
      # it seems we need to pass these vars via salsa.env in order to get them into the openqa-link job
      cat >> ${CI_PROJECT_DIR}/salsa.env <<-EOF
      	OQA_JOBSTOCLONE=${OQA_JOBSTOCLONE}
      	OQA_CLONE_EXTRA_ARGS="${OQA_CLONE_EXTRA_ARGS}"
      	OQA_CLONE_GRUBADD="${OQA_CLONE_GRUBADD}"
      	EOF
    - *di-script-build-definition

.openqa-link-definition:
  stage: test
  variables:
    OQA_ISO_URL: '$BUILD_JOB_URL/artifacts/raw/$BUILD_JOB_ISO'
    OQA_ISO: 'salsa-$CI_PIPELINE_ID-gtk-mini.iso'
    OQA_BUILD: '-salsa-$CI_PIPELINE_ID:$CI_PROJECT_PATH:$CI_COMMIT_REF_NAME'
    OQA_CI_PIPELINE_URL: '$CI_SERVER_URL/$CI_PROJECT_PATH/-/pipelines/$CI_PIPELINE_ID'
    # OQA_SALSA_RESULT_LINK: "$CI_API_V4_URL/projects/$CI_PROJECT_ID/statuses/$CI_COMMIT_SHA?pipeline_id=$CI_PIPELINE_ID&name=openqa&target_url=:target_url&state=:state"
    CLONE_NAME_SUFFIX: '$CI_PROJECT_NAMESPACE-$CI_COMMIT_REF_SLUG'
  trigger:
    project: $GITLAB_USER_LOGIN/openqa-link

Test with openQA:
  extends: .openqa-link-definition
  rules:
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"
      when: never
    - when: manual
      variables:
        OPENQALINK_MANUALLY_TRIGGERED: 1
  needs:
    - job: build
      artifacts: true

openqa-link:
  extends: .openqa-link-definition
  rules:
    - if: $CI_PIPELINE_SOURCE != "parent_pipeline"
      when: never
    - when: on_success
  variables:
    OQA_JOBSTOCLONE: $OQA_JOBSTOCLONE
    OQA_CLONE_EXTRA_ARGS: $OQA_CLONE_EXTRA_ARGS
    OQA_CLONE_GRUBADD: $OQA_CLONE_GRUBADD
  needs:
    - job: mini-ISO
      artifacts: true
  allow_failure: true

# the deb822 problem breaks these tests, so let's allow them to fail for now:
reprotest:
  extends: .test-reprotest
  allow_failure: true

test-build-any:
  extends: .test-build-package-any
  allow_failure: true

ABI-check:
  stage: test
  image: $SALSA_CI_IMAGES_BASE
  script:
    - |
      if [ "$LINUX_KERNEL_ABI" ]; then
        cat <<-EOF
      	############################[ KERNEL SKEW ]############################
      	LINUX_KERNEL_ABI has been set ($LINUX_KERNEL_ABI) in the build job
      	which indicates that we had to kludge the ABI version to make the build work.
      	.
      	That being the case, this test will fail in order to indicate that
      	something unusual is occuring here.
      	#######################################################################
      	EOF
        exit 1
      elif [ "$LINUX_KERNEL_UNSIGNED" ]; then
        cat <<-EOF
      	######################[ UNSIGNED KERNEL IN USE ]#######################
      	The build job noticed that the signed kernel is not yet available,
      	so we've used the unsigned version in order to let the D-I build succeed.
      	.
      	That being the case, this test will fail in order to indicate that
      	something unusual is occuring here.
      	#######################################################################
      	EOF
        exit 1
      fi
      echo "Normal, non-ABI-skewed build."
  variables:
    GIT_STRATEGY: none
  allow_failure: true
  needs:
    - job: build
      optional: true
    - job: mini-ISO
      optional: true
