obs-x86_64-default:
  image: registry.gitlab.apertis.org/infrastructure/apertis-docker-images/v2023dev2-package-source-builder
  stage: build
  tags:
    - monitoring
  timeout: 1d # the LLVM build can take more than 20h
  retry:
    max: 2 # the OBS API can be quite flaky, try again when failing to see if the stars are better aligned
  variables:
    GIT_STRATEGY: none
  script:
    - echo 'https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl' 'default' 'x86_64' '1' 'f51c4465eebde4972aa5d857615aebc7'
    - osc-results () {
        osc results --verbose 'apertis:v2023dev2:development' 'libcss-tiny-perl'
          --repo='default' --arch='x86_64' --csv
          --format='%(code)s • %(dirty)s • %(repository)s • %(arch)s • %(state)s • %(details)s' || true;
      }
    - echo "📒 Current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
    - osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | head -n3 || true
    - touch build.log
    - |
      while true
      do
        echo "🔍 Check the status of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if grep -E '^\S+ • True' result.txt
        then
          echo "⏲️ OBS reported that the status need to be recomputed, wait a bit"
          sleep 30
          continue
        fi
        if osc api '/build/apertis:v2023dev2:development/default/x86_64/libcss-tiny-perl/_log?end=750' | grep "f51c4465eebde4972aa5d857615aebc7"
        then
          OFFSET=$(stat --format '%s' build.log)
          echo "📜 Retrieve the build log at offset ${OFFSET}"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
          if [ "${OFFSET}" -lt "3000000" ]
          then
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'x86_64' --offset ${OFFSET} | tee -a build.log | head -c "$((MAX_BUILDLOG_SIZE - OFFSET))" || true
            if [ "$(stat --format '%s' build.log)" -ge "3000000" ]
            then
              continue
            fi
          else
            echo "🔍 Truncated at 3000000 bytes, full build log at ${CI_JOB_URL}/artifacts/file/build.log"
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'x86_64' --offset ${OFFSET} >> build.log || true
            echo "🔍 Last 10000 bytes of build log"
            tail -c 10000 build.log
          fi
        else
          echo "📜 Build log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/x86_64 f51c4465eebde4972aa5d857615aebc7 not available"
        fi
        echo "📒 Check the current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | tee log.txt || true
        echo "🔍 Check the updated package status for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if ! head -n1 log.txt | grep '^1|'
        then
          echo "🏳️ Revision 1 of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl is no longer current"
          break
        fi
        if grep -E '^(succeeded|failed|unresolvable|broken|disabled|excluded) • False' result.txt
        then
          echo "🏁 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/x86_64 reached a final status"
          break
        fi
        echo "⏲️ Wait a bit before checking the status again"
        sleep 30
      done
    - osc api '/build/apertis:v2023dev2:development/default/x86_64/_jobhistory?package=libcss-tiny-perl' | tee buildhistory.txt
    - |
      if grep -E '^(disabled|excluded)' result.txt
      then
        echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl won't be built on default/x86_64"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
        exit 0
      fi
    - |
      if ! grep -q '<jobhist\b' buildhistory.txt
      then
        echo "No builds registered, the package may have never been rebuilt since branching"
        if grep ^succeeded result.txt
        then
          echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was imported successfully on default/x86_64"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
          exit 0
        else
          echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was never built successfully on default/x86_64"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
          exit 1
        fi
      fi
    - grep 'rev="1"' buildhistory.txt | tail -n1 | tee result.txt
    - |
      if ! grep 'code="succeeded"' result.txt
      then
        echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl didn't succeed on default/x86_64"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
        exit 1
      fi
    - echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl built successfully on default/x86_64"
    - echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/x86_64"
    - osc getbinaries -d results 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'x86_64'
  artifacts:
    paths:
      - build.log
      - results/
    when: always
    expire_in: 3 days
  rules:
    # NOTE: this is copied from the .rules template at the top of this
    # file, make sure any edits to one or the other stay in sync!
    # This needs to be repeated here to avoid errors:
    # https://gitlab.com/gitlab-org/gitlab/-/issues/276179#note_700305979
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      # do not run on a branch if there's a MR open, run only in MR context so
      # we can derive the release from the target branch
      when: never
    - when: on_success

