Skip to content

Commit 8c85560

Browse files
committed
Fancify version string
Use git rev-parse and describe to pull commit sha and tag information for use in version string.
1 parent 20ac24a commit 8c85560

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
3333
RUN ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
3434

3535
# Set version for apm
36-
RUN echo "export DD_VERSION=$(git rev-parse --short HEAD)" >> ~/.bashrc
36+
RUN echo "export DD_VERSION=$(python3 packet/git.py)" >> /tmp/version
3737

38-
CMD ["/bin/bash", "-c", "source ~/.bashrc && ddtrace-run gunicorn packet:app --bind=0.0.0.0:8080 --access-logfile=- --timeout=600"]
38+
CMD ["/bin/bash", "-c", "source /tmp/version && ddtrace-run gunicorn packet:app --bind=0.0.0.0:8080 --access-logfile=- --timeout=600"]

packet/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from sentry_sdk.integrations.flask import FlaskIntegration
2020
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
2121

22+
from .git import get_version
23+
2224
app = Flask(__name__)
2325
gzip = Gzip(app)
2426

@@ -31,9 +33,8 @@
3133
if os.path.exists(_pyfile_config):
3234
app.config.from_pyfile(_pyfile_config)
3335

34-
# Fetch the version number from the npm package file
35-
with open(os.path.join(_root_dir, 'package.json')) as package_file:
36-
app.config['VERSION'] = json.load(package_file)['version']
36+
# Fetch the version number
37+
app.config['VERSION'] = get_version()
3738

3839
# Logger configuration
3940
logging.getLogger().setLevel(app.config['LOG_LEVEL'])

packet/git.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import json
2+
import os
3+
import subprocess
4+
5+
def get_short_sha(commit_ish: str = 'HEAD'):
6+
"""
7+
Get the short hash of a commit-ish
8+
Returns '' if unfound
9+
"""
10+
11+
try:
12+
rev_parse = subprocess.run(f'git rev-parse --short {commit_ish}'.split(), capture_output=True, check=True)
13+
return rev_parse.stdout.decode('utf-8').strip()
14+
except subprocess.CalledProcessError:
15+
return ''
16+
17+
def get_tag(commit_ish: str = 'HEAD'):
18+
"""
19+
Get the name of the tag at a given commit-ish
20+
Returns '' if untagged
21+
"""
22+
23+
try:
24+
describe = subprocess.run(f'git describe --exact-match {commit_ish}'.split(), capture_output=True, check=True)
25+
return describe.stdout.decode('utf-8').strip()
26+
except subprocess.CalledProcessError:
27+
return ''
28+
29+
def get_version(commit_ish: str = 'HEAD'):
30+
"""
31+
Get the version string of a commit-ish
32+
33+
If we have a commit and the commit is tagged, version is `tag (commit-sha)`
34+
If we have a commit but not a tag, version is `commit-sha`
35+
If we have neither, version is the version field of package.json
36+
"""
37+
38+
if sha := get_short_sha(commit_ish):
39+
if tag := get_tag(commit_ish):
40+
return f'{tag} ({sha})'
41+
else:
42+
return sha
43+
else:
44+
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
45+
with open(os.path.join(root_dir, 'package.json')) as package_file:
46+
return json.load(package_file)['version']
47+
48+
if __name__ == '__main__':
49+
print(get_version())

0 commit comments

Comments
 (0)