Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Azure Pipeline generate Swagger for ASP.NET Core Web API

Open Totot0 opened this issue 9 months ago • 4 comments

I have an ASP.NET Core Web API that I build in Azure Devops. I have to extract the swagger during the build.

I therefore issue the following command:

- task: CmdLine@2 displayName: 'Generate Swagger' inputs: script: | dotnet new tool-manifest dotnet tool install --version 6.5.0 Swashbuckle.AspNetCore.Cli cd Api set ASPNETCORE_ENVIRONMENT=Development dotnet swagger tofile --output $(Build.ArtifactStagingDirectory)/swagger.json $(System.DefaultWorkingDirectory)/src/Api/bin/Release/net8.0/Api.dll v1 workingDirectory: '${{ parameters.workingDirectory }}'

The problem is that I have the impression that it launches the application because it looks for my appsettings.json and once filled, it tries to connect to the BDD.

How do I generate my Swagger in CI?

Load appsettings.json file in Main method :

IConfiguration Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .AddEnvironmentVariables() .Build();

Totot0 avatar May 05 '24 21:05 Totot0