obs-armv7hl-default:
  image: registry.gitlab.apertis.org/infrastructure/apertis-docker-images/v2023dev2-package-source-builder
  stage: build
  tags:
    - monitoring
  timeout: 1d # the LLVM build can take more than 20h
  retry:
    max: 2 # the OBS API can be quite flaky, try again when failing to see if the stars are better aligned
  variables:
    GIT_STRATEGY: none
  script:
    - echo 'https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl' 'default' 'armv7hl' '1' 'f51c4465eebde4972aa5d857615aebc7'
    - osc-results () {
        osc results --verbose 'apertis:v2023dev2:development' 'libcss-tiny-perl'
          --repo='default' --arch='armv7hl' --csv
          --format='%(code)s • %(dirty)s • %(repository)s • %(arch)s • %(state)s • %(details)s' || true;
      }
    - echo "📒 Current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
    - osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | head -n3 || true
    - touch build.log
    - |
      while true
      do
        echo "🔍 Check the status of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if grep -E '^\S+ • True' result.txt
        then
          echo "⏲️ OBS reported that the status need to be recomputed, wait a bit"
          sleep 30
          continue
        fi
        if osc api '/build/apertis:v2023dev2:development/default/armv7hl/libcss-tiny-perl/_log?end=750' | grep "f51c4465eebde4972aa5d857615aebc7"
        then
          OFFSET=$(stat --format '%s' build.log)
          echo "📜 Retrieve the build log at offset ${OFFSET}"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
          if [ "${OFFSET}" -lt "3000000" ]
          then
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'armv7hl' --offset ${OFFSET} | tee -a build.log | head -c "$((MAX_BUILDLOG_SIZE - OFFSET))" || true
            if [ "$(stat --format '%s' build.log)" -ge "3000000" ]
            then
              continue
            fi
          else
            echo "🔍 Truncated at 3000000 bytes, full build log at ${CI_JOB_URL}/artifacts/file/build.log"
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'armv7hl' --offset ${OFFSET} >> build.log || true
            echo "🔍 Last 10000 bytes of build log"
            tail -c 10000 build.log
          fi
        else
          echo "📜 Build log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/armv7hl f51c4465eebde4972aa5d857615aebc7 not available"
        fi
        echo "📒 Check the current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | tee log.txt || true
        echo "🔍 Check the updated package status for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if ! head -n1 log.txt | grep '^1|'
        then
          echo "🏳️ Revision 1 of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl is no longer current"
          break
        fi
        if grep -E '^(succeeded|failed|unresolvable|broken|disabled|excluded) • False' result.txt
        then
          echo "🏁 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/armv7hl reached a final status"
          break
        fi
        echo "⏲️ Wait a bit before checking the status again"
        sleep 30
      done
    - osc api '/build/apertis:v2023dev2:development/default/armv7hl/_jobhistory?package=libcss-tiny-perl' | tee buildhistory.txt
    - |
      if grep -E '^(disabled|excluded)' result.txt
      then
        echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl won't be built on default/armv7hl"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
        exit 0
      fi
    - |
      if ! grep -q '<jobhist\b' buildhistory.txt
      then
        echo "No builds registered, the package may have never been rebuilt since branching"
        if grep ^succeeded result.txt
        then
          echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was imported successfully on default/armv7hl"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
          exit 0
        else
          echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was never built successfully on default/armv7hl"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
          exit 1
        fi
      fi
    - grep 'rev="1"' buildhistory.txt | tail -n1 | tee result.txt
    - |
      if ! grep 'code="succeeded"' result.txt
      then
        echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl didn't succeed on default/armv7hl"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
        exit 1
      fi
    - echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl built successfully on default/armv7hl"
    - echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/armv7hl"
    - osc getbinaries -d results 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'armv7hl'
  artifacts:
    paths:
      - build.log
      - results/
    when: always
    expire_in: 3 days
  rules:
    # NOTE: this is copied from the .rules template at the top of this
    # file, make sure any edits to one or the other stay in sync!
    # This needs to be repeated here to avoid errors:
    # https://gitlab.com/gitlab-org/gitlab/-/issues/276179#note_700305979
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      # do not run on a branch if there's a MR open, run only in MR context so
      # we can derive the release from the target branch
      when: never
    - when: on_success

