Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 | |
| 30 | # Global defaults |
| 31 | REFSPEC_MASTER="refs/heads/master" |
| 32 | GIT_REPO="https://git.trustedfirmware.org" |
| 33 | GERRIT_HOST="https://review.trustedfirmware.org" |
Leonardo Sandoval | 49a18a4 | 2021-09-26 19:48:53 -0500 | [diff] [blame^] | 34 | GIT_CLONE_PARAMS="" |
Leonardo Sandoval | d1b6b5a | 2021-09-13 12:06:26 -0500 | [diff] [blame] | 35 | |
| 36 | # Defaults Projects |
| 37 | TF_GERRIT_PROJECT="${GERRIT_HOST}/${TF_GERRIT_PROJECT:-TF-A/trusted-firmware-a}" |
| 38 | TFTF_GERRIT_PROJECT="${GERRIT_HOST}/${TFTF_GERRIT_PROJECT:-/TF-A/tf-a-tests}" |
| 39 | SCRIPTS_PROJECT="${SCRIPTS_PROJECT:-${GIT_REPO}/ci/tf-a-ci-scripts.git}" |
| 40 | JOBS_PROJECT="${JOB_PROJECT:-${GIT_REPO}/ci/tf-a-job-configs.git}" |
| 41 | |
| 42 | # Default Reference specs |
| 43 | TF_GERRIT_REFSPEC="${TF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}" |
| 44 | TFTF_GERRIT_REFSPEC="${TFTF_GERRIT_REFSPEC:-${REFSPEC_MASTER}}" |
| 45 | SCRIPTS_REFSPEC="${SCRIPTS_REFSPEC:-${REFSPEC_MASTER}}" |
| 46 | JOBS_REFSPEC="${JOBS_REFSPEC:-${REFSPEC_MASTER}}" |
| 47 | |
| 48 | # Array containing "<repo url>;"<repo name>;<refspec>" elements |
| 49 | repos=( |
| 50 | "${SCRIPTS_PROJECT};tf-a-ci-scripts;${SCRIPTS_REFSPEC}" |
| 51 | "${JOBS_PROJECT};tf-a-job-configs;${JOBS_REFSPEC}" |
| 52 | "${TF_GERRIT_PROJECT};trusted-firmware-a;${TF_GERRIT_REFSPEC}" |
| 53 | "${TFTF_GERRIT_PROJECT};tf-a-tests;${TFTF_GERRIT_REFSPEC}" |
| 54 | ) |
| 55 | |
| 56 | # Take into consideration non-CI runs where SHARE_FOLDER variable |
| 57 | # may not be present |
| 58 | if [ -z "${SHARE_FOLDER}" ]; then |
| 59 | # Default Jenkins values |
| 60 | SHARE_VOLUME="${SHARE_VOLUME:-$PWD}" |
| 61 | JOB_NAME="${JOB_NAME:-local}" |
| 62 | BUILD_NUMBER="${BUILD_NUMBER:-0}" |
| 63 | SHARE_FOLDER=${SHARE_VOLUME}/${JOB_NAME}/${BUILD_NUMBER} |
| 64 | fi |
| 65 | |
| 66 | # clone git repos |
| 67 | for repo in ${repos[@]}; do |
| 68 | |
| 69 | # parse the repo elements |
| 70 | REPO_URL="$(echo "${repo}" | awk -F ';' '{print $1}')" |
| 71 | REPO_NAME="$(echo "${repo}" | awk -F ';' '{print $2}')" |
| 72 | REPO_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $3}')" |
| 73 | |
| 74 | # clone and checkout in case it does not exit |
| 75 | if [ ! -d ${SHARE_FOLDER}/${REPO_NAME} ]; then |
| 76 | git clone ${GIT_CLONE_PARAMS} ${REPO_URL} ${SHARE_FOLDER}/${REPO_NAME} |
| 77 | |
| 78 | # fetch and checkout the corresponding refspec |
| 79 | cd ${SHARE_FOLDER}/${REPO_NAME} |
| 80 | git fetch ${REPO_URL} ${REPO_REFSPEC} |
| 81 | git checkout FETCH_HEAD |
| 82 | git log -1 |
| 83 | cd $OLDPWD |
| 84 | |
| 85 | else |
| 86 | # otherwise just show the head's log |
| 87 | cd ${SHARE_FOLDER}/${REPO_NAME} |
| 88 | git log -1 |
| 89 | cd $OLDPWD |
| 90 | fi |
| 91 | |
| 92 | # copy repository into pwd dir (workspace in CI), so each job would work |
| 93 | # on its own workspace |
| 94 | cp -a -f ${SHARE_FOLDER}/${REPO_NAME} ${PWD}/${REPO_NAME} |
| 95 | |
| 96 | done |