blob: e2f1042e21443f4c179cb506796961e88af03b33 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#
8# Clone and sync all Trusted Firmware repositories.
9#
Fathi Boudra422bf772019-12-02 11:10:16 +020010# 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
15set -e
16
17ci_root="$(readlink -f "$(dirname "$0")/..")"
18source "$ci_root/utils.sh"
19
20clone_log="$workspace/clone_repos.log"
21clone_data="$workspace/clone.data"
22override_data="$workspace/override.data"
Fathi Boudra422bf772019-12-02 11:10:16 +020023inject_data="$workspace/inject.data"
24
25# File containing parameters for sub jobs
26param_file="$workspace/env.param"
27
28# Emit a parameter to sub jobs
29emit_param() {
30 echo "$1=$2" >> "$param_file"
31}
32
Zelalem219df412020-05-17 19:21:20 -050033# Emit a parameter for code coverage metadata
34code_cov_emit_param() {
35 emit_param "CC_$(echo ${1^^} | tr '-' _)_$2" "$3"
36}
37
Fathi Boudra422bf772019-12-02 11:10:16 +020038meta_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.
44ci_overrides="$project_filer/ci-overrides"
45
46display_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
53strip_var() {
54 local var="$1"
55 local val="$(echo "${!var}" | sed 's#^\s*\|\s*$##g')"
56 eval "$var=\"$val\""
57}
58
59prefix_tab() {
60 sed 's/^/\t/g' < "${1:?}"
61}
62
63prefix_arrow() {
64 sed 's/^/ > /g' < "${1:?}"
65}
66
67test_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 Boudra422bf772019-12-02 11:10:16 +020077# Whether we've overridden some CI environment variables.
78has_overrides=0
79
80# Whether we've injected environment via. Jenkins
81has_env_inject=0
82
Fathi Boudra422bf772019-12-02 11:10:16 +020083clone_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 Boudra422bf772019-12-02 11:10:16 +020091
92 strip_var refspec
93 strip_var url
94
Fathi Boudra422bf772019-12-02 11:10:16 +020095 # 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
Zelalem219df412020-05-17 19:21:20 -0500107 echo "$ref_repo $url $name $branch"
Fathi Boudra422bf772019-12-02 11:10:16 +0200108 git clone -q $ref_repo "$url" "$name" &>"$clone_log"
Zelalem219df412020-05-17 19:21:20 -0500109 code_cov_emit_param "${name}" "URL" "${url}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200110 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 Romero06717eb2023-08-18 11:20:38 +0100124 git branch -f "$refspec" FETCH_HEAD
Fathi Boudra422bf772019-12-02 11:10:16 +0200125 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200126 fi
127
Zelalem219df412020-05-17 19:21:20 -0500128 code_cov_emit_param "${name}" "REFSPEC" "${refspec}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200129 # 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
Zelalem219df412020-05-17 19:21:20 -0500135 code_cov_emit_param "${name}" "COMMIT" "$(git show --quiet --format=%H)"
Fathi Boudra422bf772019-12-02 11:10:16 +0200136
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 Boudra422bf772019-12-02 11:10:16 +0200152}
153
Fathi Boudra422bf772019-12-02 11:10:16 +0200154# Environment file in Java property file format, that's soured in Jenkins job
155env_file="$workspace/env"
156rm -f "$env_file"
157
158# Workspace on external filer where all repositories gets cloned so that they're
159# accessible to all Jenkins slaves.
160if upon "$local_ci"; then
161 ci_scratch="$workspace/filer"
162else
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"
167fi
168
169if [ -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"
174fi
175mkdir -p "$ci_scratch"
176
Fathi Boudra422bf772019-12-02 11:10:16 +0200177# Set CI_SCRATCH so that it'll be injected when sub-jobs are triggered.
178emit_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!)
186emit_env "SCRATCH_OWNER" "$scratch_owner"
187emit_env "SCRATCH_OWNER_SPACE" "$ci_scratch"
188
189strip_var CI_ENVIRONMENT
190if [ "$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
206fi
207
Fathi Boudra422bf772019-12-02 11:10:16 +0200208TF_REFSPEC="${tf_refspec:-$TF_REFSPEC}"
209if 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 Boudra422bf772019-12-02 11:10:16 +0200213 clone_and_sync
214fi
215
216TFTF_REFSPEC="${tftf_refspec:-$TFTF_REFSPEC}"
217if 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 Boudra422bf772019-12-02 11:10:16 +0200221 clone_and_sync
222fi
223
Zelalem219df412020-05-17 19:21:20 -0500224# Clone code coverage repository if code coverage is enabled
225if 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
229fi
230
Olivier Depreze52d6fc2019-12-17 17:48:19 +0100231SPM_REFSPEC="${spm_refspec:-$SPM_REFSPEC}"
232if 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
236fi
237
Fathi Boudra422bf772019-12-02 11:10:16 +0200238CI_REFSPEC="${ci_refspec:-$CI_REFSPEC}"
239if 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 Kaye38de7a2025-08-04 19:06:35 +0100242 loc="CI_ROOT" clone_and_sync
Fathi Boudra422bf772019-12-02 11:10:16 +0200243fi
244
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -0500245TF_M_TESTS_REFSPEC="${tf_m_tests_refspec:-$TF_M_TESTS_REFSPEC}"
246if 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
249fi
250
251TF_M_EXTRAS_REFSPEC="${tf_m_extras_refspec:-$TF_M_EXTRAS_REFSPEC}"
252if 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
255fi
256
Manish V Badarkhe41909452025-04-11 12:06:45 +0100257RMM_REFSPEC="${rmm_refspec:-$RMM_REFSPEC}"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000258if not_upon "$no_rmm"; then
259 url="$rmm_src_repo_url" name="tf-rmm" ref="RMM_REFSPEC" \
260 loc="RMM_PATH" clone_and_sync
261fi
262
Fathi Boudra422bf772019-12-02 11:10:16 +0200263# Copy environment file to ci_scratch for sub-jobs' access
264cp "$env_file" "$ci_scratch"
265cp "$param_file" "$ci_scratch"
266
267# Copy clone data so that it's available for sub-jobs' HTML reporting
268if [ -f "$clone_data" ]; then
269 cp "$clone_data" "$ci_scratch"
270fi
271
272# vim: set tw=80 sw=8 noet: