tfa-next: add a cleanup job for S3 bucket files that are >3 days old

Create a new 'tf-a-cleanup-tfa-next' job that removes all files
that are older than 3 days from tfa-next's S3 bucket.

This job will be triggered on each Friday at 11 am.

Change-Id: I66b4fac119c19d475a31dac7a3a2d8980184e8a7
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
diff --git a/tf-a-cleanup-tfa-next.yaml b/tf-a-cleanup-tfa-next.yaml
new file mode 100644
index 0000000..7bc8658
--- /dev/null
+++ b/tf-a-cleanup-tfa-next.yaml
@@ -0,0 +1,49 @@
+- job:
+    name: tf-a-cleanup-tfa-next
+    description: Cleanup https://downloads.trustedfirmware.org/tf-a/next/artifacts/
+    node: build-amd64-private
+    concurrent: false
+    disabled: false
+    project-type: freestyle
+    sandbox: true
+    properties:
+    - build-discarder:
+        days-to-keep: 90
+        num-to-keep: 15
+    triggers:
+    - timed: "0 11 * * 5" # Triggered every Friday at 11 am (GMT)
+    builders:
+        - shell: |
+            #!/bin/bash
+            set -ex
+            if ! type aws
+            then
+                sudo apt-get -y -qq update
+                sudo apt-get -y -qq install --no-install-recommends unzip
+                curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
+                unzip -q awscliv2.zip
+                sudo ./aws/install
+            fi
+
+            aws configure list
+            ECR=987685672616.dkr.ecr.us-east-1.amazonaws.com
+            aws ecr get-login-password --region us-east-1|docker login --username AWS --password-stdin $ECR
+
+            # All tfa-next related files should be under "artifacts" on the S3 bucket.
+            ROOT=s3://trustedfirmware-prod-storage/
+            for OBJECT in $(aws s3 ls $ROOT --recursive | grep artifacts | awk '{print $4}');
+            do
+                echo "Current object: ${ROOT}${OBJECT}"
+                # Expected URL format: tf-a/next/artifacts/YYYY-MM-DD-HHMMSS/<bin_mode>/<file_name>
+                # Take the YYYY-MM-DD information from the URL for each object
+                dir_date=$(echo $OBJECT | sed 's+tf-a/next/artifacts/++' | sed 's+/.*++' | grep -E "(([0-9]+-){3,}[0-9]+)" | sed -E 's/-[0-9]+$//')
+                if [ $dir_date ]; then
+                    # Calculate how old the file is (in days)
+                    diff_in_days=$(( ( $(date +%s) - $(date -d "$dir_date" +%s) ) / $((60*60*24)) ))
+                    # Remove the file if it's older than 3 days.
+                    if (( diff_in_days > 3 )); then
+                        echo "Removing object: ${ROOT}${OBJECT}"
+                        aws s3 rm ${ROOT}${OBJECT}
+                    fi
+                fi
+            done