Skip to content

Commit

Permalink
feat: Convert to Azure Container App
Browse files Browse the repository at this point in the history
Merge pull request #24 from DFE-Digital/convert-to-container-app
  • Loading branch information
killij authored Jan 17, 2024
2 parents 0f16ec9 + 901520b commit cc37785
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 264 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Docker Publish
on:
release:
types: [published]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
jobs:
build:
name: 'Docker Publish'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge with sigstore/fulcio when running outside of PRs.
id-token: write
steps:
# Checkout the release tag version
- name: Checkout repository ${{ env.RELEASE_TAG }}
uses: actions/checkout@v3
with:
ref: ${{ env.RELEASE_TAG }}
# Get git commit hash
- name: Get short hash
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
# Need to lower case the image name for the docker tags when publishing
- name: Downcase IMAGE_NAME variable
run: echo "IMAGE_NAME_LOWER=${IMAGE_NAME,,}" >> $GITHUB_ENV

# Sort out the image tags
- name: Set initial tag
run: echo "IMAGE_TAGS=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:${{ env.RELEASE_TAG }}" >> $GITHUB_ENV
- name: Add latest tag if we're not production release
if: contains(env.RELEASE_TAG, 'next')
run: echo "IMAGE_TAGS=${{ env.IMAGE_TAGS }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}:latest" >> $GITHUB_ENV
#debug
- name: Log the tags
run: echo "Calculated tags value => ${{ env.IMAGE_TAGS }}"
# Setup docker build tool
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
# Login against a Docker registry
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: ./src
file: ./src/Childrens-Social-Care-CPD-Indexer/Dockerfile
push: true
tags: ${{ env.IMAGE_TAGS }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VCSREF=${{ env.sha_short }}
VCSTAG=${{ env.RELEASE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Sign the resulting Docker image digest.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Install Cosign
uses: sigstore/[email protected]
- name: Check install!
run: cosign version
- name: Sign the published Docker image
# This step uses the identity token to provision an ephemeral certificate against the sigstore community Fulcio instance.
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest }}
43 changes: 0 additions & 43 deletions .github/workflows/dotnet-publish.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/function-app-deploy.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
using Childrens_Social_Care_CPD_Indexer.Core;
using Microsoft.ApplicationInsights;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NSubstitute.ExceptionExtensions;

namespace Childrens_Social_Care_CPD_Indexer.Tests;

public class IndexingServiceTests
public class WorkerTests
{
private ILogger<Indexer> _logger;
private ILogger<Worker> _logger;
private IResourcesIndexerConfig _config;
private IResourcesIndexer _indexer;
private Indexer _sut;
private IHostApplicationLifetime _hostingApplicationLifetime;
private Worker _sut;

[SetUp]
public void Setup()
{
_logger = Substitute.For<ILogger<Indexer>>();
_logger = Substitute.For<ILogger<Worker>>();
_config = Substitute.For<IResourcesIndexerConfig>();
_indexer = Substitute.For<IResourcesIndexer>();
_sut = new Indexer(_logger, _indexer, _config);
_hostingApplicationLifetime = Substitute.For<IHostApplicationLifetime>();

_sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime);
}

[TearDown]
public void Teardown()
{
_sut.Dispose();
}

[Test]
public async Task StartAsync_Deletes_Index_If_Configured()
{
// arrange
_config.RecreateIndex.Returns(true);
var cancellationTokenSource = new CancellationTokenSource();

// act
await _sut.Run(new TimerInfo());
await _sut.StartAsync(cancellationTokenSource.Token);

// assert
await _indexer.Received(1).DeleteIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
Expand All @@ -41,9 +50,10 @@ public async Task StartAsync_Populates_Index()
{
// arrange
_config.RecreateIndex.Returns(false);
var cancellationTokenSource = new CancellationTokenSource();

// act
await _sut.Run(new TimerInfo());
await _sut.StartAsync(cancellationTokenSource.Token);

// assert
await _indexer.Received(1).PopulateIndexAsync(Arg.Any<string>(), Arg.Any<int>(), Arg.Any<CancellationToken>());
Expand All @@ -56,9 +66,10 @@ public async Task StartAsync_Logs_Exception()
var exception = new InvalidOperationException();
_config.RecreateIndex.Returns(true);
_indexer.DeleteIndexAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Throws(exception);
var cancellationTokenSource = new CancellationTokenSource();

// act
await _sut.Run(new TimerInfo());
await _sut.StartAsync(cancellationTokenSource.Token);

// assert
_logger.Received(1).LogError(exception, "Unhandled exception occured");
Expand Down
24 changes: 12 additions & 12 deletions src/Childrens-Social-Care-CPD-Indexer.sln
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{6984BF56-808E-4294-949D-FFE4B02CCE16}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{F2710DEC-7B59-484A-9D04-F73B1E09563A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Childrens-Social-Care-CPD-Indexer", "Childrens-Social-Care-CPD-Indexer\Childrens-Social-Care-CPD-Indexer.csproj", "{3555944D-1913-4979-B5BF-991C7064613E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Childrens-Social-Care-CPD-Indexer.Tests", "Childrens-Social-Care-CPD-Indexer.Tests\Childrens-Social-Care-CPD-Indexer.Tests.csproj", "{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6984BF56-808E-4294-949D-FFE4B02CCE16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6984BF56-808E-4294-949D-FFE4B02CCE16}.Release|Any CPU.Build.0 = Release|Any CPU
{3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3555944D-1913-4979-B5BF-991C7064613E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3555944D-1913-4979-B5BF-991C7064613E}.Release|Any CPU.Build.0 = Release|Any CPU
{F2710DEC-7B59-484A-9D04-F73B1E09563A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2710DEC-7B59-484A-9D04-F73B1E09563A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2710DEC-7B59-484A-9D04-F73B1E09563A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2710DEC-7B59-484A-9D04-F73B1E09563A}.Release|Any CPU.Build.0 = Release|Any CPU
{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0344BA1-9B92-42A7-B650-B8064EDD2B9E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEC0CA75-7434-4AC0-9F45-31CA6CC7D59A}
SolutionGuid = {5F3E3B0D-C6EA-4304-851B-75680DAB332E}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-Childrens_Social_Care_CPD_Indexer-907bada8-7f54-479d-b04e-45ec4f148332</UserSecretsId>
<RootNamespace>Childrens_Social_Care_CPD_Indexer</RootNamespace>
<DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Childrens-Social-Care-CPD-Indexer.Tests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Childrens-Social-Care-CPD-Indexer.Tests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Search.Documents" Version="11.5.1" />
<PackageReference Include="contentful.csharp" Version="7.5.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
</Project>
11 changes: 5 additions & 6 deletions src/Childrens-Social-Care-CPD-Indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
WORKDIR /home/site/wwwroot
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER app
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
Expand All @@ -18,7 +18,6 @@ ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Childrens-Social-Care-CPD-Indexer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /home/site/wwwroot
WORKDIR /app
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
ENTRYPOINT ["dotnet", "Childrens-Social-Care-CPD-Indexer.dll"]
Loading

0 comments on commit cc37785

Please sign in to comment.