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

Add solution based validation #2

Merged
merged 8 commits into from
May 8, 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
33 changes: 33 additions & 0 deletions Dockerfile.simplified
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
ARG PROJECT
WORKDIR /src/
COPY . .
RUN <<EOR
grep -q "<AssemblyName>" ${PROJECT}
if [ $? -eq 0 ]; then
sed -i ${PROJECT} -e "s|<AssemblyName>.*</AssemblyName>|<AssemblyName>main</AssemblyName>|"
else
sed -i ${PROJECT} -e "s|</PropertyGroup>|<AssemblyName>main</AssemblyName></PropertyGroup>|"
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
COPY --from=build /app/publish .
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 ["/app/main"]
106 changes: 106 additions & 0 deletions actions/dotnet-validate-solution/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Dotnet Validate

description: Lints and tests a domain.

inputs:
path:
description: The path 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

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"

- 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; }