Issue
I have an API that's wrapped as a Docker Image and having 3 different appsettings file, they are appsettings.Development.json
, appsettings.QA.json
and appsettings.Production.json
, each of them contains the connection string for different environment needs. And a main appsettings.json
for other common credentials.
I've used the Environment Variable in CI to replace the following lines:
ENV ASPNETCORE_ENVIRONMENT #{environment-profile}#
...
ENTRYPOINT ["dotnet", "Api.dll", "--environment=#{environment-profile}#"]
environment-profile
= Development in my current CI.
However, I want to make it like the environment-profile
can be flexible? Like when I want to deploy to another environment such as QA. The environment-profile
can switch to QA without manually changing it in the CI pipeline?
Is there a way to do what I want? Or what is the normal way people do to adapt different environment app settings in CI/CD pipeline?
Appreciate it if anyone can help, thanks!
Solution
If the ASPNETCORE_ENVIRONMENT
environment variable is defined like below in your dockerfile. The --environment=#{environment-profile}#
in the ENTRYPOINT
is not refering to the ASPNETCORE_ENVIRONMENT
environment variable. You just assigned the --environment
to value #{environment-profile}#
.
ENV ASPNETCORE_ENVIRONMENT #{environment-profile}#
...
ENTRYPOINT ["dotnet", "Api.dll", "--environment=#{environment-profile}#"]
I guess you properly used a replace token task in your CI pipeline to replace the #{environment-profile}#
in the dockerfile with the Variable defined in your pipeline.
To make environment-profile
to be flexible, if you do not want to change your dockerfile. You can continue to use above way to replace the environment-profile
using a replace token task. All you need to do is to make the Variable you defined in your pipeline Settabe at queue time
. See below: Tick the Settabe at queue time
to make it settable
Then when you run you pipeline again to deploy to QA environment. You can just Click the Variables to update the variable environment-profile
to QA
:
But i would recommend using ARG
instead of ENV
and set the ARG to the Pipeline variable in your docker build task: See below:
First, Change your dockerfile like below:
ARG ASPNETCORE_ENVIRONMENT
...
ENTRYPOINT ["dotnet", "Api.dll", "--environment=$ASPNETCORE_ENVIRONMENT"]
Then add Build Arguments
like below to assign the pipeline variable environment-profile
to ARG ASPNETCORE_ENVIRONMENT.
When you run you pipeline to deploy to a different environment. You need to update the variable's value accordingly at the Run Pipeline page
.
If you are using yaml pipeline. You can use runtime parameters, which will enable you to select the environment when you run your pipeline.
parameters:
- name: Environment-Profile
type: string
values:
- Development
- QA
- Product
....
steps:
- task: Docker@0
displayName: 'Build an image'
inputs:
containerregistrytype: 'Container Registry'
dockerRegistryConnection: MyDoccker
dockerFile: '**/dockerfile'
buildArguments: 'ASPNETCORE_ENVIRONMENT="${{parameters.Environment-Profile}}"'
defaultContext: false
context: .
Then when you run the pipeline. You can select which environment to deploy in the UI page:
Answered By - Levi Lu-MSFT Answer Checked By - Terry (PHPFixing Volunteer)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.