Skip to content

Commit

Permalink
feat(ProperDockerfile): 100 points if dotnet build or `dotnet publi…
Browse files Browse the repository at this point in the history
…sh` exists

docs(ProperDockerfile): Removed empty list item
  • Loading branch information
Vincent Mahnke committed Mar 9, 2023
1 parent eeed8bb commit c209e83
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["CoolService.csproj", "CoolService/"]
RUN dotnet restore "CoolService.csproj"
COPY . .
WORKDIR "CoolService"
RUN dotnet publish "CoolService.csproj" -c Release -o /app/build

FROM build AS test
WORKDIR "CoolService"
RUN dotnet test "CoolService.csproj" -c Release -o /app/test

FROM build AS publish
RUN dotnet tool install --global dotnet-sonarscanner
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN dotnet sonarscanner begin /k:"ScorecardGenerator" /d:sonar.host.url="http://sonarqube:9000" /d:sonar.login="admin" /d:sonar.password="admin"
RUN dotnet publish "CoolService.csproj" -c Release -o /app/build
RUN dotnet sonarscanner end /d:sonar.login="admin" /d:sonar.password="admin"

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoolService.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ScorecardGenerator.Test.Helper;
using Serilog;

namespace ScorecardGenerator.Test.Checks.ProperDockerfile.Result100;

public class DotnetPublishAndSonarscanner : TestWithNeighboringCsprojAndDockerfileFixture
{
[Test]
public void Returns100Points()
{
var logger = new LoggerConfiguration().CreateLogger();
var check = new ScorecardGenerator.Checks.ProperDockerfile.Check(logger);
var deductions = check.SetupLoggerAndRun(AbsolutePathToProjectFile);
deductions.CountAndFinalScore(0, 100);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<Content Include="wwwroot\css\site.css" />
<Content Include="wwwroot\css\site.less" />
<Content Include="wwwroot\css\site.min.css" />
<Content Include="wwwroot\favicon.ico" />
<Content Include="wwwroot\icon.jpg" />
<Content Include="wwwroot\icon.png" />
<Content Include="wwwroot\js\signout-redirect.js" />
<Content Include="wwwroot\lib\bootstrap\css\bootstrap.css" />
<Content Include="wwwroot\lib\bootstrap\css\bootstrap.css.map" />
<Content Include="wwwroot\lib\bootstrap\css\bootstrap.min.css" />
<Content Include="wwwroot\lib\bootstrap\fonts\glyphicons-halflings-regular.eot" />
<Content Include="wwwroot\lib\bootstrap\fonts\glyphicons-halflings-regular.svg" />
<Content Include="wwwroot\lib\bootstrap\fonts\glyphicons-halflings-regular.ttf" />
<Content Include="wwwroot\lib\bootstrap\fonts\glyphicons-halflings-regular.woff" />
<Content Include="wwwroot\lib\bootstrap\fonts\glyphicons-halflings-regular.woff2" />
<Content Include="wwwroot\lib\bootstrap\js\bootstrap.js" />
<Content Include="wwwroot\lib\bootstrap\js\bootstrap.min.js" />
<Content Include="wwwroot\lib\jquery\jquery.js" />
<Content Include="wwwroot\lib\jquery\jquery.min.js" />
<Content Include="wwwroot\lib\jquery\jquery.min.map" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="1.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0">
<HintPath>..\..\..\..\..\..\thangchung.HARVEYNASH\.nuget\packages\identityserver4\1.1.0\lib\net452\IdentityServer4.dll</HintPath>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0">
<HintPath>..\..\..\..\..\..\thangchung.HARVEYNASH\.nuget\packages\identityserver4\1.1.0\lib\net452\IdentityServer4.dll</HintPath>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions ScorecardGenerator.Test/ScorecardGenerator.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<EmbeddedResource Include="Checks\ProperDockerfile\Result0\NoBuild.Dockerfile" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result100\DotnetBuildAndSonarscanner.csproj.xml" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result100\DotnetBuildAndSonarscanner.Dockerfile" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result100\DotnetPublishAndSonarscanner.csproj.xml" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result100\DotnetPublishAndSonarscanner.Dockerfile" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result50\DotnetBuildOnly.csproj.xml" />
<EmbeddedResource Include="Checks\ProperDockerfile\Result50\DotnetBuildOnly.Dockerfile" />
<EmbeddedResource Include="Checks\ProperDockerfile\ResultSkipped\NoDockerfile.csproj.xml" />
Expand Down
6 changes: 3 additions & 3 deletions ScorecardGenerator.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/Environment/Highlighting/HighlightingEnabled/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/Environment/Highlighting/HighlightingMode/@EntryValue">OnlyMarkers</s:String>
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/vimaster/Library/Caches/JetBrains/Rider2022.3/resharper-host/temp/Rider/vAny/CoverageData/_ScorecardGenerator.-1550194268/Snapshot/snapshot.utdcvr</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=0d266b78_002D165c_002D4c82_002Daae2_002D4d80b1157fca/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;

<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=0d266b78_002D165c_002D4c82_002Daae2_002D4d80b1157fca/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>

Expand All @@ -11,7 +11,7 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5c24d107_002D0088_002D4351_002Da98c_002D618ca1b70306/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Session" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=78d823dc_002D30e0_002D459f_002Dae18_002Dc8fb019783d4/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=78d823dc_002D30e0_002D459f_002Dae18_002Dc8fb019783d4/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
</wpf:ResourceDictionary>
4 changes: 2 additions & 2 deletions ScorecardGenerator/Checks/ProperDockerfile/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ protected override IList<Deduction> Run(string absolutePathToProjectFile)
}

var dockerfileContent = File.ReadAllText(dockerfile);
if (!dockerfileContent.Contains("dotnet build"))
if (!dockerfileContent.Contains("dotnet build") && !dockerfileContent.Contains("dotnet publish"))
{
return new List<Deduction> {Deduction.Create(Logger, 100, "Dockerfile at {Location} does not contain 'dotnet build'", dockerfile)};
return new List<Deduction> {Deduction.Create(Logger, 100, "Dockerfile at {Location} does not contain 'dotnet build' or 'dotnet publish", dockerfile)};
}

if (!dockerfileContent.Contains("dotnet sonarscanner"))
Expand Down
4 changes: 2 additions & 2 deletions ScorecardGenerator/Checks/ProperDockerfile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## About
This check returns **100 points** when a `Dockerfile` next to the `.csproj` exists and contains `dotnet build`.
A `Dockerfile` that doesn't contain `dotnet sonarscanner` results in a **50 point deduction**.
A `Dockerfile` that doesn't contain `dotnet build` results in a **100 point deduction**.
A `Dockerfile` that contains neither `dotnet build` nor `dotnet publish` results in a **100 point deduction**.
The Scorecard **skips this check** if no `Dockerfile` exists next to the `.csproj` file.

## How to achieve 100 points
Expand All @@ -18,6 +18,6 @@ This ensures:
- executables generated locally match the ones running in generated images inside the Kubernetes cluster
- the project compiles across platforms
- local and CI/CD environments can run all build steps
-

### Run code analysis
This allows local runs of code analysis

0 comments on commit c209e83

Please sign in to comment.