From d34e196830be11755a66048056affde8cb98b8e9 Mon Sep 17 00:00:00 2001 From: maxime Date: Mon, 19 Feb 2024 22:02:34 +0100 Subject: [PATCH] fix CI --- API/Program.cs | 9 +++++ API/appsettings.json | 2 +- AppContext/AppContext.cs | 1 - ci/.drone.yml | 66 ++++++++++++++++++------------------- ci/DB-init.dockerfile | 3 +- ci/db-init.sh | 15 +++++++-- ci/deploy_staging_server.sh | 16 ++++++--- 7 files changed, 67 insertions(+), 45 deletions(-) diff --git a/API/Program.cs b/API/Program.cs index 94e8f1a..2a4f6f2 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -54,6 +54,15 @@ builder.Services.AddScoped(); var app = builder.Build(); +System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + +app.Use((context, next) => +{ + var req = context.Request; + Console.WriteLine($"{req.Method}: {req.Path}{req.QueryString}"); + return next.Invoke(); +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/API/appsettings.json b/API/appsettings.json index 7212941..d649d1b 100644 --- a/API/appsettings.json +++ b/API/appsettings.json @@ -6,7 +6,7 @@ "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning", - "Microsoft.AspNetCore.Authentication": "Trace" + "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Trace" } }, "AllowedHosts": "*" diff --git a/AppContext/AppContext.cs b/AppContext/AppContext.cs index d789825..5bafd19 100644 --- a/AppContext/AppContext.cs +++ b/AppContext/AppContext.cs @@ -33,7 +33,6 @@ public class AppContext : DbContext } var pgsqliteDsn = Environment.GetEnvironmentVariable("PGSQL_DSN"); - Console.WriteLine(pgsqliteDsn); if (pgsqliteDsn != null) { diff --git a/ci/.drone.yml b/ci/.drone.yml index dd1f311..fc841ea 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -19,7 +19,7 @@ steps: registry: hub.codefirst.iut.uca.fr repo: hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet tags: - - ${DRONE_BRANCH} + - ${DRONE_BRANCH/\//_} username: from_secret: SECRET_REGISTRY_USERNAME password: @@ -39,36 +39,36 @@ steps: - chmod 0600 ~/.ssh - chmod 0500 ~/.ssh/id_rsa* - rsync -avz -e "ssh -p 80 -o 'StrictHostKeyChecking=no'" ci/deploy_staging_server.sh iqball@maxou.dev:/srv/www/iqball/$DRONE_BRANCH - - ssh -p 80 -o 'StrictHostKeyChecking=no' iqball@maxou.dev "chmod +x /srv/www/iqball/$DRONE_BRANCH/deploy_staging_server.sh && /srv/www/iqball/$DRONE_BRANCH/deploy_staging_server.sh $DRONE_BRANCH" + - ssh -p 80 -o 'StrictHostKeyChecking=no' iqball@maxou.dev "chmod +x /srv/www/iqball/$DRONE_BRANCH/deploy_staging_server.sh && /srv/www/iqball/$DRONE_BRANCH/deploy_staging_server.sh $(echo $DRONE_BRANCH | tr / _) $DRONE_COMMIT_SHA" - # Deploy the production database and server on codefirst - - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest - name: "Instantiate dotnet api docker image on codefirst" - depends_on: - - "build and push docker image" - environment: - IMAGENAME: hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:master - CONTAINERNAME: iqball_production_dotnet_webserver - OVERWRITE: true - COMMAND: create - - CODEFIRST_CLIENTDRONE_ENV_PGSQL_DSN: - from_secret: POSTGRES_DSN - ADMINS: maximebatista - - - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest - name: "Instantiate pgsql database docker image on codefirst" - depends_on: - - "build and push docker image" - environment: - IMAGENAME: postgres - CONTAINERNAME: iqball_production_database - COMMAND: create - - CODEFIRST_CLIENTDRONE_ENV_POSTGRES_PASSWORD: - from_secret: POSTGRES_PASSWORD - CODEFIRST_CLIENTDRONE_ENV_POSTGRES_USER: - from_secret: POSTGRES_USER - CODEFIRST_CLIENTDRONE_ENV_POSTGRES_DB: - from_secret: POSTGRES_DB - ADMINS: maximebatista +# Deploy the production database and server on codefirst +# - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest +# name: "Instantiate dotnet api docker image on codefirst" +# depends_on: +# - "build and push docker image" +# environment: +# IMAGENAME: hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:master +# CONTAINERNAME: iqball_production_dotnet_webserver +# OVERWRITE: true +# COMMAND: create +# +# CODEFIRST_CLIENTDRONE_ENV_PGSQL_DSN: +# from_secret: POSTGRES_DSN +# ADMINS: maximebatista +# +# - image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest +# name: "Instantiate pgsql database docker image on codefirst" +# depends_on: +# - "build and push docker image" +# environment: +# IMAGENAME: postgres +# CONTAINERNAME: iqball_production_database +# COMMAND: create +# +# CODEFIRST_CLIENTDRONE_ENV_POSTGRES_PASSWORD: +# from_secret: POSTGRES_PASSWORD +# CODEFIRST_CLIENTDRONE_ENV_POSTGRES_USER: +# from_secret: POSTGRES_USER +# CODEFIRST_CLIENTDRONE_ENV_POSTGRES_DB: +# from_secret: POSTGRES_DB +# ADMINS: maximebatista diff --git a/ci/DB-init.dockerfile b/ci/DB-init.dockerfile index ba8c648..be9b25c 100644 --- a/ci/DB-init.dockerfile +++ b/ci/DB-init.dockerfile @@ -10,6 +10,5 @@ ENV PATH="$PATH:/root/.dotnet/tools" ADD --chmod=755 db-init.sh ./ - -ENTRYPOINT ./db-init.sh $BRANCH +ENTRYPOINT ./db-init.sh $BRANCH $COMMIT_SHA diff --git a/ci/db-init.sh b/ci/db-init.sh index 0f7ef2c..b53aef4 100644 --- a/ci/db-init.sh +++ b/ci/db-init.sh @@ -1,13 +1,22 @@ set -xeu +BRANCH=$1 +COMMIT_SHA=$2 + git clone https://codefirst.iut.uca.fr/git/IQBall/Dotnet-WebAPI cd Dotnet-WebAPI -git switch $1 +git switch $BRANCH +#TODO remove migrations that are bound to commits that are no longer in the branch (which could occur via a rebase) -(cd AppContext && rm -rf Migrations && dotnet ef migrations add iqball-postgres-db-mig) -(cd API && dotnet ef database update --project ../AppContext) +cd AppContext +rm -rf Migrations # remove migrations from git +ln -s /migrations Migrations # bind auto branch migrations to the project +# apply migrations +dotnet ef migrations add "auto-migration-$COMMIT_SHA" +# update database +dotnet ef database update diff --git a/ci/deploy_staging_server.sh b/ci/deploy_staging_server.sh index 2197c6f..d510b9b 100644 --- a/ci/deploy_staging_server.sh +++ b/ci/deploy_staging_server.sh @@ -2,10 +2,14 @@ set -exu -API_CONTAINER_NAME="iqball-api-dotnet-$1" -DB_CONTAINER_NAME="iqball-db-$1" +BRANCH=$1 +COMMIT_SHA=$2 + +API_CONTAINER_NAME="iqball-api-dotnet-$BRANCH" +DB_CONTAINER_NAME="iqball-db-$BRANCH" (docker stop "$API_CONTAINER_NAME" && docker rm "$API_CONTAINER_NAME") || true +docker volume create "iqball-migrations-$BRANCH" || true docker run -d \ --name "$DB_CONTAINER_NAME" \ @@ -19,12 +23,14 @@ docker run -d \ # apply migrations on database docker run --rm -t \ --env PGSQL_DSN="Server=$DB_CONTAINER_NAME;Username=iqball;Password=1234;Database=iqball" \ - --env BRANCH="$1" \ + --env BRANCH="$BRANCH" \ + --env COMMIT_SHA="$COMMIT_SHA" \ + --mount source="iqball-migrations-$BRANCH",target=/migrations \ --network iqball_net \ iqball-db-init:latest -docker pull "hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:$1" +docker pull "hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:$BRANCH" # run the API docker run -d \ @@ -32,7 +38,7 @@ docker run -d \ --restart=always \ --network iqball_net \ --env PGSQL_DSN="Server=$DB_CONTAINER_NAME;Username=iqball;Password=1234;Database=iqball" \ - "hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:$1" + "hub.codefirst.iut.uca.fr/maxime.batista/iqball-api-dotnet:$BRANCH"