Skip to content

feat: add API versioning to Dockerfile and Swagger configuration #179

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

Merged
merged 3 commits into from
Nov 25, 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
2 changes: 2 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
build-args: |
API_VERSION=${{ steps.sanitize-version.outputs.version }}
file: ./src/FIAP.TechChallenge.ByteMeBurger.Api/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ Endpoints are protected since JWT token is now required. Since this application

### Prerequisites
- Docker with Minikube configured
- [webhook.site](https://webhook.site) token


<details>
Expand Down Expand Up @@ -124,10 +123,6 @@ kubectl port-forward service/svc-seq 30008:80
```bash
minikube tunnel
```
#### To forward mercado pago webhook notifications to host
```bash
whcli forward --token=f513d4a6-4dbd-4e32-93f5-b35376f33c89 --target=http://localhost/api/notifications/mercadopago
```

#### Testing
You can use the [postman collection](/postman) for testing
Expand Down Expand Up @@ -156,23 +151,16 @@ Once you are done, you can stop the services running [rollback.ps1](kubernetes/r
- **Endpoint:** `POST /api/customers`
- Requires `Customer/Admin` role

### PaymentsController

- **Endpoint:** `POST /api/payments`
- Requires `Admin` role

### OrdersController

- **Endpoint:** `POST /api/orders`
- Requires `Customer/Admin` role
- **Endpoint:** `GET /api/orders/{id}`
- Requires `Admin/Kitchen` role

### NotificationsController

- **Endpoint:** `POST /api/notifications`
- No role required

## This repo on the infrastructure

![Architecture Diagram](aws-infra-phase-4.png)
![Architecture Diagram](doc/aws-infra-phase-4.png)

### K8S
![Architecture Diagram](doc/k8s.png)
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added doc/k8s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Bmb.Auth;
using Bmb.Tools.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Customers;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.ObjectModel;
using Bmb.Auth;
using Bmb.Domain.Core.ValueObjects;
using Bmb.Tools.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Orders;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Bmb.Auth;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Products;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
using Bmb.Domain.Core.ValueObjects;
using Bmb.Tools.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
3 changes: 2 additions & 1 deletion src/FIAP.TechChallenge.ByteMeBurger.Api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ RUN dotnet restore src/FIAP.TechChallenge.ByteMeBurger.Api

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "src/FIAP.TechChallenge.ByteMeBurger.Api/FIAP.TechChallenge.ByteMeBurger.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
ARG API_VERSION
RUN dotnet publish "src/FIAP.TechChallenge.ByteMeBurger.Api/FIAP.TechChallenge.ByteMeBurger.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false /p:Version=$API_VERSION
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

API_VERSION build argument is not properly configured in GitHub Actions workflow

The Dockerfile uses the API_VERSION build argument, but in the GitHub Actions workflow, this argument is only set in the main branch workflow under the build-docker-image job. The version is not set for pull request builds or other workflow runs, which could lead to inconsistent versioning.

  • In .github/workflows/publish-api-docker-image.yml, add the build-arg to the docker build step:
build-args: |
  API_VERSION=${{ steps.meta.outputs.version }}
🔗 Analysis chain

Verify API_VERSION usage in GitHub Actions workflow

The Version parameter is correctly implemented in the publish command. Let's verify its integration with the GitHub Actions workflow.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the API_VERSION argument is properly set in GitHub Actions workflow

# Check if the workflow file exists and contains the API_VERSION build argument
rg -A 5 "build-arg API_VERSION" .github/workflows/

Length of output: 52


Script:

#!/bin/bash
# Let's check for any docker build commands in workflow files
rg -l "docker build" .github/workflows/

# Also check the content of workflow files for version-related configurations
fd -e yml -e yaml . .github/workflows/ -x cat {}

Length of output: 22117



FROM base AS final
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Product>Bmb Orders/Customer API</Product>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -18,8 +20,8 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Bmb.Auth" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.17" />
<PackageReference Include="Bmb.Tools" Version="0.0.4" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
Expand All @@ -28,7 +30,6 @@
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
39 changes: 4 additions & 35 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using Bmb.Auth;
using Bmb.Tools.Auth;
using Bmb.Tools.OpenApi;
using FIAP.TechChallenge.ByteMeBurger.DI;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Serilog;
using Serilog.Events;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
Expand Down Expand Up @@ -52,36 +50,7 @@ public static void Main(string[] args)
// https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-8.0#log-automatic-400-responses
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Tech Challenge Restaurant API", Version = "v1", Extensions =
{
{
"x-logo",
new OpenApiObject
{
{
"url",
new OpenApiString(
"https://avatars.githubusercontent.com/u/165858718?s=384")
},
{
"background",
new OpenApiString(
"#FF0000")
}
}
}
}
});

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});

builder.Services.ConfigBmbSwaggerGen();
var jwtOptions = builder.Configuration
.GetSection("JwtOptions")
.Get<JwtOptions>();
Expand All @@ -107,7 +76,7 @@ public static void Main(string[] args)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseBmbSwaggerUi();
}

app.UseSerilogRequestLogging();
Expand Down
Loading