Skip to content

Commit e1b5fbd

Browse files
authored
feat(backend): better for logging to detect issues with static files discovery (#11067)
* reduce noise in docker * refactor path infos * add more info during local frontend build * add frontend info during release build * ignore "special" import * change var name
1 parent a74d809 commit e1b5fbd

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

.github/workflows/release.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ jobs:
6565
run: cd src/backend/InvenTree/web/static/web/.vite && echo "${REF_NAME}" > tag.txt
6666
env:
6767
REF_NAME: ${{ github.ref_name }}
68+
- name: Write version file - SOURCE
69+
run: cd src/backend/InvenTree/web/static/web/.vite && echo "GitHub Actions build on $(date --utc +%Y-%m-%dT%H:%M:%SZ)" > source.txt
6870
- name: Zip frontend
6971
run: |
7072
cd src/backend/InvenTree/web/static/web

src/backend/InvenTree/InvenTree/version.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from datetime import datetime as dt
1313
from datetime import timedelta as td
1414

15+
from django.conf import settings
16+
1517
from .api_version import INVENTREE_API_TEXT, INVENTREE_API_VERSION
1618

1719
# InvenTree software version
@@ -22,6 +24,7 @@
2224

2325
logger = logging.getLogger('inventree')
2426

27+
git_warning_txt = 'INVE-W3: Could not detect git information.'
2528

2629
# Discover git
2730
try:
@@ -33,8 +36,11 @@
3336
main_repo = Repo(pathlib.Path(__file__).parent.parent.parent.parent.parent)
3437
main_commit = main_repo[main_repo.head()]
3538
except NotGitRepository:
36-
# If we are running in a docker container, the repo may not be available
37-
logger.warning('INVE-W3: Could not detect git information.')
39+
# If we are running in a docker container, the repo may not be available, only logging as warning if not in docker
40+
if settings.DOCKER:
41+
logger.info(git_warning_txt)
42+
else:
43+
logger.warning(git_warning_txt)
3844
main_repo = None
3945
main_commit = None
4046

@@ -51,7 +57,7 @@
5157
main_commit = None
5258
main_branch = None
5359
except Exception as exc:
54-
logger.warning('INVE-W3: Could not detect git information.', exc_info=exc)
60+
logger.warning(git_warning_txt, exc_info=exc)
5561
main_repo = None
5662
main_commit = None
5763
main_branch = None

tasks.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tasks for automating certain actions and interacting with InvenTree from the CLI."""
22

3+
import datetime
34
import json
45
import os
56
import pathlib
@@ -357,6 +358,26 @@ def manage_py_path():
357358
return manage_py_dir().joinpath('manage.py')
358359

359360

361+
def _frontend_info():
362+
"""Return the path of the frontend info directory."""
363+
return manage_py_dir().joinpath('web', 'static', 'web', '.vite')
364+
365+
366+
def version_target_pth():
367+
"""Return the path of the target version file."""
368+
return _frontend_info().joinpath('tag.txt')
369+
370+
371+
def version_sha_pth():
372+
"""Return the path of the SHA version file."""
373+
return _frontend_info().joinpath('sha.txt')
374+
375+
376+
def version_source_pth():
377+
"""Return the path of the source version file."""
378+
return _frontend_info().joinpath('source.txt')
379+
380+
360381
# endregion
361382

362383
if __name__ in ['__main__', 'tasks']:
@@ -1664,6 +1685,31 @@ def frontend_build(c):
16641685
info('Building frontend')
16651686
yarn(c, 'yarn run build')
16661687

1688+
def write_info(path: Path, content: str):
1689+
"""Helper function to write version content to file after cleaning it if it exists."""
1690+
if path.exists():
1691+
path.unlink()
1692+
path.write_text(content, encoding='utf-8')
1693+
1694+
# Write version marker
1695+
try:
1696+
import src.backend.InvenTree.InvenTree.version as InvenTreeVersion # type: ignore[import]
1697+
1698+
if version_hash := InvenTreeVersion.inventreeCommitHash():
1699+
write_info(version_sha_pth(), version_hash)
1700+
elif version_tag := InvenTreeVersion.inventreeVersion():
1701+
write_info(version_target_pth(), version_tag)
1702+
else:
1703+
warning('No version information available to write frontend version marker')
1704+
1705+
# Write source marker
1706+
write_info(
1707+
version_source_pth(),
1708+
f'local build on {datetime.datetime.now().isoformat()}',
1709+
)
1710+
except Exception:
1711+
warning('Failed to write frontend version marker')
1712+
16671713

16681714
@task
16691715
def frontend_server(c):
@@ -1788,13 +1834,9 @@ def check_already_current(tag=None, sha=None):
17881834
ref = 'tag' if tag else 'commit'
17891835

17901836
if tag:
1791-
current = manage_py_dir().joinpath(
1792-
'web', 'static', 'web', '.vite', 'tag.txt'
1793-
)
1837+
current = version_target_pth()
17941838
elif sha:
1795-
current = manage_py_dir().joinpath(
1796-
'web', 'static', 'web', '.vite', 'sha.txt'
1797-
)
1839+
current = version_sha_pth()
17981840
else:
17991841
raise ValueError('Either tag or sha needs to be set')
18001842

0 commit comments

Comments
 (0)