diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8eb42ed..6f1d3d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -252,6 +252,11 @@ jobs: type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} + - name: Store git commit and build date + run: | + echo "GIT_COMMIT=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV" + echo "BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_ENV" + - name: Build and push Docker images uses: docker/build-push-action@v4 with: @@ -260,6 +265,9 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + GIT_COMMIT + BUILD_DATE cache-from: | type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache-amd64 type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache-arm64 diff --git a/Dockerfile b/Dockerfile index 42bd1bc..adbbb03 100644 --- a/Dockerfile +++ b/Dockerfile @@ -59,6 +59,11 @@ WORKDIR /home/app/spoolman ENV PATH="/home/app/spoolman/.venv/bin:${PATH}" ENV PYTHONPATH="/home/app/spoolman:${PYTHONPATH}" +ARG GIT_COMMIT=unknown +ARG BUILD_DATE=unknown +ENV GIT_COMMIT=${GIT_COMMIT} +ENV BUILD_DATE=${BUILD_DATE} + # Run command EXPOSE 8000 VOLUME ["/home/app/.local/share/spoolman"] diff --git a/spoolman/api/v1/models.py b/spoolman/api/v1/models.py index 4a003c3..ba4b051 100644 --- a/spoolman/api/v1/models.py +++ b/spoolman/api/v1/models.py @@ -197,6 +197,8 @@ class Info(BaseModel): data_dir: str = Field(example="/home/app/.local/share/spoolman") backups_dir: str = Field(example="/home/app/.local/share/spoolman/backups") db_type: str = Field(example="sqlite") + git_commit: Optional[str] = Field(example="a1b2c3d") + build_date: Optional[datetime] = Field(example="2021-01-01T00:00:00Z") class HealthCheck(BaseModel): diff --git a/spoolman/api/v1/router.py b/spoolman/api/v1/router.py index 00e8f05..57ca61c 100644 --- a/spoolman/api/v1/router.py +++ b/spoolman/api/v1/router.py @@ -43,6 +43,8 @@ async def info() -> models.Info: data_dir=str(env.get_data_dir().resolve()), backups_dir=str(env.get_backups_dir().resolve()), db_type=str(env.get_database_type() or "sqlite"), + git_commit=env.get_commit_hash(), + build_date=env.get_build_date(), ) diff --git a/spoolman/env.py b/spoolman/env.py index 31e2afa..2330b1d 100644 --- a/spoolman/env.py +++ b/spoolman/env.py @@ -2,6 +2,7 @@ import logging import os +from datetime import datetime from enum import Enum from pathlib import Path from typing import Optional @@ -245,3 +246,29 @@ def get_version() -> str: str: The version. """ return pkg_resources.get_distribution("spoolman").version + + +def get_commit_hash() -> Optional[str]: + """Get the latest commit hash of the package. + + Can end with "-dirty" if there are uncommitted changes. + + Returns: + Optional[str]: The commit hash. + """ + commit_hash = os.getenv("GIT_COMMIT", "unknown") + if commit_hash == "unknown": + return None + return commit_hash + + +def get_build_date() -> Optional[datetime]: + """Get the build date of the package. + + Returns: + Optional[datetime.datetime]: The build date. + """ + build_date = os.getenv("BUILD_DATE", "unknown") + if build_date == "unknown": + return None + return datetime.fromisoformat(build_date) diff --git a/spoolman/main.py b/spoolman/main.py index b04dac4..c52ab12 100644 --- a/spoolman/main.py +++ b/spoolman/main.py @@ -63,7 +63,12 @@ if env.is_debug_mode(): @app.on_event("startup") async def startup() -> None: """Run the service's startup sequence.""" - logger.info("Starting Spoolman v%s...", app.version) + logger.info( + "Starting Spoolman v%s (commit: %s) (built: %s)", + app.version, + env.get_commit_hash(), + env.get_build_date(), + ) logger.info("Setting up database...") database.setup_db(database.get_connection_url())