- job:
    name: linaro-hikey-stable-rc-4.4-merge-and-tag
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 30
    disabled: false
    node: triggers
    display-name: 'Linaro HiKey Stable RC 4.4 Merge-and-Tag'
    scm:
        - git:
            remotes:
                - linux-stable-rc-4.4:
                    url: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git
                    refspec: +refs/heads/linux-4.4.y:refs/remotes/linux-stable-rc-4.4/linux-4.4.y
                - sumit-lts:
                    url: https://git.linaro.org/people/sumit.semwal/linux-lts.git
                    refspec: +refs/heads/lts-4.4.y-hikey:refs/remotes/sumit-lts/lts-4.4.y-hikey
                - arm64-stable-rc-4.4:
                    url: ssh://git@git.linaro.org/lkft/arm64-stable-rc.git
                    refspec: +refs/heads/4.4.y-rc-hikey:refs/remotes/arm64-stable-rc-4.4/4.4.y-rc-hikey
                    credentials-id: '768c1ba1-6052-4b97-a531-7e4fabda6979'
            branches:
                - linux-stable-rc-4.4/linux-4.4.y
            git-config-name: 'Linaro CI'
            git-config-email: 'ci_notify@linaro.org'
            skip-tag: true
            shallow-clone: false
            wipe-workspace: false
    wrappers:
        - timestamps
        - ssh-agent-credentials:
                users:
                    - '768c1ba1-6052-4b97-a531-7e4fabda6979'
        - build-name:
            name: '#${BUILD_NUMBER}-${GIT_REVISION,length=8}'
    builders:
        - shell: |
            #!/bin/bash

            set -e

            git config --global user.name "Linaro CI"
            git config --global user.email "ci_notify@linaro.org"

            # Find out the kernel version and bail out if it is not a RC
            git checkout -B linux-4.4.y linux-stable-rc-4.4/linux-4.4.y
            kernel_version=$(make kernelversion)
            rm -f stable-rc-detected
            case "${kernel_version}" in
              4.4.*-rc*)
                echo "Kernel version: ${kernel_version}"

                # Merge HiKey rebased branch into stable 4.4.y
                git merge --no-edit sumit-lts/lts-4.4.y-hikey

                # Apply tag
                tag=${kernel_version}-hikey-$(date +"%Y%m%d")-${BUILD_NUMBER}
                git tag ${tag}

                # Push tag
                git push arm64-stable-rc-4.4 ${tag}

                touch stable-rc-detected
                ;;
              *)
                echo "Kernel version ${kernel_version} is not a 4.4.y RC."
                ;;
            esac
        - conditional-step:
            condition-kind: file-exists
            condition-filename: stable-rc-detected
            steps:
                - trigger-builds:
                    - project: 'trigger-openembedded-lkft-linaro-hikey-stable-rc-4.4'
    publishers:
        - email-ext:
            recipients: 'sumit.semwal@linaro.org, fathi.boudra@linaro.org, daniel.diaz@linaro.org'
            body: |
                Project: ${PROJECT_NAME}
                Build number: ${BUILD_NUMBER}
                Build status: ${BUILD_STATUS}
                Build URL: ${BUILD_URL}
            always: true
