Skip to content

Commit 0e192eb

Browse files
baogorekclaude
andcommitted
Bake git provenance into Modal images via env vars
.git is intentionally excluded from Modal images (size + cache invalidation). Capture GIT_COMMIT/GIT_BRANCH at image build time (locally) and bake via .env(). get_git_provenance() falls back to these env vars when git commands fail inside containers. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6879591 commit 0e192eb

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

modal_app/local_area.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import os
1515
import subprocess
16+
import subprocess as _sp
1617
import json
1718
import modal
1819
from pathlib import Path
@@ -34,6 +35,24 @@
3435
)
3536

3637
_REPO_ROOT = Path(__file__).resolve().parent.parent
38+
39+
_GIT_ENV = {}
40+
try:
41+
_GIT_ENV["GIT_COMMIT"] = (
42+
_sp.check_output(["git", "rev-parse", "HEAD"], stderr=_sp.DEVNULL)
43+
.decode()
44+
.strip()
45+
)
46+
_GIT_ENV["GIT_BRANCH"] = (
47+
_sp.check_output(
48+
["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=_sp.DEVNULL
49+
)
50+
.decode()
51+
.strip()
52+
)
53+
except Exception:
54+
pass
55+
3756
_IGNORE = [
3857
".git",
3958
"__pycache__",
@@ -60,6 +79,7 @@
6079
copy=True,
6180
ignore=_IGNORE,
6281
)
82+
.env(_GIT_ENV)
6383
.run_commands(
6484
"cd /root/policyengine-us-data && UV_HTTP_TIMEOUT=300 uv sync --frozen"
6585
)

modal_app/pipeline.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from typing import Optional
4545

4646
import modal
47+
import subprocess as _sp
4748

4849
# ── Modal resources ──────────────────────────────────────────────
4950

@@ -56,6 +57,24 @@
5657
staging_volume = modal.Volume.from_name("local-area-staging", create_if_missing=True)
5758

5859
_REPO_ROOT = Path(__file__).resolve().parent.parent
60+
61+
_GIT_ENV = {}
62+
try:
63+
_GIT_ENV["GIT_COMMIT"] = (
64+
_sp.check_output(["git", "rev-parse", "HEAD"], stderr=_sp.DEVNULL)
65+
.decode()
66+
.strip()
67+
)
68+
_GIT_ENV["GIT_BRANCH"] = (
69+
_sp.check_output(
70+
["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=_sp.DEVNULL
71+
)
72+
.decode()
73+
.strip()
74+
)
75+
except Exception:
76+
pass
77+
5978
_IGNORE = [
6079
".git",
6180
"__pycache__",
@@ -82,6 +101,7 @@
82101
copy=True,
83102
ignore=_IGNORE,
84103
)
104+
.env(_GIT_ENV)
85105
.run_commands(
86106
"cd /root/policyengine-us-data && UV_HTTP_TIMEOUT=300 uv sync --frozen"
87107
)

modal_app/remote_calibration_runner.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import subprocess
3+
import subprocess as _sp
34
import modal
45

56
app = modal.App("policyengine-us-data-fit-weights")
@@ -10,6 +11,24 @@
1011
from pathlib import Path
1112

1213
_REPO_ROOT = Path(__file__).resolve().parent.parent
14+
15+
_GIT_ENV = {}
16+
try:
17+
_GIT_ENV["GIT_COMMIT"] = (
18+
_sp.check_output(["git", "rev-parse", "HEAD"], stderr=_sp.DEVNULL)
19+
.decode()
20+
.strip()
21+
)
22+
_GIT_ENV["GIT_BRANCH"] = (
23+
_sp.check_output(
24+
["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=_sp.DEVNULL
25+
)
26+
.decode()
27+
.strip()
28+
)
29+
except Exception:
30+
pass
31+
1332
_IGNORE = [
1433
".git",
1534
"__pycache__",
@@ -36,6 +55,7 @@
3655
copy=True,
3756
ignore=_IGNORE,
3857
)
58+
.env(_GIT_ENV)
3959
.run_commands(
4060
"cd /root/policyengine-us-data && "
4161
"UV_HTTP_TIMEOUT=300 uv sync --frozen --extra l0"

policyengine_us_data/calibration/unified_calibration.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def get_git_provenance() -> dict:
9797
info["git_dirty"] = len(porcelain) > 0
9898
except Exception:
9999
pass
100+
import os
101+
102+
if not info["git_commit"]:
103+
info["git_commit"] = os.environ.get("GIT_COMMIT")
104+
if not info["git_branch"]:
105+
info["git_branch"] = os.environ.get("GIT_BRANCH")
100106
try:
101107
from policyengine_us_data.__version__ import __version__
102108

0 commit comments

Comments
 (0)