Skip to content

Commit b48317c

Browse files
authored
Merge pull request #1248 from mokibit/add-short-syntax-for-env-variables
Implement short syntax for env variables in compose.yml "environment:"
2 parents 7105198 + 0cbf70a commit b48317c

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implemented short syntax for environment variables set in `.env` for compose.yml "environment:" section.

podman_compose.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,14 @@ async def container_to_args(
11591159
podman_args.extend(["-e", e])
11601160
env = norm_as_list(cnt.get("environment", {}))
11611161
for e in env:
1162-
podman_args.extend(["-e", e])
1162+
# new environment variable is set
1163+
if "=" in e:
1164+
podman_args.extend(["-e", e])
1165+
else:
1166+
# environment variable already exists in environment so pass its value
1167+
if e in compose.environ.keys():
1168+
podman_args.extend(["-e", f"{e}={compose.environ[e]}"])
1169+
11631170
tmpfs_ls = cnt.get("tmpfs", [])
11641171
if isinstance(tmpfs_ls, str):
11651172
tmpfs_ls = [tmpfs_ls]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ZZVAR1='This value is loaded but should be overwritten'
22
ZZVAR2='This value is loaded from .env in project/ directory'
3+
ZZVAR3=TEST
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
app:
3+
image: nopush/podman-compose-test
4+
command: ["/bin/busybox", "sh", "-c", "env | grep ZZVAR3"]
5+
# 'env_file:' section is not used, so .env file is searched in the same directory as compose.yml
6+
# file
7+
environment:
8+
# this is short syntax: podman-compose takes only this variable value from '.env' file and
9+
# sends it to container environment
10+
- ZZVAR3

tests/integration/env_file_tests/test_podman_compose_env_file.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def test_taking_env_variables_from_env_files_from_different_directories(self) ->
233233
[
234234
'ZZVAR1=This value is loaded but should be overwritten\r',
235235
'ZZVAR2=This value is loaded from .env in project/ directory\r',
236-
'ZZVAR3=\r',
236+
'ZZVAR3=TEST\r',
237237
'',
238238
],
239239
)
@@ -244,3 +244,36 @@ def test_taking_env_variables_from_env_files_from_different_directories(self) ->
244244
path_compose_file,
245245
"down",
246246
])
247+
248+
def test_env_var_value_accessed_in_compose_file_short_syntax(self) -> None:
249+
# Test that compose file can access the environment variable set in .env file using
250+
# short syntax, that is: only the name of environment variable is used in "environment:" in
251+
# compose.yml file and its value is picked up directly from .env file
252+
# long syntax of environment variables interpolation is tested in
253+
# tests/integration/interpolation
254+
255+
base_path = compose_base_path()
256+
compose_file_path = os.path.join(base_path, "project/container-compose.short_syntax.yaml")
257+
try:
258+
self.run_subprocess_assert_returncode([
259+
podman_compose_path(),
260+
"-f",
261+
compose_file_path,
262+
"up",
263+
"-d",
264+
])
265+
output, _ = self.run_subprocess_assert_returncode([
266+
podman_compose_path(),
267+
"-f",
268+
compose_file_path,
269+
"logs",
270+
])
271+
# ZZVAR3 was set in .env file
272+
self.assertEqual(output, b"ZZVAR3=TEST\n")
273+
finally:
274+
self.run_subprocess_assert_returncode([
275+
podman_compose_path(),
276+
"-f",
277+
compose_file_path,
278+
"down",
279+
])

tests/unit/test_container_to_args.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,42 @@ async def test_no_hosts_extension(self) -> None:
262262
],
263263
)
264264

265+
@parameterized.expand([
266+
# short syntax: only take this specific environment variable value from .env file
267+
("use_env_var_from_default_env_file_short_syntax", ["ZZVAR1"], "ZZVAR1=TEST1"),
268+
# long syntax: environment variable value from .env file is taken through variable
269+
# interpolation
270+
# only the value required in 'environment:' compose file is sent to containers
271+
# environment
272+
("use_env_var_from_default_env_file_long_syntax", ["ZZVAR1=TEST1"], "ZZVAR1=TEST1"),
273+
# "environment:" section in compose file overrides environment variable value from .env file
274+
(
275+
"use_env_var_from_default_env_file_override_value",
276+
["ZZVAR1=NEW_TEST1"],
277+
"ZZVAR1=NEW_TEST1",
278+
),
279+
])
280+
async def test_env_file(self, test_name: str, cnt_env: list, expected_var: str) -> None:
281+
c = create_compose_mock()
282+
# environment variables were set in .env file
283+
c.environ = {"ZZVAR1": "TEST1", "ZZVAR2": "TEST2"}
284+
285+
cnt = get_minimal_container()
286+
cnt["environment"] = cnt_env
287+
288+
args = await container_to_args(c, cnt)
289+
self.assertEqual(
290+
args,
291+
[
292+
"--name=project_name_service_name1",
293+
"-d",
294+
"-e",
295+
f"{expected_var}",
296+
"--network=bridge:alias=service_name",
297+
"busybox",
298+
],
299+
)
300+
265301
async def test_env_file_str(self) -> None:
266302
c = create_compose_mock()
267303

0 commit comments

Comments
 (0)