Skip to content

Commit 8f618b6

Browse files
authored
Merge pull request #763 from otto-liljalaakso-nt/additional_contexts
Support additional_contexts
2 parents 3bb305c + cac836b commit 8f618b6

File tree

8 files changed

+91
-0
lines changed

8 files changed

+91
-0
lines changed

podman_compose.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,12 @@ def normalize_service(service, sub_dir=""):
14641464
if not context:
14651465
context = "."
14661466
service["build"]["context"] = context
1467+
if "build" in service and "additional_contexts" in service["build"]:
1468+
if is_dict(build["additional_contexts"]):
1469+
new_additional_contexts = []
1470+
for k, v in build["additional_contexts"].items():
1471+
new_additional_contexts.append(f"{k}={v}")
1472+
build["additional_contexts"] = new_additional_contexts
14671473
for key in ("command", "entrypoint"):
14681474
if key in service:
14691475
if is_str(service[key]):
@@ -2330,6 +2336,8 @@ async def build_one(compose, args, cnt):
23302336
build_args.extend(get_secret_args(compose, cnt, secret, podman_is_building=True))
23312337
for tag in build_desc.get("tags", []):
23322338
build_args.extend(["-t", tag])
2339+
for additional_ctx in build_desc.get("additional_contexts", {}):
2340+
build_args.extend([f"--build-context={additional_ctx}"])
23332341
if "target" in build_desc:
23342342
build_args.extend(["--target", build_desc["target"]])
23352343
container_to_ulimit_build_args(cnt, build_args)

pytests/test_normalize_service.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class TestNormalizeService(unittest.TestCase):
2020
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}},
2121
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}},
2222
),
23+
(
24+
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}},
25+
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}},
26+
),
27+
(
28+
{"build": {"additional_contexts": {"ctx": "../ctx", "ctx2": "../ctx2"}}},
29+
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}},
30+
),
2331
])
2432
def test_simple(self, input, expected):
2533
self.assertEqual(normalize_service(input), expected)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Test podman-compose with build.additional_contexts
2+
3+
```
4+
podman-compose build
5+
podman-compose up
6+
podman-compose down
7+
```
8+
9+
expected output would be
10+
11+
```
12+
[dict] | Data for dict
13+
[list] | Data for list
14+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Data for dict
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Data for list
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM busybox
2+
COPY --from=data data.txt /data/data.txt
3+
CMD ["busybox", "cat", "/data/data.txt"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3.7"
2+
services:
3+
dict:
4+
build:
5+
context: .
6+
additional_contexts:
7+
data: ../data_for_dict
8+
list:
9+
build:
10+
context: .
11+
additional_contexts:
12+
- data=../data_for_list
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
4+
"""Test how additional contexts are passed to podman."""
5+
6+
import os
7+
import subprocess
8+
import unittest
9+
10+
from .test_podman_compose import podman_compose_path
11+
from .test_podman_compose import test_path
12+
13+
14+
def compose_yaml_path():
15+
""" "Returns the path to the compose file used for this test module"""
16+
return os.path.join(test_path(), "additional_contexts", "project")
17+
18+
19+
class TestComposeBuildAdditionalContexts(unittest.TestCase):
20+
def test_build_additional_context(self):
21+
"""podman build should receive additional contexts as --build-context
22+
23+
See additional_context/project/docker-compose.yaml for context paths
24+
"""
25+
cmd = (
26+
"coverage",
27+
"run",
28+
podman_compose_path(),
29+
"--dry-run",
30+
"--verbose",
31+
"-f",
32+
os.path.join(compose_yaml_path(), "docker-compose.yml"),
33+
"build",
34+
)
35+
p = subprocess.run(
36+
cmd,
37+
stdout=subprocess.PIPE,
38+
check=False,
39+
stderr=subprocess.STDOUT,
40+
text=True,
41+
)
42+
self.assertEqual(p.returncode, 0)
43+
self.assertIn("--build-context=data=../data_for_dict", p.stdout)
44+
self.assertIn("--build-context=data=../data_for_list", p.stdout)

0 commit comments

Comments
 (0)