Debian Codegraph @

docs/ci/structured-tagging.rst

raw
1 ================================
2 Structured Tagging for CI Builds
3 ================================
4
5 This document explains the new structural tagging system integrated into our CI pipeline. Structural
6 tagging ensures that every build and test job is tied to a unique, reproducible build state by
7 computing a deterministic tag from each component's build script (and its relevant inputs, such as
8 patches) and verifying that tag at both build and test time.
9
10 Overview
11 --------
12 Structural tagging enhances container and rootfs tag management with an automated, end-to-end
13 process. Key aspects include:
14
15 Deterministic Tag Generation
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 During the build, the system calculates a tag (an MD5 hash) from the contents of the build script
18 plus any extra files that affect the build. This tag represents the "structure" of the build.
19
20 YAML-Declared Tags
21 ~~~~~~~~~~~~~~~~~~
22 For each component, a tag is declared in the YAML configuration file (located at
23 ``.gitlab-ci/conditional-build-image-tags.yml``). The declared value is used to verify that the
24 build script has not changed without an accompanying update to the tag.
25
26 Dual-Phase Verification
27 ~~~~~~~~~~~~~~~~~~~~~~~
28 * **Build Time:** The build script calls the helper function ``ci_build_time_tag_check`` immediately after calculating the tag. This function compares the calculated tag against the declared tag and writes the computed tag to a file (located at ``/mesa-ci-build-tag/``), ensuring early detection of mismatches.
29
30 * **Test Time:** Later, test scripts invoke ``ci_tag_test_time_check`` to read the stored tag and confirm that the tests run against the expected build version. A mismatch here will cause the test job to fail immediately.
31
32 Automated Tag Updates
33 ~~~~~~~~~~~~~~~~~~~~~
34 A helper script ``bin/ci/update_tag.py`` is provided to list, check, and update tags for all or
35 individual components. This tool is intended to simplify the process of keeping the declared tags
36 synchronized with the build scripts.
37
38 How it works
39 ------------
40
41 The mechanism considers a "component" any build script that follows the
42 ``.gitlab-ci/container/build-*.sh`` pattern and also uses the ``ci_tag_*`` prefixed functions provided by
43 ``.gitlab-ci/setup_test_env.sh``.
44
45 Suppose that SkQP just received the structured tagging support.
46 Let's look how the build and test phases work.
47
48 .. graphviz::
49 :caption: Structured Tagging
50
51 digraph StructuredTagging {
52 rankdir=TD;
53 node [style=filled, fillcolor=lightgray];
54
55 // =========================
56 // Build Phase Subgraph
57 // =========================
58 subgraph cluster_build {
59 label="Build Phase";
60 style=dashed;
61
62 // Define nodes with descriptive IDs and labels.
63 tag_decl [
64 label="SKQP_TAG declared in\n.gitlab-ci/conditional-build-image-tags.yml",
65 shape=note, fillcolor=white
66 ];
67 calc_tag [
68 label="build-skqp.sh:\nCalculate tag (build script content + patches)",
69 shape=box, style="rounded,filled", fillcolor=lightgray
70 ];
71 early_check [
72 label="container_pre_build.sh:\nCheck all structured tags early",
73 shape=box, style="rounded,filled", fillcolor=lightgray
74 ];
75 validate_tag [
76 label="Validate calculated tag\nagainst declared SKQP_TAG",
77 shape=box, style="rounded,filled", fillcolor=lightgray
78 ];
79 build_decision [
80 label="Is calculated tag equal\nto declared SKQP_TAG?",
81 shape=diamond, fillcolor=lightyellow
82 ];
83 fail_build [
84 label="Fail build",
85 shape=rounded, fillcolor=salmon
86 ];
87 write_tag [
88 label="Write calculated tag to\n/var/tmp/mesa-ci-build-tag/SKQP_TAG",
89 shape=box, style="rounded,filled", fillcolor=lightblue
90 ];
91 compile_skqp [
92 label="Compile SkQP",
93 shape=box, fillcolor=palegreen
94 ];
95
96 // Define edges for the build phase.
97 tag_decl -> calc_tag;
98 calc_tag -> validate_tag;
99 early_check -> validate_tag;
100 validate_tag -> build_decision;
101 build_decision -> fail_build [label="No"];
102 build_decision -> write_tag [label="Yes"];
103 write_tag -> compile_skqp;
104 }
105
106 // =========================
107 // Test Phase Subgraph
108 // =========================
109 subgraph cluster_test {
110 label="Test Phase";
111 style=dashed;
112
113 // Define nodes with descriptive IDs and labels.
114 skqp_running [
115 label="Is SKQP running?",
116 shape=diamond, fillcolor=lightyellow
117 ];
118 ci_var_include [
119 label="image-tags.yml:\nincludes\ncontainer-builds-image-tags.yml",
120 shape=note, fillcolor=white
121 ];
122 ci_var [
123 label="CI Variable\n(CONDITIONAL_BUILD_SKQP_TAG)\nfrom container-builds-image-tags.yml",
124 shape=note, fillcolor=white
125 ];
126 ci_var_extends [
127 label="This job extends\n.container-builds-skqp\nMaking SKQP_TAG=CONDITIONAL_BUILD_SKQP_TAG",
128 shape=note, fillcolor=white
129 ];
130 check_tag [
131 label="deqp-runner.sh:\nPull tag in /var/tmp/mesa-ci-build-tag/SKQP_TAG",
132 shape=box, style="rounded,filled", fillcolor=lightblue
133 ];
134 decision [
135 label="Is calculated tag equal\nto declared SKQP_TAG?",
136 shape=diamond, fillcolor=lightyellow
137 ];
138 proceed_test [
139 label="Proceed the test job",
140 shape=box, fillcolor=palegreen
141 ];
142 fail_test [
143 label="Fail test",
144 shape=box, fillcolor=salmon
145 ];
146
147 // Define edges for the test phase.
148 skqp_running -> check_tag [label="Yes"];
149 skqp_running -> proceed_test [label="No"];
150 check_tag -> decision;
151 decision -> proceed_test [label="Yes"];
152 decision -> fail_test [label="Mismatch"];
153 ci_var_extends -> decision;
154 ci_var -> ci_var_extends;
155 ci_var_include -> ci_var;
156 }
157 }
158
159
160 Build-Time Checks
161 ~~~~~~~~~~~~~~~~~
162 During the build phase:
163
164 * **Tag Calculation:**
165 In the component's build script (named following the convention ``build-<component>.sh``), the
166 function ``_ci_calculate_tag`` computes an MD5 hash based on:
167
168 - The build script's contents.
169 - Any additional files (e.g. patches) that affect the build.
170
171 * **Validation:**
172 The build script calls ``ci_tag_build_time_check`` to verify that the current value of the component's
173 tag (passed in as an environment variable) matches the tag calculated by the build script.
174
175 * **Failure on Mismatch:**
176 If the tags do not match, the build is aborted. This prevents any accidental use of stale or mismatched artifacts.
177
178 * **Early checks:**
179 Right now, the `container_pre_build.sh` script is responsible for checking the structured tagging
180 in all registered components. So, we can check quickly, before the component's build starts, if
181 the tag is correct.
182
183 * **Tag writing:**
184 The build script writes the computed tag into a new file the structured tagging directory, namely
185 ``/mesa-ci-build-tag/<component>_TAG``.
186
187 Test-Time Checks
188 ~~~~~~~~~~~~~~~~
189 In the test scripts (for example, in ``.gitlab-ci/deqp-runner.sh``):
190
191 * **Verification:**
192 The test job retrieves the tag written into the artifact (e.g. from ``/mesa-ci-build-tag/DEQP_RUNNER_TAG``) and then calls:
193
194 .. code-block:: bash
195
196 ci_tag_test_time_check "DEQP_RUNNER_TAG"
197
198 * **Purpose:**
199 This check ensures that the tests are run against the exact build that was produced. If a mismatch is found, the test job fails immediately.
200
201 .. note::
202 Even when the developer forgets to update the ``image-tags.yml`` file when needed, the test job
203 will fail if the tag is not correct, given that the ``conditional-build-image-tags.yml``
204 file is properly updated.
205
206
207 Adding a New Component Tag
208 --------------------------
209 To integrate structured tagging for a new component (for example, ``my_component``), follow these steps:
210
211 1. **Modify the Build Script:**
212
213 - In your build script (e.g. ``.gitlab-ci/container/build-my-component.sh``), map out the external files that can affect the build output.
214 *Tip:* You can mimic the approach in ``build-angle.sh`` early variable declaration to get the tag.
215 - Immediately after calculating the tag, add a validation step:
216
217 .. code-block:: bash
218
219 PATCH_FILES=("...")
220
221 ci_tag_build_time_check "MY_COMPONENT_TAG" "${PATCH_FILES[@]}"
222
223 2. **If the component is run in a DUT job, update the passthrough script:**
224
225 On ``.gitlab-ci/common/generate-env.sh``:
226
227 .. code-block:: bash
228
229 VARS=(
230 ...
231 MY_COMPONENT_TAG
232 ...
233 )
234
235 3. **Update the CI YAMLs:**
236
237 - In your conditional image tags file (e.g. ``.gitlab-ci/conditional-build-image-tags.yml``), add an entry for your component:
238
239 .. code-block:: yaml
240
241 variables:
242 CONDITIONAL_BUILD_MY_COMPONENT_TAG: <initial-tag-value>
243
244 - Now we need to update the build related YAMLs to include the new component tag. In ``.gitlab-ci/container/gitlab-ci.yml``, add a new hidden job:
245
246 .. code-block:: yaml
247
248 .container-builds-my-component:
249 variables:
250 MY_COMPONENT_TAG: "${CONDITIONAL_BUILD_MY_COMPONENT_TAG}"
251
252 - It is time to modify the job that builds the component image to include the new component tag. Let's suppose that only the ``debian/arm64_test-gl`` job builds the component image. We need to add the new component tag to the job as an extension:
253
254 .. code-block:: yaml
255
256 debian/arm64_test-gl:
257 extends:
258 - .container-builds-my-component
259 - .container-builds-my-component2
260 variables:
261 # CI_BUILD_COMPONENTS is a space-separated list of components used during early tag checks
262 CI_BUILD_COMPONENTS: "my_component my_component2"
263
264 - Now, ``MY_COMPONENT_TAG`` will be used by the ``ci_tag_build_time_check`` and ``ci_tag_test_time_check`` functions, only for jobs that extend the ``.container-builds-my-component`` job.
265 - And the ``CI_BUILD_COMPONENTS`` variable will be swept to perform the early checks.
266
267 .. warning::
268 Do not forget to update your main image tags file (e.g. ``.gitlab-ci/image-tags.yml``) if necessary, check the header comments of the modified files for more details.
269
270 .. note::
271 Also, note that the main image tags file (``.gitlab-ci/image-tags.yml``) does not define the
272 conditional build tags directly.
273 Instead, it **retrieves** values such as ``MY_COMPONENT_TAG`` from the `includes` directive of the
274 ``.gitlab/container-builds-image-tags.yml`` file. This setup ensures centralized management of
275 tag values and maintains consistency across various components and jobs.
276
277 Updating Component Tags with the Helper Script
278 ----------------------------------------------
279 The helper script ``bin/ci/update_tag.py`` assists with tag management. Its key functionalities include:
280
281 * **Listing Available Components:**
282
283 .. code-block:: bash
284
285 ./bin/ci/update_tag.py --list
286
287 * **Updating All Component Tags:**
288
289 .. code-block:: bash
290
291 ./bin/ci/update_tag.py --all
292
293 * **Updating a Specific Component Tag:**
294
295 .. code-block:: bash
296
297 ./bin/ci/update_tag.py --include my_component
298 ./bin/ci/update_tag.py --include 'my_component.*'
299
300 * **Running a Check:**
301
302 .. code-block:: bash
303
304 ./bin/ci/update_tag.py --check my_component
305 ./bin/ci/update_tag.py --check my_component1 --check my_component2
306
307 This script uses the same underlying functions as in the build scripts to generate the deterministic tag and then updates the YAML file accordingly.
308 Ensure that your python environment has the requirements installed, see ``bin/ci/requirements.txt`` for the list of dependencies.
309
310 Limitations
311 -----------
312 The current implementation has some known limitations:
313
314 * **Local Utility Script Constraints:**
315
316 When running the update/tagging utility locally, the build inputs used by the build script (such
317 as environment variables defined in the YAML) are not automatically applied. For example, if the
318 tag calculation relies on a variable like ``EXTRA_MESON_ARGS``, you must manually set or mock its
319 value locally to generate the correct tag. Otherwise, the computed tag may be incorrect, and you
320 might need to run the actual build job (and extract the expected tag from the error message) to
321 verify the value. Future improvements may leverage tools like gitlab-ci-local to better reproduce
322 the YAML environment locally.
323
324 * **Timing Sensitivity:**
325
326 If the build script is modified after the early check (performed by the utility script) but before the actual build job runs, the calculated tag will differ from the declared tag. This discrepancy will block the build consistently until the YAML declaration is updated.
327
328 * **Manual Update Requirement:**
329
330 In this initial version, updating the ``image-tags.yml`` must be done manually. If this file is
331 not updated, the build scripts will not be validated properly.
332 However, the test-time check will still catch mismatches and abort the job, ensuring that any
333 issues do not go unnoticed.
334
335 Troubleshooting and FAQ
336 -----------------------
337
338 * **Tag Mismatch Errors:**
339 If you encounter a tag mismatch error, verify that:
340
341 - The build script and its additional inputs (patches, environment variables, etc.) are current.
342 - The declared tag in ``.gitlab-ci/conditional-build-image-tags.yml`` has been updated accordingly. Use the update helper if necessary.
343
344 * **Local Testing Challenges:**
345 When running the update utility locally, ensure that you mock any YAML-dependent variables (e.g., EXTRA_MESON_ARGS) to simulate the CI environment.
346
347 Conclusion
348 ----------
349 The new structural tagging system provides a robust, automated method to ensure that every CI build
350 is uniquely identified and that tests run against the correct build state. By integrating
351 deterministic tag calculation with dual-phase verification and a dedicated update helper script,
352 this system minimizes human error and streamlines the CI process.
353
354 .. note::
355 Be aware of the current limitations, especially around local testing and the manual update
356 requirement, as you integrate and use structural tagging. Future improvements are planned to
357 address these issues.
358
359 Happy tagging!