Skip to content

Commit f9d5413

Browse files
Merge pull request #962 from jiridanek/jd_sandboxed_tekton_pipelines
RHOAIENG-18400: chore(.tekton/): implement computation of `"pipelinesascode.tekton.dev/on-cel-expression"` values
2 parents 7e59877 + 23bfd82 commit f9d5413

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

.tekton/jupyter-minimal-ubi9-python-3-11-pull-request.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ metadata:
1010
build.appstudio.redhat.com/target_branch: '{{target_branch}}'
1111
pipelinesascode.tekton.dev/cancel-in-progress: "true"
1212
pipelinesascode.tekton.dev/max-keep-runs: '3'
13-
pipelinesascode.tekton.dev/on-cel-expression: event == "pull_request" && target_branch == "main" && has(body.repository) && body.repository.full_name == "opendatahub-io/notebooks"
13+
pipelinesascode.tekton.dev/on-cel-expression: event == "pull_request" && target_branch == "main" && ( "jupyter/minimal/ubi9-python-3.11/Pipfile.lock".pathChanged()
14+
|| "jupyter/minimal/ubi9-python-3.11/start-notebook.sh".pathChanged() || "jupyter/utils/***".pathChanged()
15+
|| ".tekton/jupyter-minimal-ubi9-python-3-11-pull-request.yaml".pathChanged()
16+
|| "jupyter/minimal/ubi9-python-3.11/Dockerfile.cpu".pathChanged() ) && has(body.repository)
17+
&& body.repository.full_name == "opendatahub-io/notebooks"
1418
creationTimestamp:
1519
labels:
1620
appstudio.openshift.io/application: notebooks

ci/cached-builds/konflux_generate_component_build_pipelines.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import re
44
import pathlib
5+
56
import yaml
67

78
import gen_gha_matrix_jobs
89
import gha_pr_changed_files
10+
import scripts.sandbox
11+
12+
# test dependencies
13+
import pyfakefs.fake_filesystem
914

1015
ROOT_DIR = pathlib.Path(__file__).parent.parent.parent
1116

@@ -21,7 +26,7 @@
2126
2227
Usage:
2328
24-
$ poetry run ci/cached-builds/konflux_generate_component_build_pipelines.py
29+
$ PYTHONPATH=. poetry run ci/cached-builds/konflux_generate_component_build_pipelines.py
2530
"""
2631

2732

@@ -58,6 +63,13 @@ def component_build_pipeline(component_name, dockerfile_path, is_pr: bool = True
5863
This is general enough to create PR pipeline as well as push pipeline.
5964
"""
6065
name = component_name + ("-on-pull-request" if is_pr else "-on-push")
66+
files_changed_cel_expression = ""
67+
if is_pr:
68+
files_changed_cel_expression = ' || '.join((
69+
compute_cel_expression(dockerfile_path),
70+
f'".tekton/{component_name}-pull-request.yaml".pathChanged()',
71+
f'"{dockerfile_path}".pathChanged()'
72+
))
6173
return {
6274
"apiVersion": "tekton.dev/v1",
6375
"kind": "PipelineRun",
@@ -71,6 +83,7 @@ def component_build_pipeline(component_name, dockerfile_path, is_pr: bool = True
7183
"pipelinesascode.tekton.dev/max-keep-runs": "3",
7284
"pipelinesascode.tekton.dev/on-cel-expression": (
7385
f'event == "{"pull_request" if is_pr else "push"}" && target_branch == "main"'
86+
+ (' && ( ' + files_changed_cel_expression + ' )' if files_changed_cel_expression else "")
7487
+ ' && has(body.repository) && body.repository.full_name == "opendatahub-io/notebooks"'
7588
),
7689
},
@@ -761,5 +774,48 @@ def main():
761774
file=yaml_file)
762775

763776

777+
def compute_cel_expression(dockerfile: pathlib.Path) -> str:
778+
return cel_expression(root_dir=ROOT_DIR, files=scripts.sandbox.buildinputs(dockerfile))
779+
780+
781+
def cel_expression(root_dir: pathlib.Path, files: list[pathlib.Path]) -> str:
782+
"""
783+
Generate a CEL expression for file change detection.
784+
785+
Args:
786+
root_dir (pathlib.Path): Docker build context.
787+
files (list[pathlib.Path]): List of file paths to check for changes.
788+
789+
Returns:
790+
str: A CEL expression that checks if any of the given files have changed.
791+
"""
792+
expressions = []
793+
for file in files:
794+
relative_path = file.relative_to(root_dir) if file.is_absolute() else file
795+
if file.is_dir():
796+
expressions.append(f'"{relative_path}/***".pathChanged()')
797+
else:
798+
expressions.append(f'"{relative_path}".pathChanged()')
799+
800+
return " || ".join(expressions)
801+
802+
764803
if __name__ == "__main__":
765804
main()
805+
806+
807+
class Tests:
808+
809+
def test_compute_cel_expression(self, fs: pyfakefs.fake_filesystem.FakeFilesystem):
810+
fs.cwd = ROOT_DIR
811+
ROOT_DIR.mkdir(parents=True)
812+
pathlib.Path("a/").mkdir()
813+
pathlib.Path("b/").mkdir()
814+
pathlib.Path("b/c.txt").write_text("")
815+
816+
assert cel_expression(
817+
ROOT_DIR,
818+
files=[
819+
pathlib.Path("a"),
820+
pathlib.Path("b") / "c.txt"
821+
]) == '"a/***".pathChanged() || "b/c.txt".pathChanged()'

scripts/__init__.py

Whitespace-only changes.

scripts/sandbox.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ def main() -> int:
3838
print("must give a `{};` parameter that will be replaced with new build context")
3939
return 1
4040

41-
if not (ROOT_DIR / "bin/buildinputs").exists():
42-
subprocess.check_call([MAKE, "bin/buildinputs"], cwd=ROOT_DIR)
43-
stdout = subprocess.check_output([ROOT_DIR / "bin/buildinputs", str(args.dockerfile)],
44-
text=True, cwd=ROOT_DIR)
45-
prereqs = [pathlib.Path(file) for file in json.loads(stdout)] if stdout != "\n" else []
46-
print(f"{prereqs=}")
41+
prereqs = buildinputs(dockerfile=args.dockerfile)
4742

4843
with tempfile.TemporaryDirectory(delete=True) as tmpdir:
4944
setup_sandbox(prereqs, pathlib.Path(tmpdir))
@@ -57,6 +52,16 @@ def main() -> int:
5752
return 0
5853

5954

55+
def buildinputs(dockerfile: pathlib.Path | str) -> list[pathlib.Path]:
56+
if not (ROOT_DIR / "bin/buildinputs").exists():
57+
subprocess.check_call([MAKE, "bin/buildinputs"], cwd=ROOT_DIR)
58+
stdout = subprocess.check_output([ROOT_DIR / "bin/buildinputs", str(dockerfile)],
59+
text=True, cwd=ROOT_DIR)
60+
prereqs = [pathlib.Path(file) for file in json.loads(stdout)] if stdout != "\n" else []
61+
print(f"{prereqs=}")
62+
return prereqs
63+
64+
6065
def setup_sandbox(prereqs: list[pathlib.Path], tmpdir: pathlib.Path):
6166
# always adding .gitignore
6267
gitignore = ROOT_DIR / ".gitignore"

0 commit comments

Comments
 (0)