How can I set environment name without building every time?

Francis, Aswin 5 Reputation points
2023-01-30T06:30:06.68+00:00

I need to set custom environment names for builds that gets deployed to different servers. From looking at official documentation and other answers elsewhere, the recommended option is to pass environment name as an argument while calling dotnet publish command. However the way pipeline is currently set such that the first stage is building the code and later when we get approval for release, we approve the deploy stage which copies the artificat and puts them in a folder which IIS is refering to for the API site.

Now that we need environment names, to avoid building for all environment in the first stage, we are planning to release to, we have to build again during deploy stage. i.e,. if we approve QA, we call dotnet publish with 'QA' as environment name and use that and if we approve LDT, then it builds the same code again with 'LDT" as environment name argument for dotnet publish command and so on. Nothing else change but the environment name. So if we decide to push a branch to dev,qa, stage and LDT, we end up building the same code 4 times.

I see that the publish command creates 'environmentVariable' tag to our web.config file. Is it enough that I update that manually after doing a default publish without any environment name or are there other approached?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-30T13:43:51.45+00:00

    You set the environment as part of the deployment (not the build) by setting the environment variable (DOTNET_ENVIRONMENT, or ASPNETCORE_ENVIRONMENT).

    For example, in a docker compose, you can set it like this (in ourservice.yml):

     - name: ASPNETCORE_ENVIRONMENT
         value: ${ parameters.aspnetcoreEnvironment }
         ...
         ...
    

    And later in the deployment tool you can set the parameters.aspnetcoreEnvironment parameter to staging, or if you have different yml files for different environment, you can hardcode it.

    For more information, take a look at: