Skip to content

Commit

Permalink
Add Version and Builder Number to Docker image (#1085)
Browse files Browse the repository at this point in the history
- Adds the following build arguments:
  * `VERSION` – represents the version of the application (e.g.: semver). It is accessible across the app via `settings.APPLICATION_VERSION`.
  * `BUILD_NUMBER` – represents the build version of the application (e.g.: build hash). It is accessible across the app via `settings.APPLICATION_BUILD_NUMBER`
- With the added support for the build arguments, the version of the application can now be set during the docker image building process instead of bundling the version with the application.
  • Loading branch information
fmrsabino committed Apr 1, 2024
1 parent 209596e commit fbb334b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ jobs:
context: .
platforms: linux/amd64,linux/arm64
push: true
build-args: |
BUILD_NUMBER=${{ env.BUILD_NUMBER }}
VERSION=${{ github.ref_name }}
tags: safeglobal/safe-config-service:staging
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
Expand Down Expand Up @@ -215,6 +218,9 @@ jobs:
context: .
platforms: linux/amd64,linux/arm64
push: true
build-args: |
BUILD_NUMBER=${{ env.BUILD_NUMBER }}
VERSION=${{ github.ref_name }}
tags: |
safeglobal/safe-config-service:${{ github.event.release.tag_name }}
safeglobal/safe-config-service:latest
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ FROM python:3.12.2-slim

# python
ENV PYTHONUNBUFFERED=1

ENV PYTHONUSERBASE=/python-deps
ENV PATH="${PATH}:${PYTHONUSERBASE}/bin"

ARG VERSION
ARG BUILD_NUMBER
ENV APPLICATION_VERSION=${VERSION} \
APPLICATION_BUILD_NUMBER=${BUILD_NUMBER}

WORKDIR /app
COPY requirements.txt ./

Expand Down
24 changes: 21 additions & 3 deletions src/about/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
from django.test import override_settings
from django.urls import reverse
from faker import Faker
from rest_framework import status
from rest_framework.test import APITestCase

faker = Faker()


def generate_semver(
min_major=0, max_major=10, min_minor=0, max_minor=10, min_patch=0, max_patch=10
):
major = faker.random_int(min=min_major, max=max_major)
minor = faker.random_int(min=min_minor, max=max_minor)
patch = faker.random_int(min=min_patch, max=max_patch)
return f"{major}.{minor}.{patch}"


semver = generate_semver()


@override_settings(APPLICATION_VERSION=semver)
class AboutJsonPayloadFormatViewTests(APITestCase):
def test_json_payload_format(self):
url = reverse("v1:about:detail")
expected_json_response = {
"name": "Safe Config Service",
"version": "2.75.0",
"version": semver,
"apiVersion": "v1",
"secure": False,
}
Expand All @@ -19,12 +36,13 @@ def test_json_payload_format(self):
self.assertEqual(response.json(), expected_json_response)


@override_settings(APPLICATION_VERSION=semver)
class AboutSecureRequestViewTests(APITestCase):
def test_https_request(self):
url = reverse("v1:about:detail")
expected_json_response = {
"name": "Safe Config Service",
"version": "2.75.0",
"version": semver,
"api_version": "v1",
"secure": True,
}
Expand All @@ -38,7 +56,7 @@ def test_http_request(self):
url = reverse("v1:about:detail")
expected_json_response = {
"name": "Safe Config Service",
"version": "2.75.0",
"version": semver,
"api_version": "v1",
"secure": False,
}
Expand Down
5 changes: 3 additions & 2 deletions src/about/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from django.conf import settings
from rest_framework.response import Response
from rest_framework.views import APIView

from version import __name__, __version__
from version import __name__


class AboutView(APIView):
def get(self, request, format=None):
response = {
"name": __name__,
"version": __version__,
"version": settings.APPLICATION_VERSION,
"api_version": self.request.version,
"secure": self.request.is_secure(),
}
Expand Down
3 changes: 3 additions & 0 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

# Application definition

APPLICATION_VERSION = os.getenv("APPLICATION_VERSION")
APPLICATION_BUILD_NUMBER = os.getenv("APPLICATION_BUILD_NUMBER")

REST_FRAMEWORK = {
# https://www.django-rest-framework.org/api-guide/renderers/
"DEFAULT_RENDERER_CLASSES": [
Expand Down
1 change: 0 additions & 1 deletion src/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
__name__ = "Safe Config Service"
__version__ = "2.75.0"

0 comments on commit fbb334b

Please sign in to comment.