- job:
    name: armnn-ci-build
    display-name: 'Arm NN - CI Build'
    concurrent: true
    project-type: freestyle
    defaults: global
    description: |
        Updates source and builds ArmNN frequently to ensure builds are successful.
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
            everyone-flat:
                - job-read
                - job-extended-read
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 90
            num-to-keep: 90
        - github:
            url: https://git.mlplatform.org/ml/armnn.git

    triggers:
        - gerrit:
            override-votes: true
            gerrit-build-failed-verified-value: -1
            trigger-on:
                - patchset-created-event:
                    exclude-drafts: true
                    exclude-trivial-rebase: true
                    exclude-no-code-change: true
                - comment-added-contains-event:
                    comment-contains-value: 'TRIGGER BUILD'
            projects:
                - project-compare-type: 'PLAIN'
                  project-pattern: 'ml/armnn'
                  branches:
                    - branch-compare-type: 'PLAIN'
                      branch-pattern: 'master'
             #silent: true
            skip-vote:
                  successful: true
                  failed: false
            server-name: review.mlplatform.org

    parameters:
        - string:
            name: LAVA_SERVER
            default: 'https://validation.linaro.org/RPC2/'
        - string:
            name: GIT_COM_ID
    disabled: false
    node: docker-bionic-arm64-armnn

    wrappers:
        - timestamps
        - credentials-binding:
            - text:
                credential-id: QA_REPORTS_TOKEN
                variable: QA_REPORTS_TOKEN
    builders:
        - shell: |
            git clone https://github.com/K1504296/armnn-ci.git
            cd armnn-ci && bash -ex arm-builders.sh
        - shell: |
            #!/bin/bash -e
            echo "DEVICE_TYPE=${DEVICE_TYPE}" > device_type_parameters
        - inject:
            properties-file: device_type_parameters
        - linaro-publish-token
        - shell: |
            #!/bin/bash

            set -ex

            sudo apt-get install -y jq

            if [ -z "$GIT_COM_ID" ]
            then
                LAVA_ID=${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}
                ARMNN_TAR_LOCATION=https://snapshots.linaro.org/components/armnn/${BUILD_NUMBER}/armnn.tar.xz
            else
                LAVA_ID=$GIT_COM_ID
                ARMNN_TAR_LOCATION=https://snapshots.linaro.org/components/armnn-master/${BUILD_NUMBER}/armnn.tar.xz
            fi


            jq -n --arg 'LAVA_ID' $LAVA_ID \
            --arg 'ARMNN_TAR_LOCATION' $ARMNN_TAR_LOCATION \
            --arg 'BUILD_NUMBER' $BUILD_NUMBER \
            '{LAVA_ID:$LAVA_ID, ARMNN_TAR_LOCATION:$ARMNN_TAR_LOCATION, BUILD_NUMBER:$BUILD_NUMBER}' > out/buildInfo.json


            # Publish
            test -d ${HOME}/bin || mkdir ${HOME}/bin


            if [ -z "$GIT_COM_ID" ]
            then
                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 \
                --link-latest \
                out/ components/armnn/${BUILD_NUMBER}
            else
                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 \
                --link-latest \
                out/ components/armnn-master/${BUILD_NUMBER}
            fi

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

            export DEVICE_TYPE=hi960-hikey

            if [ -z "$GIT_COM_ID" ]
            then
                LAVA_ID=${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}
                ARMNN_TAR_LOCATION=https://snapshots.linaro.org/components/armnn/${BUILD_NUMBER}/armnn.tar.xz
            else
                LAVA_ID=$GIT_COM_ID
                ARMNN_TAR_LOCATION=https://snapshots.linaro.org/components/armnn-master/${BUILD_NUMBER}/armnn.tar.xz
            fi

            cat << EOF > ${WORKSPACE}/submit_for_testing_parameters
            LAVA_JOB_PRIORITY=50
            PROJECT_NAME=armnn
            PROJECT=projects/armnn/
            ARMNN_TARBALL_URL=${ARMNN_TAR_LOCATION}
            BOOT_URL=http://snapshots.linaro.org/96boards/hikey/linaro/debian/30/boot-linaro-stretch-developer-hikey-20190420-30.img.gz
            BOOT_URL_COMP=gz
            ROOTFS_URL=http://snapshots.linaro.org/96boards/hikey/linaro/debian/30/rootfs-linaro-stretch-developer-hikey-20190420-30.img.gz
            ROOTFS_URL_COMP=gz
            DEPLOY_OS=debian
            OS_INFO=debian
            DEVICE_TYPE=${DEVICE_TYPE}
            BUILD_NUMBER=${BUILD_NUMBER}
            EOF

            git clone --depth 1 https://github.com/Linaro/lava-test-plans.git

            curl \
                --header "Auth-Token: $QA_REPORTS_TOKEN" \
                --form patch_source=gerrit-ssh-mlplatform \
                --form patch_id=${GERRIT_CHANGE_NUMBER}:${GERRIT_PATCHSET_NUMBER} \
                https://qa-reports.linaro.org/api/createbuild/armnn/armnn/$LAVA_ID

            # submit tests to LAVA
            rm -rf venv | true
            virtualenv -p python3 venv
            source ./venv/bin/activate

            # fix for setuptools v50
            export SETUPTOOLS_USE_DISTUTILS="stdlib"
            cd lava-test-plans
            pip3 install -r requirements.txt
            ./submit_for_testing.py --variables ${WORKSPACE}/submit_for_testing_parameters \
              --device-type ${DEVICE_TYPE} \
              --test-plan armnn \
              --qa-server-team armnn \
              --qa-server-project armnn \
              --lava-server ${LAVA_SERVER} \
              --build-id ${LAVA_ID}

            export DEVICE_TYPE=synquacer
            cat << EOF > ${WORKSPACE}/submit_for_testing_parameters
            LAVA_JOB_PRIORITY=50
            PROJECT_NAME=armnn
            PROJECT=projects/armnn/
            AUTO_LOGIN_PASSWORD_PROMPT='Password:'
            AUTO_LOGIN_PASSWORD='Linaro123'
            ARMNN_TARBALL_URL=${ARMNN_TAR_LOCATION}
            KERNEL_URL=http://images.validation.linaro.org/synquacer/hc/vmlinuz
            RAMDISK_URL=http://images.validation.linaro.org/synquacer/hc/initrd.img
            RAMDISK_URL_COMP=gz
            ROOTFS_URL=http://images.validation.linaro.org/synquacer/hc/rootfs.tar.xz
            ROOTFS_URL_COMP=xz
            DEPLOY_OS=debian
            OS_INFO=debian
            DEVICE_TYPE=${DEVICE_TYPE}
            BUILD_NUMBER=${BUILD_NUMBER}
            EOF

            ./submit_for_testing.py --variables ${WORKSPACE}/submit_for_testing_parameters \
              --device-type ${DEVICE_TYPE} \
              --test-plan armnn \
              --qa-server-team armnn \
              --qa-server-project armnn \
              --lava-server ${LAVA_SERVER} \
              --build-id ${LAVA_ID}

            export DEVICE_TYPE=dragonboard-845c

            cat << EOF > ${WORKSPACE}/submit_for_testing_parameters
            LAVA_JOB_PRIORITY=50
            PROJECT_NAME=armnn
            PROJECT=projects/armnn/
            ARMNN_TARBALL_URL=${ARMNN_TAR_LOCATION}
            PTABLE_URL=https://images.validation.linaro.org/snapshots.linaro.org/96boards/dragonboard845c/linaro/rescue/28/dragonboard-845c-bootloader-ufs-linux-28/gpt_both0.bin
            BOOT_URL=http://images.validation.linaro.org/releases.linaro.org/96boards/dragonboard845c/linaro/debian/19.09/boot-linaro-buster-dragonboard-845c-58.img.gz
            BOOT_URL_COMP=gz
            ROOTFS_URL=http://images.validation.linaro.org/releases.linaro.org/96boards/dragonboard845c/linaro/debian/19.09/linaro-buster-alip-dragonboard-845c-58.img.gz
            ROOTFS_URL_COMP=gz
            DEPLOY_OS=debian
            OS_INFO=debian
            DEVICE_TYPE=${DEVICE_TYPE}
            BUILD_NUMBER=${BUILD_NUMBER}
            EOF

            ./submit_for_testing.py --variables ${WORKSPACE}/submit_for_testing_parameters \
              --device-type ${DEVICE_TYPE} \
              --test-plan armnn \
              --qa-server-team armnn \
              --qa-server-project armnn \
              --lava-server ${LAVA_SERVER} \
              --build-id ${LAVA_ID}
