Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres #140

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/build-and-push-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This workflow will build a Docker image and push it to GitHub Container Registry

name: Publish Docker image prod

on:
workflow_dispatch:

jobs:
push_to_registries:
name: Push Docker image to ghcr.io
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/[email protected]
with:
install: true
driver-opts: network=host
platforms: linux/amd64,linux/arm64

- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest
type=raw,value=prod

- name: Build and push Docker images
id: push
uses: docker/[email protected]
with:
context: .
file: Dockerfile.prod
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
29 changes: 29 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Stage 1: Build
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG TARGETARCH
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production

# Copy only the required project files
COPY GirafAPI/*.csproj ./GirafAPI/
COPY GirafAPI/Data/Migrations/*.cs ./GirafAPI/Data/Migrations/
RUN dotnet restore ./GirafAPI/GirafAPI.csproj

# Build and publish the application
COPY . .
RUN dotnet build ./GirafAPI/GirafAPI.csproj -c Release -o /app/build -a $TARGETARCH
RUN dotnet publish ./GirafAPI/GirafAPI.csproj -c Release -o /app/publish -a $TARGETARCH --no-restore

# Stage 2: Runtime
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app

# Copy the published app from the build stage
COPY --from=build /app/publish .

# Expose the application port
EXPOSE 5171
ENV ASPNETCORE_URLS=http://+:5171

# Set the entry point
ENTRYPOINT ["dotnet", "/app/GirafAPI.dll"]
38 changes: 31 additions & 7 deletions GirafAPI/Data/GirafDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,37 @@

namespace GirafAPI.Data
{
public class GirafDbContext(DbContextOptions<GirafDbContext> options) : IdentityDbContext<GirafUser>(options)
public class GirafDbContext : IdentityDbContext<GirafUser>
{
public DbSet<Citizen> Citizens => Set<Citizen>();
public DbSet<Activity> Activities => Set<Activity>();
public DbSet<Organization> Organizations => Set<Organization>();
public DbSet<Invitation> Invitations => Set<Invitation>();
public GirafDbContext(DbContextOptions<GirafDbContext> options) : base(options)
{
}

public DbSet<Citizen> Citizens { get; set; }
public DbSet<Activity> Activities { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<Invitation> Invitations { get; set; }
public DbSet<Grade> Grades { get; set; }
public DbSet<Pictogram> Pictograms => Set<Pictogram>();
public DbSet<Pictogram> Pictograms { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Citizen>()
.HasMany(c => c.Activities)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Grade>()
.HasMany(g => g.Activities)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Pictogram>()
.HasMany<Activity>()
.WithOne(a => a.Pictogram)
.OnDelete(DeleteBehavior.SetNull);
}
}
}
}
Loading
Loading