- job:
    name: trigger-ci-job-configs
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-discover
            everyone-flat:
                - job-read
                - job-extended-read
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 200
    parameters:
        - string:
            name: GERRIT_PATCHSET_REVISION
            default: 'HEAD'
            description: 'Gerrit patchset revision.'
    disabled: false
    node: triggers
    display-name: 'CI job configs deployment'
    scm:
        - git:
            url: https://gitlab.com/LinaroLtd/tcwg/ci/configs
            skip-tag: true
            clean:
                before: true
            basedir: configs
    wrappers:
        - timestamps
        - credentials-binding:
            - text:
                credential-id: JJB_USER
                variable: JJB_USER
        - credentials-binding:
            - text:
                credential-id: JJB_PASSWORD
                variable: JJB_PASSWORD
    builders:
        - shell: |
            #!/bin/bash -e
            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION}" > ${WORKSPACE}/version.txt
        - build-name-setter:
            name: 'version.txt'
            file: true
        - shell: |
            #!/bin/bash

            set -e

            echo ""
            echo "########################################################################"
            echo "    Gerrit Environment"
            env |grep '^GERRIT'
            echo "########################################################################"
            cd configs/

            export GIT_PREVIOUS_COMMIT=$(git rev-parse HEAD~1)
            export GIT_COMMIT=${GERRIT_PATCHSET_REVISION}

            set +e
            KERNEL_TEST_CONFIG=$(git show --pretty="format:" --name-only | grep "kernel-testing-configs/test-params")
            set -e
            if [ ! -z ${KERNEL_TEST_CONFIG} ];then
              source ${KERNEL_TEST_CONFIG}
              export KERNEL_BRANCH KERNEL_REPO DUT EMAIL_ADDRESS
              cd kernel-testing-configs
              ./generate-jjb-configs.sh
              export GIT_PREVIOUS_COMMIT=$(git rev-parse HEAD~1)
              export GIT_COMMIT=$(git rev-parse HEAD)
              cd ../
            fi

            jenkins-jobs --version
            python3 run-jjb.py
    publishers:
        - email:
            recipients: 'laurent.alfonsi@linaro.org'
