From 9274b94c0f0aaf6e2beb566b927e59e4c9c3362e Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:47:24 +0100 Subject: [PATCH 1/2] ci: add release tag into docker container as env variable --- .github/workflows/docker-publish.yml | 9 +++++---- Childrens-Social-Care-CPD/Dockerfile | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index a0864226..c3ee3e45 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -14,6 +14,7 @@ env: REGISTRY: ghcr.io # github.repository as / IMAGE_NAME: ${{ github.repository }} + RELEASE_TAG: ${{ github.event.release.tag_name }} jobs: build: @@ -29,10 +30,10 @@ jobs: steps: # Checkout the release tag version - - name: Checkout repository ${{ github.event.release.tag_name }} + - name: Checkout repository ${{ env.RELEASE_TAG }} uses: actions/checkout@v3 with: - ref: ${{ github.event.release.tag_name }} + ref: ${{ env.RELEASE_TAG }} # Get git commit hash - name: Get short hash @@ -80,9 +81,9 @@ jobs: with: context: Childrens-Social-Care-CPD push: ${{ github.event_name != 'pull_request' }} - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:${{ github.event.release.tag_name }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:latest + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:${{ env.RELEASE_TAG }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:latest labels: ${{ steps.meta.outputs.labels }} - build-args: VCSREF=${{ env.sha_short }} + build-args: VCSREF=${{ env.sha_short }},VCSTAG=${{ env.RELEASE_TAG }} cache-from: type=gha cache-to: type=gha,mode=max diff --git a/Childrens-Social-Care-CPD/Dockerfile b/Childrens-Social-Care-CPD/Dockerfile index 07bd769e..44de42fa 100644 --- a/Childrens-Social-Care-CPD/Dockerfile +++ b/Childrens-Social-Care-CPD/Dockerfile @@ -21,4 +21,6 @@ WORKDIR /app COPY --from=publish /app/publish . ARG VCSREF=0 ENV VCS-REF=$VCSREF +ARG VCSTAG=0.0.0 +ENV VCS-TAG=$VCSTAG ENTRYPOINT ["dotnet", "Childrens-Social-Care-CPD.dll"] From e39c57661f83f04be314760694394df7f1730eba Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:54:22 +0100 Subject: [PATCH 2/2] Added app version env var into JSON response for AppInfo --- .../Controllers/AppInfoControllerTests.cs | 14 +++++++ .../ApplicationConfiguration.cs | 1 + .../Controllers/AppInfoController.cs | 38 +++++++++---------- .../IApplicationConfiguration.cs | 1 + .../Models/ApplicationInfo.cs | 14 +++---- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/AppInfoControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/AppInfoControllerTests.cs index 4ef442d5..1028fce7 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/AppInfoControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/AppInfoControllerTests.cs @@ -61,4 +61,18 @@ public void AppInfo_Includes_Git_Hash() // assert actual.GitShortHash.Should().Be(value); } + + [Test] + public void AppInfo_Includes_App_Version() + { + // arrange + var value = "foo"; + _applicationConfiguration.AppVersion.Returns(value); + + // act + var actual = _controller.AppInfo().Value as ApplicationInfo; + + // assert + actual.Version.Should().Be(value); + } } \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/ApplicationConfiguration.cs b/Childrens-Social-Care-CPD/ApplicationConfiguration.cs index 3cbfc0d9..bdfd62d8 100644 --- a/Childrens-Social-Care-CPD/ApplicationConfiguration.cs +++ b/Childrens-Social-Care-CPD/ApplicationConfiguration.cs @@ -4,6 +4,7 @@ public class ApplicationConfiguration : IApplicationConfiguration { private static string ValueOrStringEmpty(string key) => Environment.GetEnvironmentVariable(key) ?? string.Empty; public string AppInsightsConnectionString => ValueOrStringEmpty("CPD_INSTRUMENTATION_CONNECTIONSTRING"); + public string AppVersion => ValueOrStringEmpty("VCS-TAG"); public string AzureEnvironment => ValueOrStringEmpty("CPD_AZURE_ENVIRONMENT"); public string ClarityProjectId => ValueOrStringEmpty("CPD_CLARITY"); public string ContentfulDeliveryApiKey => ValueOrStringEmpty("CPD_DELIVERY_KEY"); diff --git a/Childrens-Social-Care-CPD/Controllers/AppInfoController.cs b/Childrens-Social-Care-CPD/Controllers/AppInfoController.cs index 015d7df2..71dd5cf6 100644 --- a/Childrens-Social-Care-CPD/Controllers/AppInfoController.cs +++ b/Childrens-Social-Care-CPD/Controllers/AppInfoController.cs @@ -1,29 +1,29 @@ using Childrens_Social_Care_CPD.Models; using Microsoft.AspNetCore.Mvc; -namespace Childrens_Social_Care_CPD.Controllers +namespace Childrens_Social_Care_CPD.Controllers; + +public class AppInfoController : Controller { - public class AppInfoController : Controller - { - private readonly IApplicationConfiguration _applicationConfiguration; + private readonly IApplicationConfiguration _applicationConfiguration; - public AppInfoController(IApplicationConfiguration applicationConfiguration) - { - _applicationConfiguration = applicationConfiguration; - } + public AppInfoController(IApplicationConfiguration applicationConfiguration) + { + _applicationConfiguration = applicationConfiguration; + } - [HttpGet] - [Route("CPD/AppInfo")] - public JsonResult AppInfo() + [HttpGet] + [Route("CPD/AppInfo")] + public JsonResult AppInfo() + { + var applicationInfo = new ApplicationInfo() { - var applicationInfo = new ApplicationInfo() - { - Environment = _applicationConfiguration.AzureEnvironment, - ContentfulEnvironment = _applicationConfiguration.ContentfulEnvironment, - GitShortHash = _applicationConfiguration.GitHash - }; + Environment = _applicationConfiguration.AzureEnvironment, + ContentfulEnvironment = _applicationConfiguration.ContentfulEnvironment, + GitShortHash = _applicationConfiguration.GitHash, + Version = _applicationConfiguration.AppVersion, + }; - return Json(applicationInfo); - } + return Json(applicationInfo); } } \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/IApplicationConfiguration.cs b/Childrens-Social-Care-CPD/IApplicationConfiguration.cs index 5a99e536..882556a0 100644 --- a/Childrens-Social-Care-CPD/IApplicationConfiguration.cs +++ b/Childrens-Social-Care-CPD/IApplicationConfiguration.cs @@ -3,6 +3,7 @@ public interface IApplicationConfiguration { string AppInsightsConnectionString { get; } + string AppVersion { get; } string AzureEnvironment { get; } string ClarityProjectId { get; } string ContentfulDeliveryApiKey { get; } diff --git a/Childrens-Social-Care-CPD/Models/ApplicationInfo.cs b/Childrens-Social-Care-CPD/Models/ApplicationInfo.cs index 86a66443..9fe36720 100644 --- a/Childrens-Social-Care-CPD/Models/ApplicationInfo.cs +++ b/Childrens-Social-Care-CPD/Models/ApplicationInfo.cs @@ -1,11 +1,9 @@ -using Newtonsoft.Json; +namespace Childrens_Social_Care_CPD.Models; -namespace Childrens_Social_Care_CPD.Models +public class ApplicationInfo { - public class ApplicationInfo - { - public string Environment { get; set; } - public string ContentfulEnvironment { get; set; } - public string GitShortHash { get; set; } - } + public string ContentfulEnvironment { get; set; } + public string Environment { get; set; } + public string GitShortHash { get; set; } + public string Version { get; set; } }