Skip to content

Commit fd4059d

Browse files
committed
ci(gitlab): use template in dynamic configuration
We make the dynamic configuration generation script leaner by taking out literal tempate strings into template files.
1 parent 5fdd9c2 commit fd4059d

File tree

3 files changed

+92
-73
lines changed

3 files changed

+92
-73
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
build_base_venvs:
2+
extends: .cached_testrunner
3+
stage: setup
4+
needs: []
5+
parallel:
6+
matrix:
7+
- PYTHON_VERSION: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
8+
variables:
9+
CMAKE_BUILD_PARALLEL_LEVEL: '12'
10+
PIP_VERBOSE: '0'
11+
DD_PROFILING_NATIVE_TESTS: '1'
12+
DD_USE_SCCACHE: '1'
13+
DD_FAST_BUILD: '1'
14+
rules:
15+
- if: '$CI_COMMIT_REF_NAME == "main"'
16+
variables:
17+
DD_FAST_BUILD: '0'
18+
- when: always
19+
script: |
20+
set -e -o pipefail
21+
apt update && apt install -y sccache
22+
pip install riot==0.20.1
23+
riot -P -v generate --python=$PYTHON_VERSION
24+
echo "Running smoke tests"
25+
riot -v run -s --python=$PYTHON_VERSION smoke_test
26+
artifacts:
27+
name: venv_$PYTHON_VERSION
28+
paths:
29+
- scripts/restore-ext-cache.sh
30+
- scripts/save-ext-cache.sh
31+
- .riot/venv_*
32+
- ddtrace/_version.py
33+
- ddtrace/**/*.so*
34+
- ddtrace/internal/datadog/profiling/crashtracker/crashtracker_exe*
35+
- ddtrace/internal/datadog/profiling/test/test_*
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.cached_testrunner:
2+
extends: .testrunner
3+
variables:
4+
PIP_CACHE_DIR: '${{CI_PROJECT_DIR}}/.cache/pip'
5+
SCCACHE_DIR: '${{CI_PROJECT_DIR}}/.cache/sccache'
6+
DD_CMAKE_INCREMENTAL_BUILD: '1'
7+
DD_SETUP_CACHE_DOWNLOADS: '1'
8+
EXT_CACHE_VENV: '${{CI_PROJECT_DIR}}/.cache/ext_cache_venv'
9+
before_script: |
10+
set -e -o pipefail
11+
if [ ! -d $EXT_CACHE_VENV ]; then
12+
python$PYTHON_VERSION -m venv $EXT_CACHE_VENV
13+
source $EXT_CACHE_VENV/bin/activate
14+
pip install cmake setuptools_rust Cython
15+
else
16+
source $EXT_CACHE_VENV/bin/activate
17+
fi
18+
python scripts/gen_ext_cache_scripts.py
19+
deactivate
20+
$SHELL scripts/restore-ext-cache.sh
21+
after_script: |
22+
set -e -o pipefail
23+
source $EXT_CACHE_VENV/bin/activate
24+
python scripts/gen_ext_cache_scripts.py
25+
deactivate
26+
$SHELL scripts/save-ext-cache.sh
27+
cache:
28+
# Share pip/sccache between jobs of the same Python version
29+
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-cache-{current_month}
30+
paths:
31+
- .cache
32+
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-ext-{current_month}
33+
paths:
34+
- .ext_cache
35+
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-download-cache-{current_month}
36+
paths:
37+
- .download_cache

scripts/gen_gitlab_config.py

Lines changed: 20 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -245,85 +245,24 @@ def check(name: str, command: str, paths: t.Set[str]) -> None:
245245
key: v1-precheck-pip-cache
246246
paths:
247247
- .cache
248-
"""
248+
"""
249249
)
250250

251251

252-
def gen_build_base_venvs() -> None:
253-
"""Generate the list of base jobs for building virtual environments."""
252+
def gen_cached_testrunner() -> None:
253+
"""Generate the cached testrunner job."""
254+
with TESTS_GEN.open("a") as f:
255+
f.write(template("cached-testrunner", current_month=datetime.datetime.now().month))
254256

255-
current_month = datetime.datetime.now().month
256257

