pushing to docker hub, testing in container build

This commit is contained in:
andy 2022-02-13 17:12:00 +00:00
parent a2db15831c
commit 090b12eebd
10 changed files with 136 additions and 17 deletions

View File

@ -38,14 +38,24 @@ jobs:
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1 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 - name: Build CLI Container
uses: docker/build-push-action@v2 uses: docker/build-push-action@v2
with: with:
push: true
tags: sarsoo/selector-cli:latest
file: Dockerfile.CLI file: Dockerfile.CLI
- name: Build Web Container - name: Build Web Container
uses: docker/build-push-action@v2 uses: docker/build-push-action@v2
with: with:
push: true
tags: sarsoo/selector-web:latest
file: Dockerfile.Web file: Dockerfile.Web
build-Js: build-Js:

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
appsettings.Development.json appsettings.Development.json
appsettings.Production.json
# User-specific files # User-specific files
*.rsuser *.rsuser

View File

@ -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 *.sln .
COPY Selector/*.csproj ./Selector/ COPY Selector/*.csproj ./Selector/
@ -6,14 +6,21 @@ COPY Selector.Cache/*.csproj ./Selector.Cache/
COPY Selector.Event/*.csproj ./Selector.Event/ COPY Selector.Event/*.csproj ./Selector.Event/
COPY Selector.Model/*.csproj ./Selector.Model/ COPY Selector.Model/*.csproj ./Selector.Model/
COPY Selector.CLI/*.csproj ./Selector.CLI/ COPY Selector.CLI/*.csproj ./Selector.CLI/
COPY Selector.Tests/*.csproj ./Selector.Tests/
RUN dotnet restore ./Selector.CLI/Selector.CLI.csproj RUN dotnet restore ./Selector.CLI/Selector.CLI.csproj
COPY . ./ 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 RUN dotnet publish Selector.CLI/Selector.CLI.csproj -c Release -o /app --no-restore
FROM mcr.microsoft.com/dotnet/runtime:6.0 FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app WORKDIR /app
COPY --from=build /app ./ COPY --from=publish /app ./
ENV DOTNET_EnableDiagnostics=0 ENV DOTNET_EnableDiagnostics=0
ENTRYPOINT ["dotnet", "Selector.CLI.dll"] ENTRYPOINT ["dotnet", "Selector.CLI.dll"]

View File

@ -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 *.sln .
COPY Selector/*.csproj ./Selector/ COPY Selector/*.csproj ./Selector/
@ -6,14 +6,21 @@ COPY Selector.Cache/*.csproj ./Selector.Cache/
COPY Selector.Event/*.csproj ./Selector.Event/ COPY Selector.Event/*.csproj ./Selector.Event/
COPY Selector.Model/*.csproj ./Selector.Model/ COPY Selector.Model/*.csproj ./Selector.Model/
COPY Selector.Web/*.csproj ./Selector.Web/ COPY Selector.Web/*.csproj ./Selector.Web/
COPY Selector.Tests/*.csproj ./Selector.Tests/
RUN dotnet restore ./Selector.Web/Selector.Web.csproj RUN dotnet restore ./Selector.Web/Selector.Web.csproj
COPY . ./ 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 RUN dotnet publish Selector.Web/Selector.Web.csproj -c Release -o /app --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0 FROM mcr.microsoft.com/dotnet/aspnet:6.0
EXPOSE 80 EXPOSE 80
WORKDIR /app WORKDIR /app
COPY --from=build /app ./ COPY --from=publish /app ./
ENV DOTNET_EnableDiagnostics=0 ENV DOTNET_EnableDiagnostics=0
ENTRYPOINT ["dotnet", "Selector.Web.dll"] ENTRYPOINT ["dotnet", "Selector.Web.dll"]

View File

@ -78,13 +78,6 @@ namespace Selector.CLI
AudioFeatures, AudioFeaturesCache, CacheWriter, Publisher, PlayCounter 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 class RedisOptions
{ {
public const string Key = "Redis"; public const string Key = "Redis";

View File

@ -12,6 +12,7 @@ using Selector.Model;
using Selector.Cache; using Selector.Cache;
using Selector.Cache.Extensions; using Selector.Cache.Extensions;
using Selector.Events; using Selector.Events;
using Selector.Model.Services;
namespace Selector.CLI namespace Selector.CLI
{ {
@ -74,6 +75,8 @@ namespace Selector.CLI
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(config.DatabaseOptions.ConnectionString) options.UseNpgsql(config.DatabaseOptions.ConnectionString)
); );
services.AddHostedService<MigratorService>();
} }
} }

View File

@ -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;
}
}

View File

@ -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<MigratorService> logger;
public MigratorService(ApplicationDbContext _context, IOptions<DatabaseOptions> _options, ILogger<MigratorService> _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;
}
}
}

36
docker-compose.build.yml Normal file
View File

@ -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"

View File

@ -1,15 +1,27 @@
version: "3.9" version: "3.9"
services: services:
web: web:
build: image: sarsoo/selector-web:latest
context: .
dockerfile: Dockerfile.Web
ports: ports:
- "8080:80" - "8080:80"
depends_on:
- database
- redis
volumes:
- "./Selector.Web/appsettings.Production.json:/appsettings.Production.json"
environment:
DOTNET_ENVIRONMENT: Production
cli: cli:
build: image: sarsoo/selector-cli:latest
context: . depends_on:
dockerfile: Dockerfile.CLI - database
- redis
volumes:
- "./Selector.CLI/appsettings.Production.json:/appsettings.Production.json"
environment:
DOTNET_ENVIRONMENT: Production
redis: redis:
image: redis:alpine image: redis:alpine
ports: ports: