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

Azure Pipeline generate Swagger for ASP.NET Core Web API #2853

Open
Totot0 opened this issue May 5, 2024 · 3 comments
Open

Azure Pipeline generate Swagger for ASP.NET Core Web API #2853

Totot0 opened this issue May 5, 2024 · 3 comments

Comments

@Totot0
Copy link

Totot0 commented May 5, 2024

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();

@martincostello
Copy link
Collaborator

I have the impression that it launches the application

That's exactly how it works.

If you have things that you need your app to not do if run in this manner to generate the file, you should add a mechanism to your application to avoid those things when run a certain way, such as setting an environment variable that specifies a value that indicates skipping those operations.

@Totot0
Copy link
Author

Totot0 commented May 6, 2024

This is the only way to extract my swagger. There is no other solution?This is the only way to extract my swagger. There is no other solution?

@martincostello
Copy link
Collaborator

The app has to be run to execute the code to generate your OpenAPI document. If running the application does things you don't want your app to do when run in this context, you'll need to refactor to add the configurability you need to opt-out of whatever those things are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants