Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 3 | # Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 4 | # |
| 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 | |
| 28 | set -e |
| 29 | |
Paul Sokolovsky | 54f0ea2 | 2021-11-17 21:51:02 +0300 | [diff] [blame] | 30 | # WORKAROUND START |
| 31 | # Install last-minute dependencies. This should not be done like that, |
| 32 | # instead all dependencies should go into the build docker image. But |
| 33 | # to unbreak urgent regressions, some packages may be installed here |
| 34 | # until they're moved to the docker image. |
| 35 | sudo apt update |
| 36 | sudo apt-get install -y python3-venv |
| 37 | # WORKAROUND END |
| 38 | |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 39 | # Global defaults |
| 40 | REFSPEC_MASTER="refs/heads/master" |
| 41 | GIT_REPO="https://git.trustedfirmware.org" |
| 42 | GERRIT_HOST="https://review.trustedfirmware.org" |
Leonardo Sandoval | 49a18a4 | 2021-09-26 19:48:53 -0500 | [diff] [blame] | 43 | GIT_CLONE_PARAMS="" |
Arthur She | f0eb5e7 | 2022-10-11 08:19:36 -0700 | [diff] [blame] | 44 | SSH_PARAMS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 29418 -i ${CI_BOT_KEY}" |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 45 | GERRIT_QUERY_PARAMS="--format=JSON --patch-sets --current-patch-set status:open" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 46 | |
| 47 | # Defaults Projects |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 48 | TF_GERRIT_PROJECT="${TF_GERRIT_PROJECT:-TF-A/trusted-firmware-a}" |
| 49 | TFTF_GERRIT_PROJECT="${TFTF_GERRIT_PROJECT:-TF-A/tf-a-tests}" |
| 50 | CI_GERRIT_PROJECT="${CI_GERRIT_PROJECT:-ci/tf-a-ci-scripts}" |
| 51 | JOBS_PROJECT="${JOBS_PROJECT:-ci/tf-a-job-configs.git}" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 52 | |
| 53 | # Default Reference specs |
| 54 | TF_GERRIT_REFSPEC="${TF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}" |
| 55 | TFTF_GERRIT_REFSPEC="${TFTF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}" |
Zelalem Aweke | e8801df | 2021-10-25 13:41:44 -0500 | [diff] [blame] | 56 | CI_REFSPEC="${CI_REFSPEC:-${REFSPEC_MASTER}}" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 57 | JOBS_REFSPEC="${JOBS_REFSPEC:-${REFSPEC_MASTER}}" |
| 58 | |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 59 | JOBS_REPO_NAME="tf-a-job-configs" |
| 60 | |
| 61 | # Array containing "<repo host>;<project>;<repo name>;<refspec>" elements |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 62 | repos=( |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 63 | "${GERRIT_HOST};${CI_GERRIT_PROJECT};tf-a-ci-scripts;${CI_REFSPEC}" |
| 64 | "${GERRIT_HOST};${TF_GERRIT_PROJECT};trusted-firmware-a;${TF_GERRIT_REFSPEC}" |
| 65 | "${GERRIT_HOST};${TFTF_GERRIT_PROJECT};tf-a-tests;${TFTF_GERRIT_REFSPEC}" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 66 | ) |
| 67 | |
| 68 | # Take into consideration non-CI runs where SHARE_FOLDER variable |
| 69 | # may not be present |
| 70 | if [ -z "${SHARE_FOLDER}" ]; then |
| 71 | # Default Jenkins values |
| 72 | SHARE_VOLUME="${SHARE_VOLUME:-$PWD}" |
| 73 | JOB_NAME="${JOB_NAME:-local}" |
| 74 | BUILD_NUMBER="${BUILD_NUMBER:-0}" |
| 75 | SHARE_FOLDER=${SHARE_VOLUME}/${JOB_NAME}/${BUILD_NUMBER} |
| 76 | fi |
| 77 | |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 78 | # Clone JOBS_PROJECT first, since we need a helper script there |
| 79 | if [ ! -d ${SHARE_FOLDER}/${JOBS_REPO_NAME} ]; then |
| 80 | git clone ${GIT_CLONE_PARAMS} ${GIT_REPO}/${JOBS_PROJECT} ${SHARE_FOLDER}/${JOBS_REPO_NAME} |
| 81 | cd ${SHARE_FOLDER}/${JOBS_REPO_NAME} |
| 82 | git fetch origin ${JOBS_REFSPEC} |
| 83 | else |
| 84 | cd ${SHARE_FOLDER}/${JOBS_REPO_NAME} |
| 85 | fi |
| 86 | git log -1 |
| 87 | cd $OLDPWD |
| 88 | cp -a -f ${SHARE_FOLDER}/${JOBS_REPO_NAME} ${PWD}/${JOBS_REPO_NAME} |
| 89 | |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 90 | # clone git repos |
| 91 | for repo in ${repos[@]}; do |
| 92 | |
| 93 | # parse the repo elements |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 94 | REPO_HOST="$(echo "${repo}" | awk -F ';' '{print $1}')" |
| 95 | REPO_PROJECT="$(echo "${repo}" | awk -F ';' '{print $2}')" |
| 96 | REPO_NAME="$(echo "${repo}" | awk -F ';' '{print $3}')" |
| 97 | REPO_DEFAULT_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $4}')" |
| 98 | REPO_URL="${REPO_HOST}/${REPO_PROJECT}" |
| 99 | REPO_REFSPEC="${REPO_DEFAULT_REFSPEC}" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 100 | |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 101 | # clone and checkout in case it does not exist |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 102 | if [ ! -d ${SHARE_FOLDER}/${REPO_NAME} ]; then |
| 103 | git clone ${GIT_CLONE_PARAMS} ${REPO_URL} ${SHARE_FOLDER}/${REPO_NAME} |
| 104 | |
Arthur She | 3eb254e | 2022-09-18 20:41:03 -0700 | [diff] [blame] | 105 | # Repo synchronization |
| 106 | if [ -n "${GERRIT_TOPIC}" -a "${REPO_HOST}" = "${GERRIT_HOST}" ]; then |
| 107 | echo "Got Gerrit Topic: ${GERRIT_TOPIC}" |
| 108 | REPO_REFSPEC="$(ssh ${SSH_PARAMS} ${CI_BOT_USERNAME}@${REPO_HOST#https://} gerrit query ${GERRIT_QUERY_PARAMS} \ |
| 109 | project:${REPO_PROJECT} topic:${GERRIT_TOPIC} | ${SHARE_FOLDER}/${JOBS_REPO_NAME}/scripts/parse_refspec.py || true)" |
| 110 | if [ -z "${REPO_REFSPEC}" ]; then |
| 111 | REPO_REFSPEC="${REPO_DEFAULT_REFSPEC}" |
| 112 | echo "Roll back to \"${REPO_REFSPEC}\" for \"${REPO_PROJECT}\"" |
| 113 | fi |
| 114 | echo "Checkout refspec \"${REPO_REFSPEC}\" from repository \"${REPO_NAME}\"" |
| 115 | fi |
| 116 | |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 117 | # fetch and checkout the corresponding refspec |
| 118 | cd ${SHARE_FOLDER}/${REPO_NAME} |
| 119 | git fetch ${REPO_URL} ${REPO_REFSPEC} |
| 120 | git checkout FETCH_HEAD |
Paul Sokolovsky | b41ac9e | 2022-10-14 19:23:26 +0300 | [diff] [blame] | 121 | echo "Freshly cloned ${REPO_URL} (refspec ${REPO_REFSPEC}):" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 122 | git log -1 |
| 123 | cd $OLDPWD |
| 124 | |
| 125 | else |
| 126 | # otherwise just show the head's log |
| 127 | cd ${SHARE_FOLDER}/${REPO_NAME} |
Paul Sokolovsky | b41ac9e | 2022-10-14 19:23:26 +0300 | [diff] [blame] | 128 | echo "Using existing shared folder checkout for ${REPO_URL}:" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 129 | git log -1 |
| 130 | cd $OLDPWD |
| 131 | fi |
| 132 | |
| 133 | # copy repository into pwd dir (workspace in CI), so each job would work |
| 134 | # on its own workspace |
| 135 | cp -a -f ${SHARE_FOLDER}/${REPO_NAME} ${PWD}/${REPO_NAME} |
| 136 | |
| 137 | done |