- job:
    name: zephyr-upstream
    project-type: matrix
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
                - job-workspace
            everyone-flat:
                - job-read
                - job-extended-read
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 60
            num-to-keep: 60
            artifact-num-to-keep: 60
        - throttle:
            option: category
            categories: [lite-zephyr]
            matrix-builds: false
            matrix-configs: true
    parameters:
        - string:
            name: BRANCH
            default: 'main'
        - string:
            name: LAVA_SERVER
            default: 'validation.linaro.org/RPC2/'
        - string:
            name: QA_SERVER
            default: 'https://qa-reports.linaro.org'
        - string:
            name: GIT_COMMIT
            default: 'HEAD'
        - string:
            name: TWISTER_EXTRA
            default: '--enable-slow -x=BUILD_VERSION=ci_build_version'
    disabled: true
    node: master
    child-workspace: .
    display-name: 'DELETE ME'
# We should avoid starting this job "on its own". That's because default
# Git revision it builds is main/HEAD, and as this is a matrix job,
# different sub-builds may get different actual HEAD revision (e.g. if
# the tree is actively committed to). Instead, it should be triggered
# externally, with GIT_COMMIT parameter set to a specific SHA1 revision
# to build. This is achieved using "trigger-zephyr-upstream" trigger
# job.
#    triggers:
#        - timed: 'H H/12 * * *'
    axes:
        - axis:
            type: slave
            name: label
            values:
                - docker-focal-amd64-lite
        - axis:
            type: user-defined
            name: ZEPHYR_TOOLCHAIN_VARIANT
            values:
                - gnuarmemb
                - zephyr
        - axis:
            type: user-defined
            name: PLATFORM
            values:
                # Jenkins runs these configurations in non-obvious, but
                # mostly stable manner. Configurations are ordered to
                # get frdm_k64f built first, as it's both a "default"
                # test target, and easily contains more tests than other
                # targets (== could make whole job longer if run last).
                - disco_l475_iot1
                - frdm_kw41z
                - lpcxpresso55s69_cpu0
                - mps2_an385
                - nucleo_f103rb
                - frdm_k64f
                - nucleo_f401re
                - qemu_cortex_m3
    execution-strategy:
        combination-filter: |
             ZEPHYR_TOOLCHAIN_VARIANT=="zephyr" || (ZEPHYR_TOOLCHAIN_VARIANT=="gnuarmemb" && !(PLATFORM=="arduino_101" || PLATFORM=="qemu_x86"))
    wrappers:
        - timestamps
        - credentials-binding:
            - text:
                credential-id: LAVA_USER
                variable: LAVA_USER
        - credentials-binding:
            - text:
                credential-id: LAVA_TOKEN
                variable: LAVA_TOKEN
        - credentials-binding:
            - text:
                credential-id: QA_REPORTS_TOKEN
                variable: QA_REPORTS_TOKEN
    builders:
        - shell:
            !include-raw: lite-common/header.sh
        - shell:
            !include-raw: lite-common/install-toolchains.sh
        - shell:
            !include-raw: zephyr-upstream/builders.sh
        - inject:
            properties-file: env_var_parameters
        - build-name-setter:
            template: '#${BUILD_NUMBER}-${GIT_COMMIT_ID}'
            macro: true
        - linaro-publish-token
        - inject:
            properties-file: env_var_parameters
        - shell: |
            #!/bin/bash

            # Publish
            test -d ${HOME}/bin || mkdir ${HOME}/bin
            wget -q https://git.linaro.org/ci/publishing-api.git/blob_plain/HEAD:/linaro-cp.py -O ${HOME}/bin/linaro-cp.py
            time python3 ${HOME}/bin/linaro-cp.py \
              --api_version 3 \
              out/${PLATFORM} components/kernel/zephyr/${BRANCH}/${ZEPHYR_TOOLCHAIN_VARIANT}/${PLATFORM}/${BUILD_NUMBER}
            echo "Build artifacts download URL: https://snapshots.linaro.org/components/kernel/zephyr/${BRANCH}/${ZEPHYR_TOOLCHAIN_VARIANT}/${PLATFORM}/${BUILD_NUMBER}/"

        - shell: |
            #!/bin/bash -ex

            # Remap Zephyr platform/board name to LAVA device type name.
            case "${PLATFORM}" in
              frdm_k64f)
                export DEVICE_TYPE=frdm-k64f
                ;;
              frdm_kw41z)
                export DEVICE_TYPE=frdm-kw41z
                ;;
              disco_l475_iot1)
                export DEVICE_TYPE=disco-l475-iot1
                ;;
              lpcxpresso55s69_cpu0)
                export DEVICE_TYPE=lpcxpresso55s69
                #LAVA_SERVER="staging.validation.linaro.org/RPC2/"
                ;;
              qemu_cortex_m3)
                export DEVICE_TYPE=qemu
                ;;
              *)
                echo "Skipping LAVA testing for ${PLATFORM} (no LAVA device type mapping)"
                ;;
            esac


            # 2021-04-28 - try to submit all (== ~2 builds per day) to LAVA.
            #if [ "${ROOT_BUILD_CAUSE}" = "SCMTRIGGER" ]; then
            #    echo "Skipping LAVA testing for SCM Trigger builds (to not overload LAVA)"
            #    unset DEVICE_TYPE
            #fi

            if [ -n "${DEVICE_TYPE}" ]; then
                rm -rf configs
                git clone --depth 1 --single-branch http://git.linaro.org/ci/job/configs.git

                python3 configs/zephyr-upstream/submit_for_testing.py \
                --board-name ${PLATFORM} \
                --device-type ${DEVICE_TYPE} \
                --build-number ${BUILD_NUMBER} \
                --build-url ${BUILD_URL} \
                --branch-name ${BRANCH} \
                --lava-server ${LAVA_SERVER} \
                --qa-server ${QA_SERVER} \
                --qa-server-team lite \
                --qa-server-project zephyr-upstream \
                --gcc-variant ${ZEPHYR_TOOLCHAIN_VARIANT} \
                --git-commit ${GIT_COMMIT_ID}
            fi

    publishers:
        - email:
            recipients: 'fathi.boudra@linaro.org erwan.gouriou@linaro.org'
