Add tf-a-lts-automation.yaml

The tf-a-lts-automation job is triggered by new LTS branch
creation in trusted-firmware-a repository.
It helps to create corrosponding branch in the
tf-a-test, hafnium, and tf-a-ci-scripts repositories,
as well as set up a new download space for the new LTS branch

Change-Id: I9d5c218381baff08ddb6757c8e4cdc1f1b3bcba8
Signed-off-by: Arthur She <arthur.she@linaro.org>
diff --git a/tf-a-lts-automation.yaml b/tf-a-lts-automation.yaml
new file mode 100644
index 0000000..5abfff3
--- /dev/null
+++ b/tf-a-lts-automation.yaml
@@ -0,0 +1,106 @@
+- job:
+    name: tf-a-lts-automation
+    node: build-amd64-private
+    project-type: freestyle
+    disabled: false
+    defaults: global
+    description: |
+      Trusted Firmware A LTS automation, this job triggered by a new LTS branch created in the trusted-firmware-a repository.
+      It helps to create corrosponding branch in the tf-a-test, hafnium, and tf-a-ci-scripts repositories, as well as set up a new download
+      space for the new LTS branch.
+    properties:
+        - build-discarder:
+            days-to-keep: 15
+    parameters:
+        - string:
+            name: EMAIL_RECIPIENT
+            default: |
+              bipin.ravi@arm.com, vwadekar@nvidia.com, yann.gautier@st.com, palmer@google.com, govindraj.raja@arm.com arthur.she@linaro.org
+            description: |
+              The notification recipients
+    wrappers:
+        - timestamps
+        - timeout:
+            timeout: 120
+            fail: true
+        - credentials-binding:
+            - ssh-user-private-key:
+                credential-id: TFA_CI_BOT_USER_SSH_KEY
+                key-file-variable: CI_BOT_KEY
+                username-variable: CI_BOT_USERNAME
+                passphrase-variable: ''
+    triggers:
+        - gerrit:
+            silent: true
+            server-name: 'review.trustedfirmware.org'
+            projects:
+            - branches:
+                - branch-compare-type: REG_EXP
+                  branch-pattern: 'lts-v.*'
+              project-compare-type: PLAIN
+              project-pattern: 'TF-A/trusted-firmware-a'
+            trigger-on:
+            - ref-updated-event
+
+    builders:
+        - shell: |
+            echo "***************************************"
+            env | grep GERRIT
+            echo "***************************************"
+        - shell: |
+            #!/bin/bash
+            set -ex
+
+            SSH_PARAMS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PubkeyAcceptedKeyTypes=+ssh-rsa"
+            export GIT_SSH_COMMAND="ssh -p 29418 -i $CI_BOT_KEY ${SSH_PARAMS}"
+            gerrit_host="ssh://${CI_BOT_USERNAME}@${GERRIT_HOST}:29418"
+            new_branch=${GERRIT_REFNAME}
+            forked_tag=${new_branch##*-}
+
+            _workdir=$(mktemp -d)
+            rm -f email.txt || true
+            trap '[ -d "${_workdir}" ] && rm -rf "${_workdir}"' EXIT
+            git -C "${_workdir}" init > /dev/null 2>&1
+
+            # Check if the job is triggered by a new LTS branch creation
+            new_lts_branch=1
+            git -C "${_workdir}" fetch ${gerrit_host}/${GERRIT_PROJECT} ${new_branch} || new_lts_branch=-1
+            if [ ${new_lts_branch} -eq -1 ]; then
+                echo "No new LTS branch created! exit job"
+            	exit 0
+            fi
+
+            # Create new LTS branch to related repositories
+            repos="hafnium TF-A/tf-a-tests ci/tf-a-ci-scripts"
+            for r in ${repos};
+            do
+                echo "Create branch \"${new_branch}\" from tag \"${forked_tag}\" in the \"${r}\" project"
+                git -C "${_workdir}" fetch ${gerrit_host}/${r} ${forked_tag}
+                git -C "${_workdir}" push ${gerrit_host}/${r} FETCH_HEAD^{commit}:refs/heads/${new_branch}
+            done
+
+            # Create new download space
+            echo "Create new download space for \"${new_branch}\""
+            aws s3 sync s3://trustedfirmware-prod-storage/tf-a s3://trustedfirmware-prod-storage/tf-a-${new_branch}
+
+            cat << EOF >email.txt
+            Hello,
+            The new LTS branch "${new_branch}" has been created in the following repositoris
+            ${repos}
+
+            And the new download space is ready on https://downloads.trustedfirmware.org/tf-a-${new_branch}
+            EOF
+
+            echo "--------- email body ----------"
+            cat email.txt
+
+    publishers:
+      - conditional-publisher:
+          - condition-kind: file-exists
+            condition-filename: email.txt
+            condition-basedir: workspace
+            action:
+              - email:
+                  recipients: ${EMAIL_RECIPIENT}
+                  subject: "Build Success: ${JOB_NAME} #${BUILD_NUMBER}"
+                  body: ${FILE,path="email.txt"}