diff --git a/.github/actions/api-deploy-ecs/action.yml b/.github/actions/api-deploy-ecs/action.yml index 6b062c949927..90c76a83ec1c 100644 --- a/.github/actions/api-deploy-ecs/action.yml +++ b/.github/actions/api-deploy-ecs/action.yml @@ -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 diff --git a/.github/actions/run-local-api/action.yml b/.github/actions/run-local-api/action.yml index 4551ec7b44f6..ae914a132549 100644 --- a/.github/actions/run-local-api/action.yml +++ b/.github/actions/run-local-api/action.yml @@ -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 diff --git a/.github/workflows/api-docker-publish-image.yml b/.github/workflows/api-docker-publish-image.yml index 1d292c7b9d7c..689706bcfe79 100644 --- a/.github/workflows/api-docker-publish-image.yml +++ b/.github/workflows/api-docker-publish-image.yml @@ -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 @@ -50,6 +49,6 @@ jobs: with: platforms: linux/amd64,linux/arm64 file: api/Dockerfile - context: api/ + context: . push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/frontend-docker-publish-image.yml b/.github/workflows/frontend-docker-publish-image.yml index 257e21d1cb6e..21634cff88cd 100644 --- a/.github/workflows/frontend-docker-publish-image.yml +++ b/.github/workflows/frontend-docker-publish-image.yml @@ -31,7 +31,6 @@ jobs: run: | cd frontend echo ${{ github.sha }} > CI_COMMIT_SHA - echo '${{ github.ref_name }}' > IMAGE_TAG - name: Download features run: > @@ -56,6 +55,5 @@ jobs: with: platforms: linux/amd64,linux/arm64 file: frontend/Dockerfile - context: frontend/ push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/frontend-e2e-docker-publish-image.yml b/.github/workflows/frontend-e2e-docker-publish-image.yml index a34a864b6e50..6ee965eb7fa8 100644 --- a/.github/workflows/frontend-e2e-docker-publish-image.yml +++ b/.github/workflows/frontend-e2e-docker-publish-image.yml @@ -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: | diff --git a/.github/workflows/platform-docker-publish-all-features-image.yml b/.github/workflows/platform-docker-publish-all-features-image.yml index bfb49e245d12..95e1d7da9529 100644 --- a/.github/workflows/platform-docker-publish-all-features-image.yml +++ b/.github/workflows/platform-docker-publish-all-features-image.yml @@ -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 diff --git a/.github/workflows/platform-docker-publish-image.yml b/.github/workflows/platform-docker-publish-image.yml index 0effd4708ac9..028ae8db39fe 100644 --- a/.github/workflows/platform-docker-publish-image.yml +++ b/.github/workflows/platform-docker-publish-image.yml @@ -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 @@ -47,6 +46,5 @@ jobs: with: platforms: linux/amd64,linux/arm64 file: Dockerfile - context: . push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/platform-pull-request.yml b/.github/workflows/platform-pull-request.yml index 1285ddede093..4e58373c1bc5 100644 --- a/.github/workflows/platform-pull-request.yml +++ b/.github/workflows/platform-pull-request.yml @@ -142,7 +142,6 @@ jobs: uses: docker/build-push-action@v3 with: file: api/Dockerfile - context: api/ push: false tags: flagsmith/flagsmith-api:testing @@ -166,6 +165,5 @@ jobs: uses: docker/build-push-action@v3 with: file: frontend/Dockerfile - context: frontend/ push: false tags: flagsmith/flagsmith-frontend:testing diff --git a/.gitignore b/.gitignore index 26793f55972c..c639e95f4aa8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ checkstyle.txt # These get baked into the docker container on build src/CI_COMMIT_SHA -src/IMAGE_TAG # Web diff --git a/Dockerfile b/Dockerfile index bec34a973f4d..18e29ea377b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/api/Dockerfile b/api/Dockerfile index 38899076991b..862e6e2bbd4b 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -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 @@ -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 diff --git a/api/app/utils.py b/api/app/utils.py index 4b223cf6ec0c..9611bc331aca 100644 --- a/api/app/utils.py +++ b/api/app/utils.py @@ -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""" @@ -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(), } @@ -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 diff --git a/api/tests/unit/app/test_unit_app_utils.py b/api/tests/unit/app/test_unit_app_utils.py index bc4a68334045..ef58d2baa96f 100644 --- a/api/tests/unit/app/test_unit_app_utils.py +++ b/api/tests/unit/app/test_unit_app_utils.py @@ -1,4 +1,6 @@ +import json import pathlib +from unittest import mock from pytest_mock import MockerFixture @@ -19,8 +21,11 @@ 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() @@ -28,6 +33,33 @@ def path_side_effect(file_path: str) -> mocker.MagicMock: # 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, } diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 096b68ba25b9..40164d628b50 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -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 diff --git a/frontend/Dockerfile.e2e b/frontend/Dockerfile.e2e index 745203493f2d..fe6e9f4aadbf 100644 --- a/frontend/Dockerfile.e2e +++ b/frontend/Dockerfile.e2e @@ -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 diff --git a/frontend/api/index.js b/frontend/api/index.js index 8e012c07fc60..69c686f700d8 100755 --- a/frontend/api/index.js +++ b/frontend/api/index.js @@ -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()) diff --git a/frontend/docker-compose-e2e-tests.yml b/frontend/docker-compose-e2e-tests.yml index fb89d735246d..32d3fa4734d3 100644 --- a/frontend/docker-compose-e2e-tests.yml +++ b/frontend/docker-compose-e2e-tests.yml @@ -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