From 090b12eebdbf6c38d7078288b624d8077d2e3147 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 13 Feb 2022 17:12:00 +0000 Subject: [PATCH] pushing to docker hub, testing in container build --- .github/workflows/ci.yml | 10 ++++++ .gitignore | 1 + Dockerfile.CLI | 11 ++++-- Dockerfile.Web | 11 ++++-- Selector.CLI/Options.cs | 7 ---- Selector.CLI/Program.cs | 3 ++ Selector.Model/DatabaseOptions.cs | 11 ++++++ Selector.Model/Services/MigratorService.cs | 39 ++++++++++++++++++++++ docker-compose.build.yml | 36 ++++++++++++++++++++ docker-compose.yml | 24 +++++++++---- 10 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 Selector.Model/DatabaseOptions.cs create mode 100644 Selector.Model/Services/MigratorService.cs create mode 100644 docker-compose.build.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a826a0..3412b06 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,14 +38,24 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build CLI Container uses: docker/build-push-action@v2 with: + push: true + tags: sarsoo/selector-cli:latest file: Dockerfile.CLI - name: Build Web Container uses: docker/build-push-action@v2 with: + push: true + tags: sarsoo/selector-web:latest file: Dockerfile.Web build-Js: diff --git a/.gitignore b/.gitignore index dc4576f..fe99222 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore appsettings.Development.json +appsettings.Production.json # User-specific files *.rsuser diff --git a/Dockerfile.CLI b/Dockerfile.CLI index 461c106..089cf79 100644 --- a/Dockerfile.CLI +++ b/Dockerfile.CLI @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base COPY *.sln . COPY Selector/*.csproj ./Selector/ @@ -6,14 +6,21 @@ COPY Selector.Cache/*.csproj ./Selector.Cache/ COPY Selector.Event/*.csproj ./Selector.Event/ COPY Selector.Model/*.csproj ./Selector.Model/ COPY Selector.CLI/*.csproj ./Selector.CLI/ +COPY Selector.Tests/*.csproj ./Selector.Tests/ RUN dotnet restore ./Selector.CLI/Selector.CLI.csproj COPY . ./ + +FROM base as test +RUN dotnet restore ./Selector.Tests/Selector.Tests.csproj +RUN dotnet test --no-restore --verbosity normal + +FROM base as publish RUN dotnet publish Selector.CLI/Selector.CLI.csproj -c Release -o /app --no-restore FROM mcr.microsoft.com/dotnet/runtime:6.0 WORKDIR /app -COPY --from=build /app ./ +COPY --from=publish /app ./ ENV DOTNET_EnableDiagnostics=0 ENTRYPOINT ["dotnet", "Selector.CLI.dll"] diff --git a/Dockerfile.Web b/Dockerfile.Web index f3ce30f..4ff94aa 100644 --- a/Dockerfile.Web +++ b/Dockerfile.Web @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base COPY *.sln . COPY Selector/*.csproj ./Selector/ @@ -6,14 +6,21 @@ COPY Selector.Cache/*.csproj ./Selector.Cache/ COPY Selector.Event/*.csproj ./Selector.Event/ COPY Selector.Model/*.csproj ./Selector.Model/ COPY Selector.Web/*.csproj ./Selector.Web/ +COPY Selector.Tests/*.csproj ./Selector.Tests/ RUN dotnet restore ./Selector.Web/Selector.Web.csproj COPY . ./ + +FROM base as test +RUN dotnet restore ./Selector.Tests/Selector.Tests.csproj +RUN dotnet test --no-restore --verbosity normal + +FROM base as publish RUN dotnet publish Selector.Web/Selector.Web.csproj -c Release -o /app --no-restore FROM mcr.microsoft.com/dotnet/aspnet:6.0 EXPOSE 80 WORKDIR /app -COPY --from=build /app ./ +COPY --from=publish /app ./ ENV DOTNET_EnableDiagnostics=0 ENTRYPOINT ["dotnet", "Selector.Web.dll"] diff --git a/Selector.CLI/Options.cs b/Selector.CLI/Options.cs index 74b5f96..5736d40 100644 --- a/Selector.CLI/Options.cs +++ b/Selector.CLI/Options.cs @@ -78,13 +78,6 @@ namespace Selector.CLI AudioFeatures, AudioFeaturesCache, CacheWriter, Publisher, PlayCounter } - public class DatabaseOptions { - public const string Key = "Database"; - - public bool Enabled { get; set; } = false; - public string ConnectionString { get; set; } - } - public class RedisOptions { public const string Key = "Redis"; diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index 1d9dfe3..3b0e8ad 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -12,6 +12,7 @@ using Selector.Model; using Selector.Cache; using Selector.Cache.Extensions; using Selector.Events; +using Selector.Model.Services; namespace Selector.CLI { @@ -74,6 +75,8 @@ namespace Selector.CLI services.AddDbContext(options => options.UseNpgsql(config.DatabaseOptions.ConnectionString) ); + + services.AddHostedService(); } } diff --git a/Selector.Model/DatabaseOptions.cs b/Selector.Model/DatabaseOptions.cs new file mode 100644 index 0000000..c4a66c0 --- /dev/null +++ b/Selector.Model/DatabaseOptions.cs @@ -0,0 +1,11 @@ +namespace Selector +{ + public class DatabaseOptions + { + public const string Key = "Database"; + + public bool Enabled { get; set; } = false; + public string ConnectionString { get; set; } + public bool Migrate { get; set; } = false; + } +} diff --git a/Selector.Model/Services/MigratorService.cs b/Selector.Model/Services/MigratorService.cs new file mode 100644 index 0000000..4e15bea --- /dev/null +++ b/Selector.Model/Services/MigratorService.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.Threading; +using System.Threading.Tasks; + +namespace Selector.Model.Services +{ + public class MigratorService : IHostedService + { + private readonly ApplicationDbContext context; + private readonly DatabaseOptions options; + private readonly ILogger logger; + + public MigratorService(ApplicationDbContext _context, IOptions _options, ILogger _logger) + { + context = _context; + options = _options.Value; + logger = _logger; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + if(options.Migrate) + { + logger.LogInformation("Applying migrations"); + context.Database.Migrate(); + } + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} diff --git a/docker-compose.build.yml b/docker-compose.build.yml new file mode 100644 index 0000000..4c1e802 --- /dev/null +++ b/docker-compose.build.yml @@ -0,0 +1,36 @@ +version: "3.9" +services: + web: + build: + context: . + dockerfile: Dockerfile.Web + ports: + - "8080:80" + depends_on: + - database + - redis + volumes: + - "./Selector.Web/appsettings.Production.json:/appsettings.Production.json" + environment: + DOTNET_ENVIRONMENT: Production + + cli: + build: + context: . + dockerfile: Dockerfile.CLI + depends_on: + - database + - redis + volumes: + - "./Selector.CLI/appsettings.Production.json:/appsettings.Production.json" + environment: + DOTNET_ENVIRONMENT: Production + + redis: + image: redis:alpine + ports: + - "6379:6379" + database: + image: postgres + ports: + - "5432:5432" diff --git a/docker-compose.yml b/docker-compose.yml index fa544da..bfcdb86 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,27 @@ version: "3.9" services: web: - build: - context: . - dockerfile: Dockerfile.Web + image: sarsoo/selector-web:latest ports: - "8080:80" + depends_on: + - database + - redis + volumes: + - "./Selector.Web/appsettings.Production.json:/appsettings.Production.json" + environment: + DOTNET_ENVIRONMENT: Production + cli: - build: - context: . - dockerfile: Dockerfile.CLI + image: sarsoo/selector-cli:latest + depends_on: + - database + - redis + volumes: + - "./Selector.CLI/appsettings.Production.json:/appsettings.Production.json" + environment: + DOTNET_ENVIRONMENT: Production + redis: image: redis:alpine ports: