blob: e8e55677b561a3fc7cbe5ada1c91d5da02f51226 [file] [log] [blame]
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -05001#!/usr/bin/env bash
2#
Mikael Olssonfb99d0a2023-03-09 14:58:48 +01003# Copyright (c) 2021-2023 Arm Limited. All rights reserved.
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -05004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7# Clones and checkout TF-A related repositories in case these are not present
8# under SHARE_FOLDER, otherwise copy the share repositories into current folder
9# (workspace)
10
11# The way it works is simple: the top level job sets the SHARE_FOLDER
12# parameter based on its name and number on top of the share
13# volume (/srv/shared/<job name>/<job number>) then it calls the clone
14# script (clone.sh), which in turn it fetches the repositories mentioned
15# above. Jobs triggered on behalf of the latter, share the same
16# SHARE_FOLDER value, and these in turn also call the clone script, but
17# in this case, the script detects that the folder is already populated so
18# its role is to simply copy the repositories into the job's
19# workspace. As seen, all jobs work with repositories on their own
20# workspace, which are just copies of the share folder, so there is no
21# change of a race condition, i.e every job works with its own copy. The
22# worst case scenario is where the down-level job, tf-a-builder, uses its
23# default SHARE_FOLDER value, in this case, it would simply clone its
24# own repositories without reusing any file however the current approach
25# prevents the latter unless the job is triggered manually from the
26# builder job itself.
27
Chris Kayb0392862022-12-23 13:48:01 +000028set -ex
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050029
30# Global defaults
31REFSPEC_MASTER="refs/heads/master"
32GIT_REPO="https://git.trustedfirmware.org"
33GERRIT_HOST="https://review.trustedfirmware.org"
Leonardo Sandoval49a18a42021-09-26 19:48:53 -050034GIT_CLONE_PARAMS=""
Paul Sokolovskyd6b76192023-06-20 16:18:35 +030035SSH_PARAMS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -p 29418 -i ${CI_BOT_KEY}"
Arthur She3eb254e2022-09-18 20:41:03 -070036GERRIT_QUERY_PARAMS="--format=JSON --patch-sets --current-patch-set status:open"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050037
38# Defaults Projects
Arthur She3eb254e2022-09-18 20:41:03 -070039TF_GERRIT_PROJECT="${TF_GERRIT_PROJECT:-TF-A/trusted-firmware-a}"
Jimmy Brisson2fbc06c2023-06-09 08:49:09 -050040TF_M_TESTS_GERRIT_PROJECT="${TF_M_TESTS_GERRIT_PROJECT:-TF-M/tf-m-tests}"
41TF_M_EXTRAS_GERRIT_PROJECT="${TF_M_EXTRAS_GERRIT_PROJECT:-TF-M/tf-m-extras}"
Arthur She3eb254e2022-09-18 20:41:03 -070042TFTF_GERRIT_PROJECT="${TFTF_GERRIT_PROJECT:-TF-A/tf-a-tests}"
Daniel Boulby9dbe6f12023-11-30 15:29:10 +000043SPM_GERRIT_PROJECT="${SPM_GERRIT_PROJECT:-hafnium/hafnium}"
Arthur She3eb254e2022-09-18 20:41:03 -070044CI_GERRIT_PROJECT="${CI_GERRIT_PROJECT:-ci/tf-a-ci-scripts}"
45JOBS_PROJECT="${JOBS_PROJECT:-ci/tf-a-job-configs.git}"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050046
47# Default Reference specs
48TF_GERRIT_REFSPEC="${TF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}"
49TFTF_GERRIT_REFSPEC="${TFTF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}"
Daniel Boulby9dbe6f12023-11-30 15:29:10 +000050SPM_REFSPEC="${SPM_REFSPEC:-${REFSPEC_MASTER}}"
Jimmy Brisson2bb494c2023-05-30 18:15:48 -050051TF_M_TESTS_GERRIT_REFSPEC="${TF_M_TESTS_GERRIT_REFSPEC:-${REFSPEC_MASTER}}"
52TF_M_EXTRAS_GERRIT_REFSPEC="${TF_M_EXTRAS_GERRIT_REFSPEC:-${REFSPEC_MASTER}}"
Zelalem Awekee8801df2021-10-25 13:41:44 -050053CI_REFSPEC="${CI_REFSPEC:-${REFSPEC_MASTER}}"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050054JOBS_REFSPEC="${JOBS_REFSPEC:-${REFSPEC_MASTER}}"
55
Arthur She3eb254e2022-09-18 20:41:03 -070056JOBS_REPO_NAME="tf-a-job-configs"
57
58# Array containing "<repo host>;<project>;<repo name>;<refspec>" elements
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050059repos=(
Arthur She3eb254e2022-09-18 20:41:03 -070060 "${GERRIT_HOST};${CI_GERRIT_PROJECT};tf-a-ci-scripts;${CI_REFSPEC}"
61 "${GERRIT_HOST};${TF_GERRIT_PROJECT};trusted-firmware-a;${TF_GERRIT_REFSPEC}"
62 "${GERRIT_HOST};${TFTF_GERRIT_PROJECT};tf-a-tests;${TFTF_GERRIT_REFSPEC}"
Daniel Boulbyc61b6cd2023-12-01 11:06:45 +000063 "${GERRIT_HOST};${SPM_GERRIT_PROJECT};spm;${SPM_REFSPEC}"
Jimmy Brissone4486ec2023-06-07 09:20:27 -050064 "${GERRIT_HOST};${TF_M_TESTS_GERRIT_PROJECT};tf-m-tests;${TF_M_TESTS_GERRIT_REFSPEC}"
65 "${GERRIT_HOST};${TF_M_EXTRAS_GERRIT_PROJECT};tf-m-extras;${TF_M_EXTRAS_GERRIT_REFSPEC}"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050066)
67
Paul Sokolovsky9b2a5de2023-12-18 18:25:43 +030068df -h
69
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050070# Take into consideration non-CI runs where SHARE_FOLDER variable
71# may not be present
72if [ -z "${SHARE_FOLDER}" ]; then
73 # Default Jenkins values
74 SHARE_VOLUME="${SHARE_VOLUME:-$PWD}"
75 JOB_NAME="${JOB_NAME:-local}"
76 BUILD_NUMBER="${BUILD_NUMBER:-0}"
77 SHARE_FOLDER=${SHARE_VOLUME}/${JOB_NAME}/${BUILD_NUMBER}
78fi
79
Arthur She3eb254e2022-09-18 20:41:03 -070080# Clone JOBS_PROJECT first, since we need a helper script there
81if [ ! -d ${SHARE_FOLDER}/${JOBS_REPO_NAME} ]; then
82 git clone ${GIT_CLONE_PARAMS} ${GIT_REPO}/${JOBS_PROJECT} ${SHARE_FOLDER}/${JOBS_REPO_NAME}
83 cd ${SHARE_FOLDER}/${JOBS_REPO_NAME}
84 git fetch origin ${JOBS_REFSPEC}
85else
86 cd ${SHARE_FOLDER}/${JOBS_REPO_NAME}
87fi
88git log -1
89cd $OLDPWD
90cp -a -f ${SHARE_FOLDER}/${JOBS_REPO_NAME} ${PWD}/${JOBS_REPO_NAME}
91
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -050092# clone git repos
93for repo in ${repos[@]}; do
94
95 # parse the repo elements
Arthur She3eb254e2022-09-18 20:41:03 -070096 REPO_HOST="$(echo "${repo}" | awk -F ';' '{print $1}')"
97 REPO_PROJECT="$(echo "${repo}" | awk -F ';' '{print $2}')"
98 REPO_NAME="$(echo "${repo}" | awk -F ';' '{print $3}')"
99 REPO_DEFAULT_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $4}')"
100 REPO_URL="${REPO_HOST}/${REPO_PROJECT}"
101 REPO_REFSPEC="${REPO_DEFAULT_REFSPEC}"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -0500102
Arthur She3eb254e2022-09-18 20:41:03 -0700103 # clone and checkout in case it does not exist
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -0500104 if [ ! -d ${SHARE_FOLDER}/${REPO_NAME} ]; then
105 git clone ${GIT_CLONE_PARAMS} ${REPO_URL} ${SHARE_FOLDER}/${REPO_NAME}
106
Mikael Olssonfb99d0a2023-03-09 14:58:48 +0100107 # If the Gerrit review that triggered the CI had a topic, it will be used to synchronize the other repositories
108 if [ -n "${GERRIT_TOPIC}" -a "${REPO_HOST}" = "${GERRIT_HOST}" -a "${GERRIT_PROJECT}" != "${REPO_PROJECT}" ]; then
Arthur She3eb254e2022-09-18 20:41:03 -0700109 echo "Got Gerrit Topic: ${GERRIT_TOPIC}"
110 REPO_REFSPEC="$(ssh ${SSH_PARAMS} ${CI_BOT_USERNAME}@${REPO_HOST#https://} gerrit query ${GERRIT_QUERY_PARAMS} \
Paul Sokolovsky65bc87b2023-06-20 16:00:36 +0300111 project:${REPO_PROJECT} topic:${GERRIT_TOPIC} | ${SHARE_FOLDER}/${JOBS_REPO_NAME}/scripts/parse_refspec.py)"
Arthur She3eb254e2022-09-18 20:41:03 -0700112 if [ -z "${REPO_REFSPEC}" ]; then
113 REPO_REFSPEC="${REPO_DEFAULT_REFSPEC}"
114 echo "Roll back to \"${REPO_REFSPEC}\" for \"${REPO_PROJECT}\""
115 fi
116 echo "Checkout refspec \"${REPO_REFSPEC}\" from repository \"${REPO_NAME}\""
117 fi
118
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -0500119 # fetch and checkout the corresponding refspec
120 cd ${SHARE_FOLDER}/${REPO_NAME}
121 git fetch ${REPO_URL} ${REPO_REFSPEC}
122 git checkout FETCH_HEAD
Paul Sokolovskyb41ac9e2022-10-14 19:23:26 +0300123 echo "Freshly cloned ${REPO_URL} (refspec ${REPO_REFSPEC}):"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -0500124 git log -1
125 cd $OLDPWD
126
127 else
128 # otherwise just show the head's log
129 cd ${SHARE_FOLDER}/${REPO_NAME}
Paul Sokolovskyb41ac9e2022-10-14 19:23:26 +0300130 echo "Using existing shared folder checkout for ${REPO_URL}:"
Leonardo Sandovald1b6b5a2021-09-13 12:06:26 -0500131 git log -1
132 cd $OLDPWD
133 fi
134
135 # copy repository into pwd dir (workspace in CI), so each job would work
136 # on its own workspace
137 cp -a -f ${SHARE_FOLDER}/${REPO_NAME} ${PWD}/${REPO_NAME}
138
139done