Skip to content

Commit

Permalink
feature: Automatically pass SOURCE_DATE_EPOCH to Linux containers
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Rickerby <[email protected]>
  • Loading branch information
dalcinl and joerick committed Sep 6, 2023
1 parent e8bb208 commit 2372220
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions cibuildwheel/oci_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def __enter__(self) -> OCIContainer:
self.engine.name,
"create",
"--env=CIBUILDWHEEL",
"--env=SOURCE_DATE_EPOCH",
f"--name={self.name}",
"--interactive",
"--volume=/:/host", # ignored on CircleCI
Expand Down
3 changes: 3 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ A list of environment variables to pass into the linux container during the buil

To specify more than one environment variable, separate the variable names by spaces.

!!! note
cibuildwheel automatically passes the environment variable [`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/) if defined.

#### Examples

!!! tab examples "Environment passthrough"
Expand Down
30 changes: 29 additions & 1 deletion unit_test/oci_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import subprocess
import textwrap
from contextlib import contextmanager
from pathlib import Path, PurePath, PurePosixPath

import pytest
Expand Down Expand Up @@ -42,6 +43,20 @@ def container_engine(request):
return OCIContainerEngineConfig(name=request.param)


@contextmanager
def modified_os_environ(**env):
env_save = {key: os.environ.get(key) for key in env}
try:
os.environ.update(env)
yield
finally:
for key, value in env_save.items():
if value is None:
del os.environ[key]
else:
os.environ[key] = value


# Tests


Expand All @@ -63,13 +78,26 @@ def test_debug_info(container_engine):


def test_environment(container_engine):
with OCIContainer(engine=container_engine, image=DEFAULT_IMAGE) as container:
with modified_os_environ(CIBUILDWHEEL="1", SOURCE_DATE_EPOCH="42"), \
OCIContainer(engine=container_engine, image=DEFAULT_IMAGE) as container:
assert (
container.call(
["sh", "-c", "echo $TEST_VAR"], env={"TEST_VAR": "1"}, capture_output=True
)
== "1\n"
)
assert (
container.call(
["sh", "-c", "echo $CIBUILDWHEEL"], capture_output=True
)
== "1\n"
)
assert (
container.call(
["sh", "-c", "echo $SOURCE_DATE_EPOCH"], capture_output=True
)
== "42\n"
)


def test_cwd(container_engine):
Expand Down

0 comments on commit 2372220

Please sign in to comment.