Skip to content

Commit

Permalink
Added git commit and build date env and API
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed Sep 17, 2023
1 parent a1a4ad6 commit 1a3efd9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions spoolman/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions spoolman/api/v1/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)


Expand Down
27 changes: 27 additions & 0 deletions spoolman/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import os
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -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)
7 changes: 6 additions & 1 deletion spoolman/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@
@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())
Expand Down

0 comments on commit 1a3efd9

Please sign in to comment.