258+
def gen_build_base_venvs() -> None:
259+
"""Generate the list of base jobs for building virtual environments.
260+
261+
We need to generate this dynamically from a template because it depends
262+
on the cached testrunner job, which is also generated dynamically.
263+
"""
257264
with TESTS_GEN.open("a") as f:
258-
f.write(
259-
f"""
260-
build_base_venvs:
261-
extends: .testrunner
262-
stage: setup
263-
needs: []
264-
parallel:
265-
matrix:
266-
- PYTHON_VERSION: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
267-
variables:
268-
CMAKE_BUILD_PARALLEL_LEVEL: '12'
269-
PIP_VERBOSE: '0'
270-
DD_PROFILING_NATIVE_TESTS: '1'
271-
DD_USE_SCCACHE: '1'
272-
PIP_CACHE_DIR: '${{CI_PROJECT_DIR}}/.cache/pip'
273-
SCCACHE_DIR: '${{CI_PROJECT_DIR}}/.cache/sccache'
274-
DD_FAST_BUILD: '1'
275-
DD_CMAKE_INCREMENTAL_BUILD: '1'
276-
DD_SETUP_CACHE_DOWNLOADS: '1'
277-
EXT_CACHE_VENV: '${{CI_PROJECT_DIR}}/.cache/ext_cache_venv'
278-
rules:
279-
- if: '$CI_COMMIT_REF_NAME == "main"'
280-
variables:
281-
DD_FAST_BUILD: '0'
282-
- when: always
283-
script: |
284-
set -e -o pipefail
285-
apt update && apt install -y sccache
286-
pip install riot==0.20.1
287-
if [ ! -d $EXT_CACHE_VENV ]; then
288-
python$PYTHON_VERSION -m venv $EXT_CACHE_VENV
289-
source $EXT_CACHE_VENV/bin/activate
290-
pip install cmake setuptools_rust Cython
291-
else
292-
source $EXT_CACHE_VENV/bin/activate
293-
fi
294-
python scripts/gen_ext_cache_scripts.py
295-
deactivate
296-
$SHELL scripts/restore-ext-cache.sh
297-
riot -P -v generate --python=$PYTHON_VERSION
298-
echo "Running smoke tests"
299-
riot -v run -s --python=$PYTHON_VERSION smoke_test
300-
source $EXT_CACHE_VENV/bin/activate
301-
python scripts/gen_ext_cache_scripts.py
302-
deactivate
303-
$SHELL scripts/save-ext-cache.sh
304-
cache:
305-
# Share pip/sccache between jobs of the same Python version
306-
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-cache-{current_month}
307-
paths:
308-
- .cache
309-
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-ext-{current_month}
310-
paths:
311-
- .ext_cache
312-
- key: v1-build_base_venvs-${{PYTHON_VERSION}}-download-cache-{current_month}
313-
paths:
314-
- .download_cache
315-
artifacts:
316-
name: venv_$PYTHON_VERSION
317-
paths:
318-
- scripts/restore-ext-cache.sh
319-
- scripts/save-ext-cache.sh
320-
- .riot/venv_*
321-
- ddtrace/_version.py
322-
- ddtrace/**/*.so*
323-
- ddtrace/internal/datadog/profiling/crashtracker/crashtracker_exe*
324-
- ddtrace/internal/datadog/profiling/test/test_*
325-
"""
326-
)
265+
f.write(template("build-base-venvs"))
327266

328267

329268
# -----------------------------------------------------------------------------
@@ -358,6 +297,14 @@ def gen_build_base_venvs() -> None:
358297
sys.path.append(str(ROOT / "scripts"))
359298
sys.path.append(str(ROOT / "tests"))
360299

300+
301+
def template(name: str, **params):
302+
"""Render a template file with the given parameters."""
303+
if not (template_path := (GITLAB / "templates" / name).with_suffix(".yml")).exists():
304+
raise FileNotFoundError(f"Template file {template_path} does not exist")
305+
return "\n" + template_path.read_text().format(**params).strip() + "\n"
306+
307+
361308
has_error = False
362309

363310
LOGGER.info("Configuration generation steps:")

0 commit comments

Comments
 (0)