obs-aarch64-default:
  image: registry.gitlab.apertis.org/infrastructure/apertis-docker-images/v2023dev2-package-source-builder
  stage: build
  tags:
    - monitoring
  timeout: 1d # the LLVM build can take more than 20h
  retry:
    max: 2 # the OBS API can be quite flaky, try again when failing to see if the stars are better aligned
  variables:
    GIT_STRATEGY: none
  script:
    - echo 'https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl' 'default' 'aarch64' '1' 'f51c4465eebde4972aa5d857615aebc7'
    - osc-results () {
        osc results --verbose 'apertis:v2023dev2:development' 'libcss-tiny-perl'
          --repo='default' --arch='aarch64' --csv
          --format='%(code)s • %(dirty)s • %(repository)s • %(arch)s • %(state)s • %(details)s' || true;
      }
    - echo "📒 Current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
    - osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | head -n3 || true
    - touch build.log
    - |
      while true
      do
        echo "🔍 Check the status of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if grep -E '^\S+ • True' result.txt
        then
          echo "⏲️ OBS reported that the status need to be recomputed, wait a bit"
          sleep 30
          continue
        fi
        if osc api '/build/apertis:v2023dev2:development/default/aarch64/libcss-tiny-perl/_log?end=750' | grep "f51c4465eebde4972aa5d857615aebc7"
        then
          OFFSET=$(stat --format '%s' build.log)
          echo "📜 Retrieve the build log at offset ${OFFSET}"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
          if [ "${OFFSET}" -lt "3000000" ]
          then
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'aarch64' --offset ${OFFSET} | tee -a build.log | head -c "$((MAX_BUILDLOG_SIZE - OFFSET))" || true
            if [ "$(stat --format '%s' build.log)" -ge "3000000" ]
            then
              continue
            fi
          else
            echo "🔍 Truncated at 3000000 bytes, full build log at ${CI_JOB_URL}/artifacts/file/build.log"
            osc remotebuildlog 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'aarch64' --offset ${OFFSET} >> build.log || true
            echo "🔍 Last 10000 bytes of build log"
            tail -c 10000 build.log
          fi
        else
          echo "📜 Build log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/aarch64 f51c4465eebde4972aa5d857615aebc7 not available"
        fi
        echo "📒 Check the current revision log for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc log 'apertis:v2023dev2:development' 'libcss-tiny-perl' --csv | tee log.txt || true
        echo "🔍 Check the updated package status for https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl"
        osc-results | tee result.txt
        if ! head -n1 log.txt | grep '^1|'
        then
          echo "🏳️ Revision 1 of https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl is no longer current"
          break
        fi
        if grep -E '^(succeeded|failed|unresolvable|broken|disabled|excluded) • False' result.txt
        then
          echo "🏁 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl default/aarch64 reached a final status"
          break
        fi
        echo "⏲️ Wait a bit before checking the status again"
        sleep 30
      done
    - osc api '/build/apertis:v2023dev2:development/default/aarch64/_jobhistory?package=libcss-tiny-perl' | tee buildhistory.txt
    - |
      if grep -E '^(disabled|excluded)' result.txt
      then
        echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl won't be built on default/aarch64"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
        exit 0
      fi
    - |
      if ! grep -q '<jobhist\b' buildhistory.txt
      then
        echo "No builds registered, the package may have never been rebuilt since branching"
        if grep ^succeeded result.txt
        then
          echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was imported successfully on default/aarch64"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
          exit 0
        else
          echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl was never built successfully on default/aarch64"
          echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
          exit 1
        fi
      fi
    - grep 'rev="1"' buildhistory.txt | tail -n1 | tee result.txt
    - |
      if ! grep 'code="succeeded"' result.txt
      then
        echo "🛑 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl didn't succeed on default/aarch64"
        echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
        exit 1
      fi
    - echo "🌟 Package https://build.collabora.co.uk/package/show/apertis:v2023dev2:development/libcss-tiny-perl built successfully on default/aarch64"
    - echo "  https://build.collabora.co.uk/package/live_build_log/apertis:v2023dev2:development/libcss-tiny-perl/default/aarch64"
    - osc getbinaries -d results 'apertis:v2023dev2:development' 'libcss-tiny-perl' 'default' 'aarch64'
  artifacts:
    paths:
      - build.log
      - results/
    when: always
    expire_in: 3 days
  rules:
    # NOTE: this is copied from the .rules template at the top of this
    # file, make sure any edits to one or the other stay in sync!
    # This needs to be repeated here to avoid errors:
    # https://gitlab.com/gitlab-org/gitlab/-/issues/276179#note_700305979
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      # do not run on a branch if there's a MR open, run only in MR context so
      # we can derive the release from the target branch
      when: never
    - when: on_success

