Leonardo Sandoval | 9dfdd1b | 2020-08-06 17:08:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 2 | # |
Leonardo Sandoval | 579c737 | 2020-10-23 15:23:32 -0500 | [diff] [blame] | 3 | # Copyright (c) 2019-2020 Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | # |
| 8 | # Clone and sync all Trusted Firmware repositories. |
| 9 | # |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 10 | # For every cloned repository, set its location to a variable so that the |
| 11 | # checked out location can be passed down to sub-jobs. |
| 12 | # |
| 13 | # Generate an environment file that can then be sourced by the caller. |
| 14 | |
| 15 | set -e |
| 16 | |
| 17 | ci_root="$(readlink -f "$(dirname "$0")/..")" |
| 18 | source "$ci_root/utils.sh" |
| 19 | |
| 20 | clone_log="$workspace/clone_repos.log" |
| 21 | clone_data="$workspace/clone.data" |
| 22 | override_data="$workspace/override.data" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 23 | inject_data="$workspace/inject.data" |
| 24 | |
| 25 | # File containing parameters for sub jobs |
| 26 | param_file="$workspace/env.param" |
| 27 | |
| 28 | # Emit a parameter to sub jobs |
| 29 | emit_param() { |
| 30 | echo "$1=$2" >> "$param_file" |
| 31 | } |
| 32 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 33 | # Emit a parameter for code coverage metadata |
| 34 | code_cov_emit_param() { |
| 35 | emit_param "CC_$(echo ${1^^} | tr '-' _)_$2" "$3" |
| 36 | } |
| 37 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 38 | meta_data() { |
| 39 | echo "$1" >> "$clone_data" |
| 40 | } |
| 41 | |
| 42 | # Path into the project filer where various pieces of scripts that override |
| 43 | # some CI environment variables are stored. |
| 44 | ci_overrides="$project_filer/ci-overrides" |
| 45 | |
| 46 | display_override() { |
| 47 | echo |
| 48 | echo -n "Override: " |
| 49 | # Print the relative path of the override file. |
| 50 | echo "$1" | sed "s#$ci_overrides/\?##" |
| 51 | } |
| 52 | |
| 53 | strip_var() { |
| 54 | local var="$1" |
| 55 | local val="$(echo "${!var}" | sed 's#^\s*\|\s*$##g')" |
| 56 | eval "$var=\"$val\"" |
| 57 | } |
| 58 | |
| 59 | prefix_tab() { |
| 60 | sed 's/^/\t/g' < "${1:?}" |
| 61 | } |
| 62 | |
| 63 | prefix_arrow() { |
| 64 | sed 's/^/ > /g' < "${1:?}" |
| 65 | } |
| 66 | |
| 67 | test_source() { |
| 68 | local file="${1:?}" |
| 69 | if ! bash -c "source $file" &>/dev/null; then |
| 70 | return 1 |
| 71 | fi |
| 72 | |
| 73 | source "$file" |
| 74 | return 0 |
| 75 | } |
| 76 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 77 | # Whether we've overridden some CI environment variables. |
| 78 | has_overrides=0 |
| 79 | |
| 80 | # Whether we've injected environment via. Jenkins |
| 81 | has_env_inject=0 |
| 82 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 83 | clone_and_sync() { |
| 84 | local stat |
| 85 | local topic |
| 86 | local refspec="${!ref}" |
| 87 | local s_before s_after s_diff |
| 88 | local reference_dir="$project_filer/ref-repos/${name?}" |
| 89 | local ref_repo |
| 90 | local ret |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 91 | |
| 92 | strip_var refspec |
| 93 | strip_var url |
| 94 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 95 | # Clone in the filter workspace |
| 96 | mkdir -p "$ci_scratch" |
| 97 | pushd "$ci_scratch" |
| 98 | |
| 99 | # Seconds before |
| 100 | s_before="$(date +%s)" |
| 101 | |
| 102 | # Clone repository to the directory same as its name; HEAD stays at |
| 103 | # master. |
| 104 | if [ -d "$reference_dir" ]; then |
| 105 | ref_repo="--reference $reference_dir" |
| 106 | fi |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 107 | echo "$ref_repo $url $name $branch" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 108 | git clone -q $ref_repo "$url" "$name" &>"$clone_log" |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 109 | code_cov_emit_param "${name}" "URL" "${url}" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 110 | stat="on branch master" |
| 111 | |
| 112 | pushd "$name" |
| 113 | |
| 114 | if [ "$refspec" ] && [ "$refspec" != "master" ]; then |
| 115 | # If a specific revision is specified, always use that. |
| 116 | git fetch -q origin "$refspec" &>"$clone_log" |
| 117 | git checkout -q FETCH_HEAD &>"$clone_log" |
| 118 | stat="refspec $refspec" |
| 119 | |
| 120 | # If it's not a commit hash, have the refspec replicated on the |
| 121 | # clone so that downstream jobs can clone from this one using |
| 122 | # the same refspec. |
| 123 | if echo "$refspec" | grep -qv '^[a-f0-9]\+$'; then |
Saul Romero | 06717eb | 2023-08-18 11:20:38 +0100 | [diff] [blame] | 124 | git branch -f "$refspec" FETCH_HEAD |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 125 | fi |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 126 | fi |
| 127 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 128 | code_cov_emit_param "${name}" "REFSPEC" "${refspec}" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 129 | # Generate meta data. Eliminate any quoting in commit subject as it |
| 130 | # might cause problems when reporting back to Gerrit. |
| 131 | meta_data "$name: $stat" |
| 132 | meta_data " $(git show --quiet --format=%H): $(git show --quiet --format=%s | sed "s/[\"']/ /g")" |
| 133 | meta_data " Commit date: $(git show --quiet --format=%cd)" |
| 134 | meta_data |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 135 | code_cov_emit_param "${name}" "COMMIT" "$(git show --quiet --format=%H)" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 136 | |
| 137 | # Calculate elapsed seconds |
| 138 | s_after="$(date +%s)" |
| 139 | let "s_diff = $s_after - $s_before" || true |
| 140 | |
| 141 | echo |
| 142 | echo "Repository: $url ($stat)" |
| 143 | prefix_arrow <(git show --quiet) |
| 144 | echo "Cloned in $s_diff seconds" |
| 145 | echo |
| 146 | |
| 147 | popd |
| 148 | popd |
| 149 | |
| 150 | emit_env "$loc" "$ci_scratch/$name" |
| 151 | emit_env "$ref" "$refspec" |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 154 | # Environment file in Java property file format, that's soured in Jenkins job |
| 155 | env_file="$workspace/env" |
| 156 | rm -f "$env_file" |
| 157 | |
| 158 | # Workspace on external filer where all repositories gets cloned so that they're |
| 159 | # accessible to all Jenkins slaves. |
| 160 | if upon "$local_ci"; then |
| 161 | ci_scratch="$workspace/filer" |
| 162 | else |
| 163 | scratch_owner="${JOB_NAME:?}-${BUILD_NUMBER:?}" |
| 164 | ci_scratch="$project_scratch/$scratch_owner" |
| 165 | tforg_key="$CI_BOT_KEY" |
| 166 | tforg_user="$CI_BOT_USERNAME" |
| 167 | fi |
| 168 | |
| 169 | if [ -d "$ci_scratch" ]; then |
| 170 | # This could be because of jobs of same name running from |
| 171 | # production/staging/temporary VMs |
| 172 | echo "Scratch space $ci_scratch already exists; removing." |
| 173 | rm -rf "$ci_scratch" |
| 174 | fi |
| 175 | mkdir -p "$ci_scratch" |
| 176 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 177 | # Set CI_SCRATCH so that it'll be injected when sub-jobs are triggered. |
| 178 | emit_param "CI_SCRATCH" "$ci_scratch" |
| 179 | |
| 180 | # However, on Jenkins v2, injected environment variables won't override current |
| 181 | # job's parameters. This means that the current job (the scratch owner, the job |
| 182 | # that's executing this script) would always observe CI_SCRATCH as empty, and |
| 183 | # therefore won't be able to remove it. Therefore, use a different variable |
| 184 | # other than CI_SCRATCH parameter for the current job to refer to the scratch |
| 185 | # space (although they both will have the same value!) |
| 186 | emit_env "SCRATCH_OWNER" "$scratch_owner" |
| 187 | emit_env "SCRATCH_OWNER_SPACE" "$ci_scratch" |
| 188 | |
| 189 | strip_var CI_ENVIRONMENT |
| 190 | if [ "$CI_ENVIRONMENT" ]; then |
| 191 | { |
| 192 | echo |
| 193 | echo "Injected environment:" |
| 194 | prefix_tab <(echo "$CI_ENVIRONMENT") |
| 195 | echo |
| 196 | } >> "$inject_data" |
| 197 | |
| 198 | cat "$inject_data" |
| 199 | |
| 200 | tmp_env=$(mktempfile) |
| 201 | echo "$CI_ENVIRONMENT" > "$tmp_env" |
| 202 | source "$tmp_env" |
| 203 | cat "$tmp_env" >> "$env_file" |
| 204 | |
| 205 | has_env_inject=1 |
| 206 | fi |
| 207 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 208 | TF_REFSPEC="${tf_refspec:-$TF_REFSPEC}" |
| 209 | if not_upon "$no_tf"; then |
| 210 | # Clone Trusted Firmware repository |
| 211 | url="$tf_src_repo_url" name="trusted-firmware" ref="TF_REFSPEC" \ |
| 212 | loc="TF_CHECKOUT_LOC" \ |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 213 | clone_and_sync |
| 214 | fi |
| 215 | |
| 216 | TFTF_REFSPEC="${tftf_refspec:-$TFTF_REFSPEC}" |
| 217 | if not_upon "$no_tftf"; then |
| 218 | # Clone Trusted Firmware TF repository |
| 219 | url="$tftf_src_repo_url" name="trusted-firmware-tf" ref="TFTF_REFSPEC" \ |
| 220 | loc="TFTF_CHECKOUT_LOC" \ |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 221 | clone_and_sync |
| 222 | fi |
| 223 | |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 224 | # Clone code coverage repository if code coverage is enabled |
| 225 | if not_upon "$no_cc"; then |
| 226 | pushd "$ci_scratch" |
| 227 | git clone -q $cc_src_repo_url cc_plugin -b $cc_src_repo_tag 2> /dev/null |
| 228 | popd |
| 229 | fi |
| 230 | |
Olivier Deprez | e52d6fc | 2019-12-17 17:48:19 +0100 | [diff] [blame] | 231 | SPM_REFSPEC="${spm_refspec:-$SPM_REFSPEC}" |
| 232 | if not_upon "$no_spm"; then |
| 233 | # Clone SPM repository |
| 234 | url="$spm_src_repo_url" name="spm" ref="SPM_REFSPEC" \ |
| 235 | loc="SPM_CHECKOUT_LOC" clone_and_sync |
| 236 | fi |
| 237 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 238 | CI_REFSPEC="${ci_refspec:-$CI_REFSPEC}" |
| 239 | if not_upon "$no_ci"; then |
| 240 | # Clone Trusted Firmware CI repository |
| 241 | url="$tf_ci_repo_url" name="trusted-firmware-ci" ref="CI_REFSPEC" \ |
Chris Kay | e38de7a | 2025-08-04 19:06:35 +0100 | [diff] [blame] | 242 | loc="CI_ROOT" clone_and_sync |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 243 | fi |
| 244 | |
Jimmy Brisson | 0d5e12c | 2023-05-16 14:51:51 -0500 | [diff] [blame] | 245 | TF_M_TESTS_REFSPEC="${tf_m_tests_refspec:-$TF_M_TESTS_REFSPEC}" |
| 246 | if not_upon "$no_tfm_tests"; then |
| 247 | url="$tf_m_tests_src_repo_url" name="tf-m-tests" ref="TF_M_TESTS_REFSPEC" \ |
| 248 | loc="TF_M_TESTS_PATH" clone_and_sync |
| 249 | fi |
| 250 | |
| 251 | TF_M_EXTRAS_REFSPEC="${tf_m_extras_refspec:-$TF_M_EXTRAS_REFSPEC}" |
| 252 | if not_upon "$no_tfm_extras"; then |
| 253 | url="$tf_m_extras_src_repo_url" name="tf-m-extras" ref="TF_M_EXTRAS_REFSPEC" \ |
| 254 | loc="TF_M_EXTRAS_PATH" clone_and_sync |
| 255 | fi |
| 256 | |
Manish V Badarkhe | 4190945 | 2025-04-11 12:06:45 +0100 | [diff] [blame] | 257 | RMM_REFSPEC="${rmm_refspec:-$RMM_REFSPEC}" |
Manish V Badarkhe | d62aa5f | 2025-03-18 21:18:14 +0000 | [diff] [blame] | 258 | if not_upon "$no_rmm"; then |
| 259 | url="$rmm_src_repo_url" name="tf-rmm" ref="RMM_REFSPEC" \ |
| 260 | loc="RMM_PATH" clone_and_sync |
| 261 | fi |
| 262 | |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 263 | # Copy environment file to ci_scratch for sub-jobs' access |
| 264 | cp "$env_file" "$ci_scratch" |
| 265 | cp "$param_file" "$ci_scratch" |
| 266 | |
| 267 | # Copy clone data so that it's available for sub-jobs' HTML reporting |
| 268 | if [ -f "$clone_data" ]; then |
| 269 | cp "$clone_data" "$ci_scratch" |
| 270 | fi |
| 271 | |
| 272 | # vim: set tw=80 sw=8 noet: |