Added git commit and build date env and API
This commit is contained in:
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
@@ -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
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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(),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user