Skip to content

Commit

Permalink
refactor: remove tag hack for version number (#2641)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabeeeenster authored Sep 11, 2023
1 parent 900d475 commit acbeefe
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/actions/api-deploy-ecs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ runs:
- name: Write git info to Docker image
run: |
echo ${{ github.sha }} > api/CI_COMMIT_SHA
echo '${{ github.ref_name }}' > api/IMAGE_TAG
shell: bash

- name: Configure AWS Credentials
Expand Down
3 changes: 1 addition & 2 deletions .github/actions/run-local-api/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ runs:

steps:
- name: Build temporary Docker image for running the API
working-directory: api
run: docker build -t flagsmith/flagsmith-api:e2e-${{ github.sha }} .
run: docker build -t flagsmith/flagsmith-api:e2e-${{ github.sha }} -f api/Dockerfile .
shell: bash

- name: Run the API
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/api-docker-publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ jobs:
- name: Write git info to Docker image
run: |
echo ${{ github.sha }} > api/CI_COMMIT_SHA
echo '${{ github.ref_name }}' > api/IMAGE_TAG
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -50,6 +49,6 @@ jobs:
with:
platforms: linux/amd64,linux/arm64
file: api/Dockerfile
context: api/
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
2 changes: 0 additions & 2 deletions .github/workflows/frontend-docker-publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
run: |
cd frontend
echo ${{ github.sha }} > CI_COMMIT_SHA
echo '${{ github.ref_name }}' > IMAGE_TAG
- name: Download features
run: >
Expand All @@ -56,6 +55,5 @@ jobs:
with:
platforms: linux/amd64,linux/arm64
file: frontend/Dockerfile
context: frontend/
push: true
tags: ${{ steps.meta.outputs.tags }}
1 change: 0 additions & 1 deletion .github/workflows/frontend-e2e-docker-publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ jobs:
uses: docker/build-push-action@v2
with:
file: frontend/Dockerfile.e2e
context: frontend/
push: true
tags: ${{ steps.meta.outputs.tags }}
build-args: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ jobs:
run: |
cd api
echo ${{ github.sha }} > CI_COMMIT_SHA
echo '${{ steps.meta.outputs.tags }}' > IMAGE_TAG
echo '' > ENTERPRISE_VERSION
- name: Docker metadata
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/platform-docker-publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
- name: Write git info to Docker image
run: |
echo ${{ github.sha }} > api/CI_COMMIT_SHA
echo '${{ github.ref_name }}' > api/IMAGE_TAG
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -47,6 +46,5 @@ jobs:
with:
platforms: linux/amd64,linux/arm64
file: Dockerfile
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
2 changes: 0 additions & 2 deletions .github/workflows/platform-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ jobs:
uses: docker/build-push-action@v3
with:
file: api/Dockerfile
context: api/
push: false
tags: flagsmith/flagsmith-api:testing

Expand All @@ -166,6 +165,5 @@ jobs:
uses: docker/build-push-action@v3
with:
file: frontend/Dockerfile
context: frontend/
push: false
tags: flagsmith/flagsmith-frontend:testing
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ checkstyle.txt

# These get baked into the docker container on build
src/CI_COMMIT_SHA
src/IMAGE_TAG


# Web
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ COPY --from=build-python /usr/local/lib/python3.11/site-packages /usr/local/lib/
COPY --from=build-python /usr/local/bin /usr/local/bin

COPY api /app/
COPY .release-please-manifest.json /app/.versions.json

# Compile static Django assets
RUN python /app/manage.py collectstatic --no-input
Expand Down
5 changes: 3 additions & 2 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FROM python:3.11 as build-python
WORKDIR /app

COPY pyproject.toml poetry.lock Makefile ./
COPY api/pyproject.toml api/poetry.lock api/Makefile ./

ARG POETRY_VIRTUALENVS_CREATE=false
RUN make install-poetry
Expand All @@ -29,7 +29,8 @@ COPY --from=build-python /usr/local/lib/python3.11/site-packages /usr/local/lib/
# Copy the bin folder as well to copy the executables created in package installation
COPY --from=build-python /usr/local/bin /usr/local/bin

COPY . .
COPY api/ .
COPY .release-please-manifest.json ./.versions.json

# Compile static Django assets
RUN python manage.py collectstatic --no-input
Expand Down
20 changes: 17 additions & 3 deletions api/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
import pathlib

import shortuuid

UNKNOWN = "unknown"
VERSIONS_INFO_FILE_LOCATION = ".versions.json"


def create_hash():
"""Helper function to create a short hash"""
Expand All @@ -10,9 +14,19 @@ def create_hash():

def get_version_info() -> dict:
"""Reads the version info baked into src folder of the docker container"""
version_json = {
version_json = {}
image_tag = UNKNOWN

manifest_versions_content: str = _get_file_contents(VERSIONS_INFO_FILE_LOCATION)

if manifest_versions_content != UNKNOWN:
manifest_versions = json.loads(manifest_versions_content)
version_json["package_versions"] = manifest_versions
image_tag = manifest_versions["."]

version_json = version_json | {
"ci_commit_sha": _get_file_contents("./CI_COMMIT_SHA"),
"image_tag": _get_file_contents("./IMAGE_TAG"),
"image_tag": image_tag,
"is_enterprise": pathlib.Path("./ENTERPRISE_VERSION").exists(),
}

Expand All @@ -25,4 +39,4 @@ def _get_file_contents(file_path: str) -> str:
with open(file_path) as f:
return f.read().replace("\n", "")
except FileNotFoundError:
return "unknown"
return UNKNOWN
36 changes: 34 additions & 2 deletions api/tests/unit/app/test_unit_app_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import pathlib
from unittest import mock

from pytest_mock import MockerFixture

Expand All @@ -19,15 +21,45 @@ def path_side_effect(file_path: str) -> mocker.MagicMock:

mocked_pathlib.Path.side_effect = path_side_effect

manifest_mocked_file = {
".": "2.66.2",
}
mock_get_file_contents = mocker.patch("app.utils._get_file_contents")
mock_get_file_contents.side_effect = ("some_sha", "v1.0.0")
mock_get_file_contents.side_effect = (json.dumps(manifest_mocked_file), "some_sha")

# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "some_sha",
"image_tag": "v1.0.0",
"image_tag": "2.66.2",
"is_enterprise": True,
"package_versions": {".": "2.66.2"},
}


def test_get_version_info_with_missing_files(mocker: MockerFixture) -> None:
# Given
mocked_pathlib = mocker.patch("app.utils.pathlib")

def path_side_effect(file_path: str) -> mocker.MagicMock:
mocked_path_object = mocker.MagicMock(spec=pathlib.Path)

if file_path == "./ENTERPRISE_VERSION":
mocked_path_object.exists.return_value = True

return mocked_path_object

mocked_pathlib.Path.side_effect = path_side_effect
mock.mock_open.side_effect = IOError

# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
"is_enterprise": True,
}
3 changes: 2 additions & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ USER node

