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

Pipeline builder #6

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
87 changes: 87 additions & 0 deletions .github/workflows/pipeline-builder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: .NET CI

on:
pull_request:

jobs:
build-and-test:
runs-on: ubuntu-latest
permissions:
contents: read
issues: read
checks: write
pull-requests: write

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup .NET Core
uses: actions/setup-dotnet@v2
with:
dotnet-version: '8.0.x'

- name: Cache NuGet Packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-

- name: Restore Dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test #installs junit logger to generate junit test report
run: dotnet test --no-build --configuration Release --verbosity normal --logger "trx;logfilename=${{ runner.temp }}/testResults.trx" --logger "console" --collect:"XPlat Code Coverage"

- name: Convert Test Results
if: always()
run: |
dotnet tool install -g trx2junit
trx2junit ${{ runner.temp }}/testResults.trx --output ./results


# Assuming tests generate a `TestResults.xml` file
- name: Publish Test Results # publish test result report as a comment to PR
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
with:
files: ./results/testResults.xml

- name: Generate Coverage Summary Report # convert Cobertura coverage to markdown report
uses: irongut/[email protected]
if: always()
with:
filename: '**/coverage.cobertura.xml'
badge: true
fail_below_min: true
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: "50 80"


# required to add a comment to PR with coverage
- uses: jwalton/gh-find-current-pr@v1
if: always()
id: finder

- name: Add Coverage PR Comment # add comment with coverage report
uses: marocchino/sticky-pull-request-comment@v2
if: always()
with:
number: ${{ steps.finder.outputs.pr }}
recreate: true
path: code-coverage-results.md

- name: Run .NET Code Analysis
uses: dotnet/code-analysis@v1
id: code-analysis
with:
build-breaking: true
23 changes: 23 additions & 0 deletions Api.Tests/Api.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Api\Api.csproj" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions Api.Tests/Controllers/WeatherForecastControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Api.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;

namespace Api.Controllers.Tests
{
[TestClass()]
public class WeatherForecastControllerTests
{
private WeatherForecastController _controller;



[TestMethod]
public void Get_ReturnsWeatherForecasts()
{
// Arrange
var logger = new LoggerFactory().CreateLogger<WeatherForecastController>();
_controller = new WeatherForecastController(logger);

// Act
var result = _controller.Get();

// Assert
Assert.IsNotNull(result);
Assert.IsInstanceOfType<IEnumerable<WeatherForecast>>(result);
}
}
}
1 change: 1 addition & 0 deletions Api.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
31 changes: 31 additions & 0 deletions Api.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34112.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "Api\Api.csproj", "{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.Tests", "Api.Tests\Api.Tests.csproj", "{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Release|Any CPU.Build.0 = Release|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3476F32E-C804-43BA-B044-DDC9542049D6}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions Api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
18 changes: 18 additions & 0 deletions Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<UserSecretsId>9c9378d3-e7b0-487e-809f-cd3e98edf234</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions Api/Api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@Api_HostAddress = http://localhost:5263

GET {{Api_HostAddress}}/weatherforecast/
Accept: application/json

###
31 changes: 31 additions & 0 deletions Api/Api.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34112.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "Api.csproj", "{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.Tests", "..\Api.Tests\Api.Tests.csproj", "{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE41EC16-3F98-45AB-9D9D-D5F0C846E7DB}.Release|Any CPU.Build.0 = Release|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12B2EE3F-BB31-4ED8-9111-33BBE1B2D3FC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3476F32E-C804-43BA-B044-DDC9542049D6}
EndGlobalSection
EndGlobal
41 changes: 41 additions & 0 deletions Api/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}

private void foo() {
try {
var d = DateTime.Now;
}catch{
}
}
}

}
24 changes: 24 additions & 0 deletions Api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Api.csproj", "."]
RUN dotnet restore "./Api.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Api.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
RUN dotnet publish "Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.dll"]
25 changes: 25 additions & 0 deletions Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Loading