From 56a588b803efc1023f9825b43bc97f26c6474eb2 Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Thu, 24 Aug 2023 22:19:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D1=83=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=B5=D0=BF=D0=BB=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/gitea-push-docker.yml | 59 ++++++++++++++++++++++++++ FichaBackend/Program.cs | 58 +++++++++++++++++++++---- README.md | 26 ++++++++++++ 3 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 .gitea/workflows/gitea-push-docker.yml diff --git a/.gitea/workflows/gitea-push-docker.yml b/.gitea/workflows/gitea-push-docker.yml new file mode 100644 index 0000000..1a6ac3c --- /dev/null +++ b/.gitea/workflows/gitea-push-docker.yml @@ -0,0 +1,59 @@ +name: Create and publish a Docker image + +on: + push: + branches: ['main'] + +env: + REGISTRY: git.zetcraft.ru + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + name: Publish image + container: catthehacker/ubuntu:act-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ gitea.actor }} + password: ${{ secrets.TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: https://github.com/docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY }}/${{ gitea.repository }} + + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: FichaBackend + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + deploy: + needs: publish + name: Deploy image + runs-on: ubuntu-latest + + steps: + - name: install ssh keys + # check this thread to understand why its needed: + # + run: | + install -m 600 -D /dev/null ~/.ssh/id_rsa + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts + - name: connect and pull + run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && docker compose pull && docker compose up -d && docker image prune && exit" + - name: cleanup + run: rm -rf ~/.ssh diff --git a/FichaBackend/Program.cs b/FichaBackend/Program.cs index 8264bac..2df9870 100644 --- a/FichaBackend/Program.cs +++ b/FichaBackend/Program.cs @@ -1,24 +1,66 @@ +using FichaBackend; +using Microsoft.EntityFrameworkCore; +using Microsoft.OpenApi.Models; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +string GetEnv(string envName, string settingsName = "") +{ + string? dbConString = builder.Configuration.GetConnectionString(settingsName) ?? Environment.GetEnvironmentVariable(envName); + if (!string.IsNullOrEmpty(dbConString)) + return dbConString; + + Console.WriteLine($"Environment variable {envName} not found."); + return String.Empty; +} builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c => +{ + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "ApiDocumentation.xml")); + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Sistema Backend", Version = "v1" }); +}); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +// builder.Services.AddEndpointsApiExplorer(); +// builder.Services.AddSwaggerGen(); + +// Database +string? dbConString = GetEnv("CONNECTION_STRING", "DefaultConnection"); + +builder.Services.AddDbContext(options => + { options.UseNpgsql(dbConString); }); + +// HealthChecks +builder.Services.AddHealthChecks() + .AddNpgSql(dbConString); + +// Services +// cors +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy( + policy => + { + policy.WithOrigins("*") + .AllowAnyHeader() + .AllowAnyMethod(); + }); +}); var app = builder.Build(); // Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ +// if (app.Environment.IsDevelopment()) +// { app.UseSwagger(); app.UseSwaggerUI(); -} +// } -app.UseHttpsRedirection(); - -app.UseAuthorization(); +app.MapHealthChecks("/health"); app.MapControllers(); diff --git a/README.md b/README.md index a765629..f177523 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # FichaBackend +# Docker + +```bash +docker run -d -p 82:80 \ +--name FichaBackend \ +--restart=always \ +-e CONNECTION_STRING='Host=192.168.0.46;Port=5432;Database=backend;Username=prod;Password=' \ +-e TZ=Europe/Moscow \ +git.zetcraft.ru/fichahackaton/fichabackend:main +``` + +# Docker Compose + +```yml +version: '3.3' +services: + fichahackaton: + ports: + - '82:80' + container_name: FichaBackend + restart: always + environment: + - CONNECTION_STRING=Host=192.168.0.46;Port=5432;Database=backend;Username=prod;Password= + - TZ=Europe/Moscow + image: 'git.zetcraft.ru/fichahackaton/fichabackend:main' +``` \ No newline at end of file