Skip to content

Commit

Permalink
feat: Log commit sha (#537)
Browse files Browse the repository at this point in the history
# Motivation

To be able to link a pipeline rune with the code that has executed it,
we want to log the git sha of the last commit in the pipeline logfile.
  • Loading branch information
robinholzi authored Jun 21, 2024
1 parent beb3769 commit 3dadeac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions modyn/supervisor/internal/pipeline_executor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from modyn.supervisor.internal.eval.handler import EvalRequest
from modyn.supervisor.internal.grpc.enums import PipelineStage
from modyn.supervisor.internal.utils.evaluation_status_reporter import EvaluationStatusReporter
from modyn.supervisor.internal.utils.git_utils import get_head_sha
from pydantic import BaseModel, Field, model_serializer, model_validator
from typing_extensions import override

Expand Down Expand Up @@ -472,6 +473,7 @@ class PipelineLogs(BaseModel):
# static logs

pipeline_id: int
commit_sha: str | None = Field(None, description="The commit SHA that the pipeline was executed on.")
pipeline_stages: dict[str, tuple[int, list[str]]]
"""List of all pipeline stages, their execution order index, and their parent stages."""

Expand Down Expand Up @@ -516,6 +518,11 @@ def materialize(self, log_dir_path: Path, mode: Literal["initial", "increment",
# output logs

if mode == "initial":
try:
self.commit_sha = get_head_sha()
except Exception as e: # pylint: disable=broad-except
logger.warning(f"Failed to get commit SHA: {e}")

with open(pipeline_logdir / "pipeline.part.log", "w", encoding="utf-8") as logfile:
logfile.write(self.model_dump_json(indent=2, exclude={"supervisor", "partial_idx"}, by_alias=True))
return
Expand Down
12 changes: 12 additions & 0 deletions modyn/supervisor/internal/utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import subprocess
from pathlib import Path


def get_head_sha() -> str:
"""When launched in a git repository, return the SHA of the current HEAD."""

result = subprocess.run(
["git", "rev-parse", "HEAD"], capture_output=True, text=True, check=True, cwd=Path(__file__).parent
)
assert result.returncode == 0, f"Failed to get HEAD SHA: {result.stderr}, {result.stdout}"
return result.stdout.strip()

0 comments on commit 3dadeac

Please sign in to comment.