Добавил автосборку и деплой

This commit is contained in:
Sergey Karmanov 2023-08-24 22:19:25 +03:00
parent 90a3478c85
commit dcdc1cf2ec
Signed by: serega404
GPG Key ID: B6AD49C8C835460C
3 changed files with 135 additions and 8 deletions

View File

@ -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:
# <https://stackoverflow.com/a/70447517>
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

View File

@ -1,24 +1,66 @@
using FichaBackend;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args); 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(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); 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<DatabaseContext>(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(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) // if (app.Environment.IsDevelopment())
{ // {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} // }
app.UseHttpsRedirection(); app.MapHealthChecks("/health");
app.UseAuthorization();
app.MapControllers(); app.MapControllers();

View File

@ -1,2 +1,28 @@
# FichaBackend # 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'
```