From 6dd0530bf75e09cbd684ff9f28137d4860ee3aa4 Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Tue, 7 May 2024 12:29:02 +0200 Subject: [PATCH 1/7] Add solution based validation --- actions/dotnet-validate-solution/action.yaml | 73 ++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 actions/dotnet-validate-solution/action.yaml diff --git a/actions/dotnet-validate-solution/action.yaml b/actions/dotnet-validate-solution/action.yaml new file mode 100644 index 0000000..e547ed8 --- /dev/null +++ b/actions/dotnet-validate-solution/action.yaml @@ -0,0 +1,73 @@ +name: Dotnet Validate + +description: Lints and tests a domain. + +inputs: + path: + description: The paths of the parent folder of the solution. + required: true + + dotnet-version: + description: The version of dotnet to use + required: true + + pin-version: + description: Pin dotnet version + default: "false" + required: false + +runs: + using: composite + + steps: + - name: Setup dotnet + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ inputs.dotnet-version }} + + - name: Cache NuGet packages + uses: actions/cache@v4 + with: + path: ~/.nuget/packages + key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }} + restore-keys: | + ${{ runner.os }}-nuget- + + - name: Pin version + working-directory: ${{ inputs.path }} + shell: bash + if: ${{ inputs.pin-version == 'true' }} + run: printf '{"sdk":{"rollForward":"disable","version":"%s"}}' "${{ inputs.dotnet-version }}" > global.json + + - name: Print dotnet version + working-directory: ${{ inputs.path }} + shell: bash + run: dotnet --version + + - name: Print dotnet information + working-directory: ${{ inputs.path }} + shell: bash + run: dotnet --info + + - name: Restore + working-directory: ${{ inputs.path }} + shell: bash + env: + version: "" + run: | + dotnet tool restore || true + dotnet restore + + - name: Lint + working-directory: ${{ inputs.path }} + shell: bash + env: + version: "" + run: dotnet format --verify-no-changes -v diag + + - name: Test + working-directory: ${{ inputs.path }} + shell: bash + env: + version: "" + run: dotnet test --no-restore -warnaserror --logger:"console;verbosity=normal" From 49352e168a6f1000a17511dd417517e2e5b5baef Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Tue, 7 May 2024 14:49:30 +0200 Subject: [PATCH 2/7] more solution --- Dockerfile-solution | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Dockerfile-solution diff --git a/Dockerfile-solution b/Dockerfile-solution new file mode 100644 index 0000000..a8b09d9 --- /dev/null +++ b/Dockerfile-solution @@ -0,0 +1,27 @@ +ARG SDK_VERSION +ARG RUNTIME_VERSION +FROM mcr.microsoft.com/dotnet/aspnet:${RUNTIME_VERSION}-jammy-chiseled-extra AS base + +FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build +WORKDIR /src/ +COPY . . +RUN dotnet tool restore || true +RUN dotnet restore +RUN dotnet build -c Release --no-restore +RUN dotnet publish -c Release -o /app/publish --no-restore --no-build +WORKDIR /app/publish +RUN rm -f appsettings.json appsettings.*.json || true + +FROM base AS final +ARG ASSEMBLY_NAME +WORKDIR /app +COPY --from=build /app/publish . +COPY --from=build /app/publish/${ASSEMBLY_NAME}.dll main.dll +COPY --from=build /app/publish/${ASSEMBLY_NAME}.runtimeconfig.json main.runtimeconfig.json +COPY migrations/* /migrations/ +COPY --from=busybox:uclibc /bin/cp /bin/cp +COPY --from=busybox:uclibc /bin/cat /bin/cat +COPY --from=busybox:uclibc /bin/ls /bin/ls +EXPOSE 8080 +EXPOSE 8081 +ENTRYPOINT ["dotnet","/app/main.dll"] From dfa48959c1cc95e1f72d2be2b59c29e7425eedb2 Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Tue, 7 May 2024 15:15:24 +0200 Subject: [PATCH 3/7] debug --- Dockerfile-solution | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile-solution b/Dockerfile-solution index a8b09d9..8e20623 100644 --- a/Dockerfile-solution +++ b/Dockerfile-solution @@ -5,6 +5,7 @@ FROM mcr.microsoft.com/dotnet/aspnet:${RUNTIME_VERSION}-jammy-chiseled-extra AS FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build WORKDIR /src/ COPY . . +RUN ls -lsah RUN dotnet tool restore || true RUN dotnet restore RUN dotnet build -c Release --no-restore From 8882b6dc27879383a95f25cc3aad9a3864c7331b Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Tue, 7 May 2024 15:26:19 +0200 Subject: [PATCH 4/7] no more ls --- Dockerfile-solution | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile-solution b/Dockerfile-solution index 8e20623..a8b09d9 100644 --- a/Dockerfile-solution +++ b/Dockerfile-solution @@ -5,7 +5,6 @@ FROM mcr.microsoft.com/dotnet/aspnet:${RUNTIME_VERSION}-jammy-chiseled-extra AS FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build WORKDIR /src/ COPY . . -RUN ls -lsah RUN dotnet tool restore || true RUN dotnet restore RUN dotnet build -c Release --no-restore From 41899feea76b862b7939dc032e1844df0d30ecd1 Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Wed, 8 May 2024 10:32:59 +0200 Subject: [PATCH 5/7] tmp --- Dockerfile-solution | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Dockerfile-solution b/Dockerfile-solution index a8b09d9..96d7a89 100644 --- a/Dockerfile-solution +++ b/Dockerfile-solution @@ -6,22 +6,21 @@ FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build WORKDIR /src/ COPY . . RUN dotnet tool restore || true -RUN dotnet restore -RUN dotnet build -c Release --no-restore -RUN dotnet publish -c Release -o /app/publish --no-restore --no-build +RUN dotnet publish -c Release --artifacts-path /app WORKDIR /app/publish RUN rm -f appsettings.json appsettings.*.json || true FROM base AS final -ARG ASSEMBLY_NAME +ARG ASSEMBLY +ARG ARTIFACT WORKDIR /app -COPY --from=build /app/publish . -COPY --from=build /app/publish/${ASSEMBLY_NAME}.dll main.dll -COPY --from=build /app/publish/${ASSEMBLY_NAME}.runtimeconfig.json main.runtimeconfig.json +COPY --from=build /app/publish/${ARTIFACT}/release/ . +COPY --from=build /app/publish/${ARTIFACT}/release/${ASSEMBLY} main +COPY --from=build /app/publish/${ARTIFACT}/release/${ASSEMBLY}.runtimeconfig.json main.runtimeconfig.json COPY migrations/* /migrations/ COPY --from=busybox:uclibc /bin/cp /bin/cp COPY --from=busybox:uclibc /bin/cat /bin/cat COPY --from=busybox:uclibc /bin/ls /bin/ls EXPOSE 8080 EXPOSE 8081 -ENTRYPOINT ["dotnet","/app/main.dll"] +ENTRYPOINT ["/app/main"] From 7fac97a11c8b5986b36071a6eb1363ef284f0a52 Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Wed, 8 May 2024 13:12:53 +0200 Subject: [PATCH 6/7] tmp --- Dockerfile-solution => Dockerfile.simplified | 14 ++++----- actions/dotnet-validate-solution/action.yaml | 33 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) rename Dockerfile-solution => Dockerfile.simplified (62%) diff --git a/Dockerfile-solution b/Dockerfile.simplified similarity index 62% rename from Dockerfile-solution rename to Dockerfile.simplified index 96d7a89..b19ef1c 100644 --- a/Dockerfile-solution +++ b/Dockerfile.simplified @@ -3,20 +3,18 @@ ARG RUNTIME_VERSION FROM mcr.microsoft.com/dotnet/aspnet:${RUNTIME_VERSION}-jammy-chiseled-extra AS base FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build +ARG PROJECT WORKDIR /src/ COPY . . -RUN dotnet tool restore || true -RUN dotnet publish -c Release --artifacts-path /app -WORKDIR /app/publish RUN rm -f appsettings.json appsettings.*.json || true +RUN dotnet tool restore || true +RUN dotnet restore +RUN dotnet build -c Release --no-restore +RUN dotnet publish ${PROJECT} -c Release -o /app/publish --no-restore --no-build FROM base AS final -ARG ASSEMBLY -ARG ARTIFACT WORKDIR /app -COPY --from=build /app/publish/${ARTIFACT}/release/ . -COPY --from=build /app/publish/${ARTIFACT}/release/${ASSEMBLY} main -COPY --from=build /app/publish/${ARTIFACT}/release/${ASSEMBLY}.runtimeconfig.json main.runtimeconfig.json +COPY --from=build /app/publish . COPY migrations/* /migrations/ COPY --from=busybox:uclibc /bin/cp /bin/cp COPY --from=busybox:uclibc /bin/cat /bin/cat diff --git a/actions/dotnet-validate-solution/action.yaml b/actions/dotnet-validate-solution/action.yaml index e547ed8..db0a54e 100644 --- a/actions/dotnet-validate-solution/action.yaml +++ b/actions/dotnet-validate-solution/action.yaml @@ -7,6 +7,16 @@ inputs: description: The paths of the parent folder of the solution. required: true + project: + description: The path of a project file that is used to generate migrations with, if needed. Both project and migrations must be present for the check to happen. + default: "" + required: false + + migrations: + description: The path of a commited sql file, that needs to be validated against. Both project and migrations must be present for the check to happen. + default: "" + required: false + dotnet-version: description: The version of dotnet to use required: true @@ -71,3 +81,26 @@ runs: env: version: "" run: dotnet test --no-restore -warnaserror --logger:"console;verbosity=normal" + + - name: Validate migration + shell: bash + env: + VERSION: ${{ inputs.dotnet-version }} + PROJECT: ${{ inputs.project }} + COMMITED_SQL_FILE: ${{ inputs.migrations }} + if: ${{ '' != inputs.migrations && '' != inputs.project }} + run: | + GENERATED_SQL_FILE=/tmp/current.sql + version=$(echo $VERSION | cut -c 1) + if [ "$version" = "6" ]; then + argument="--version 6.0.25" + elif [ "$version" = "7" ]; then + argument="--version 7.0.14" + fi + dotnet list package --format=json | grep 'Microsoft.EntityFrameworkCore"' >/dev/null || exit 0 + dotnet tool install --global $argument dotnet-ef >/dev/null + dotnet ef migrations script --project "$PROJECT" -i -o "$GENERATED_SQL_FILE" + + cat < "$GENERATED_SQL_FILE" | sed '/INSERT INTO "__EFMigrationsHistory"/,/);$/d' > /tmp/a + cat < "$COMMITED_SQL_FILE" | sed '/INSERT INTO "__EFMigrationsHistory"/,/);$/d' > /tmp/b + diff -q /tmp/a /tmp/b >/dev/null || { echo "Unexpected difference:"; diff /tmp/a /tmp/b; exit 1; } From aae5d861823b22886a82f0843f6b0debff1a88c2 Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Wed, 8 May 2024 14:39:12 +0200 Subject: [PATCH 7/7] shenanigans --- Dockerfile.simplified | 11 ++++++++++- actions/dotnet-validate-solution/action.yaml | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Dockerfile.simplified b/Dockerfile.simplified index b19ef1c..3cd3349 100644 --- a/Dockerfile.simplified +++ b/Dockerfile.simplified @@ -6,11 +6,20 @@ FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION}-jammy AS build ARG PROJECT WORKDIR /src/ COPY . . -RUN rm -f appsettings.json appsettings.*.json || true +RUN <" ${PROJECT} +if [ $? -eq 0 ]; then + sed -i ${PROJECT} -e "s|.*|main|" +else + sed -i ${PROJECT} -e "s||main|" +fi +EOR RUN dotnet tool restore || true RUN dotnet restore RUN dotnet build -c Release --no-restore RUN dotnet publish ${PROJECT} -c Release -o /app/publish --no-restore --no-build +WORKDIR /app/publish +RUN rm -f appsettings.json appsettings.*.json || true FROM base AS final WORKDIR /app diff --git a/actions/dotnet-validate-solution/action.yaml b/actions/dotnet-validate-solution/action.yaml index db0a54e..95461bd 100644 --- a/actions/dotnet-validate-solution/action.yaml +++ b/actions/dotnet-validate-solution/action.yaml @@ -4,7 +4,7 @@ description: Lints and tests a domain. inputs: path: - description: The paths of the parent folder of the solution. + description: The path of the parent folder of the solution. required: true project: