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

CLI: Allow setting environment at runtime #872

Closed
nascosto opened this issue Aug 31, 2018 · 16 comments
Closed

CLI: Allow setting environment at runtime #872

nascosto opened this issue Aug 31, 2018 · 16 comments

Comments

@nascosto
Copy link

I would like the ability to specify the environment of the API when running the CLI to control which appsettings files get used. Currently, it defaults to Production, but our production values are tucked away into secrets and loaded outside the appsettings file. I'd like to point it to Development.

dotnet run has the ability to set environment via --environment and has the ability to set environment variables in general using =. It also can pull the ASPNETCORE_ENVIRONMENT from the launchSettings.json

I can still set ASPNETCORE_ENVIRONMENT at the user/system level, but I'd prefer the flexibility of one of these other options.

@domaindrivendev domaindrivendev added this to the Backlog milestone Sep 2, 2018
@domaindrivendev
Copy link
Owner

I don't see --environment as an option for dotnet run:

screen shot 2018-09-04 at 10 53 04 pm

Also, I don't understand how the introduction of such a parameter would provide any more flexibility over passing ASPNETCORE_ENVIRONMENT as an environment variable to the command as follows:

> ASPNETCORE_ENVIRONMENT=development dotnet swagger tofile --output wwwroot/api-docs/v1/swagger.json bin/Debug/netcoreapp2.0/MyAPI.dll v1

@nascosto
Copy link
Author

nascosto commented Sep 5, 2018

So it turns out the dotnet run call just takes in additional arguments which are then added to the app configuration object. You can see this option here
image

ASPNETCORE_ENVIRONMENT just happens to be translated to the environment configuration option. So by using --environment Development or environment=Development at the end of the dotnet run command, you are setting the environment configuration. Adding --foo bar or foo=bar would set foo to bar in the configuration usable during startup.

The additional flexibility comes from trying to use the command inside the csproj file like so
<Target Name="SwaggerToFile" AfterTargets="AfterBuild"> <Exec Command="dotnet swagger tofile --output $(OutputPath)swagger.json $(OutputPath)$(AssemblyName).dll v1" /> </Target>
For whatever reason, I can't have two commands (e.g. set = && dotnet swagger tofile) in the same command which means I can't set the environment variable this way, hence why I'd like it as part of the actual tofile command.

It is a niche case, but I figured I'd ask.

@persalmi
Copy link

persalmi commented Apr 3, 2019

Just ran into this missing option to set environment. Seems to be needed when using the Manage User Secrets feature of VS, then without the environment = Development setting, the "dotnet swagger tofile..." command embedded in a .csproj fails if you use config properties for oauth2 setup in the Startup.cs.

@donald93
Copy link

I have a similar use case where we wish to use the CLI to create contract diffs during PRs. Unfortunately we access secrets in our Startup.cs file that are not available at build time and are only available when we deploy. This causes a particular piece of our Startup to error because it's attempting to use a configuration variable that's null.

Being able to specify configuration options similar to the Environment would allow us to use the CLI without making code changes.

@curbol
Copy link

curbol commented Nov 15, 2019

I have the same use case as persalmi. I'm using Manage User Secrets with a script in the .csproj file

<Target Name="SwaggerToFile" AfterTargets="AfterBuild">
    <Exec Command="dotnet swagger tofile --output swagger.json $(TargetPath) v1" />
</Target>

I'm getting an error that settings are null.

@andrew-vdb
Copy link

I finally able to make it work

Set env variable like this
image

Notice there is NO && ^ between setting env variable and calling dotnet swagger

Within the project, dont forget to put the appsettings.Development.json

image

@joaopgrassi
Copy link

If using the CLI as part of you build pipelines on Azure DevOps, you can do it like this:

- task: CmdLine@2
  displayName: 'Generating swagger json'
  inputs:
    script: |
      set ASPNETCORE_ENVIRONMENT=Development
      swagger tofile --serializeasv2 --output swagger.json MyService.Dll 1.0
    workingDirectory: <your dotnet publish directory>

@domaindrivendev
Copy link
Owner

@nascosto - it seems there's very reasonable solutions here, can this be closed?

@nascosto
Copy link
Author

nascosto commented Mar 5, 2020

Yeah. Let's close this for now. Thanks.

@nascosto nascosto closed this as completed Mar 5, 2020
@DanielHabenicht
Copy link

DanielHabenicht commented Jan 31, 2022

Just if anybody still looks for a solution to make it work in .csproj:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)$(AssemblyName).dll v1" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=$(ASPNETCORE_ENVIRONMENT)" />
</Target>

@ilcos91
Copy link

ilcos91 commented Aug 29, 2022

Just if anybody still looks for a solution to make it work in .csproj:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)$(AssemblyName).dll v1" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=$(Configuration)" />
</Target>

With this instruction I'm getting "Debug" and not the Environment Variable, how is this supposed to work?

@DanielHabenicht
Copy link

Just if anybody still looks for a solution to make it work in .csproj:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)$(AssemblyName).dll v1" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=$(Configuration)" />
</Target>

With this instruction I'm getting "Debug" and not the Environment Variable, how is this supposed to work?

What are you trying to do?
The environment variable is set according to the targeted configuration.

@ilcos91
Copy link

ilcos91 commented Aug 30, 2022

I'm trying to compile the project and I need the environment to be able to connect to the correct key vault.
This means that I have to create a new configuration and call it "Development"? And when I run my pipeline I should create also a configuration to define the environment

Just if anybody still looks for a solution to make it work in .csproj:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)$(AssemblyName).dll v1" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=$(Configuration)" />
</Target>

With this instruction I'm getting "Debug" and not the Environment Variable, how is this supposed to work?

What are you trying to do?

I'm trying to compile the project and I need the environment to be able to connect to the correct key vault.

The environment variable is set according to the targeted configuration.

This means that I have to create a new configuration and call it "Development"? And when I run my pipeline I should create also a configuration to define the environment. What I think is the best is being able to read the environment variable already defined so you don't have to define the environment in multiple places

@DanielHabenicht
Copy link

DanielHabenicht commented Aug 30, 2022

Well then simply use the variable: https://stackoverflow.com/questions/48618357/how-to-get-environment-variable-in-csproj-file

I updated my code above.

@greg-signi
Copy link

greg-signi commented Nov 7, 2022

Trying to do this using gitlab pipelines without much success:

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0 && set myvar=https://example.com
- ./tools/swagger tofile --output swagger.json MyProject.dll v1

Tried many variations without success:

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0
- set myvar=https://example.com && ./tools/swagger tofile --output swagger.json MyProject.dll v1

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0
- set myvar=https://example.com
- ./tools/swagger tofile --output swagger.json MyProject.dll v1

@mehmetaltuntas
Copy link

mehmetaltuntas commented Jun 13, 2023

passing environment variables :

Trying to do this using gitlab pipelines without much success:

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0 && set myvar=https://example.com
- ./tools/swagger tofile --output swagger.json MyProject.dll v1

Tried many variations without success:

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0
- set myvar=https://example.com && ./tools/swagger tofile --output swagger.json MyProject.dll v1

- dotnet tool install --tool-path tools Swashbuckle.AspNetCore.Cli --version 6.4.0
- set myvar=https://example.com
- ./tools/swagger tofile --output swagger.json MyProject.dll v1

is this working or any other options above?

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