WORKDIR /srv/bt

COPY --chown=node:node . .
COPY --chown=node:node frontend .
COPY .release-please-manifest.json .

RUN npm ci --quiet --production
ENV ENV=prod
Expand Down
4 changes: 3 additions & 1 deletion frontend/Dockerfile.e2e
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ FROM ghcr.io/flagsmith/e2e-frontend-base:latest

# Build Flagsmith
WORKDIR /srv/flagsmith
COPY . .
COPY frontend .
COPY .release-please-manifest.json ./.versions.json

RUN npm cache clean --force
RUN npm ci

Expand Down
10 changes: 4 additions & 6 deletions frontend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,13 @@ app.get('/version', (req, res) => {
}

try {
imageTag = fs
.readFileSync('IMAGE_TAG', 'utf8')
.replace(/(\r\n|\n|\r)/gm, '')
releasePleaseManifest = JSON.parse(fs.readFileSync('./.versions.json', 'utf8'))
res.send({ 'ci_commit_sha': commitSha, 'image_tag': releasePleaseManifest["."], 'package_versions': releasePleaseManifest })
} catch (err) {
// eslint-disable-next-line
console.log('Unable to read IMAGE_TAG')
console.log('Unable to read .versions.json file')
res.send({ 'ci_commit_sha': commitSha, 'image_tag': imageTag})
}

res.send({ 'ci_commit_sha': commitSha, 'image_tag': imageTag })
})

app.use(bodyParser.json())
Expand Down
4 changes: 2 additions & 2 deletions frontend/docker-compose-e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ services:

frontend:
build:
context: .
dockerfile: Dockerfile.e2e
context: ../
dockerfile: frontend/Dockerfile.e2e
platform: linux/amd64
environment:
E2E_TEST_TOKEN_DEV: some-token
Expand Down

3 comments on commit acbeefe

@vercel
Copy link

@vercel vercel bot commented on acbeefe Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs.bullet-train.io
docs-git-main-flagsmith.vercel.app
docs.flagsmith.com
docs-flagsmith.vercel.app

@vercel
Copy link

@vercel vercel bot commented on acbeefe Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on acbeefe Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.