[英]How to set environment variables (ASPNETCORE_ENVIRONMENT) in AzureDevops for Dockerfile?
I was planning to use Azure Devops to do the CI/CD for my dockerized .NET Core API.我打算使用 Azure Devops 为我的 dockerized .NET Core API 做 CI/CD。 My API now have
appsettings.json
and appsettings.Development.json
.我的 API 现在有
appsettings.json
和appsettings.Development.json
。
Below are my Dockerfile:下面是我的 Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_ENVIRONMENT Development
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["Api/Api.csproj", "Api/"]
RUN dotnet restore "Api/Api.csproj"
COPY . .
WORKDIR "/src/Api"
RUN dotnet build "Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.dll", "--environment=Development"]
As can seen, I set the environment at ASPNETCORE_ENVIRONMENT
and also the ENTRYPOINT ["dotnet", "Api.dll", "--environment=Development"]
.可以看出,我将环境设置在
ASPNETCORE_ENVIRONMENT
以及ENTRYPOINT ["dotnet", "Api.dll", "--environment=Development"]
。 However how can I make it flexible in Azure Devops pipeline?但是,如何使其在 Azure Devops 管道中变得灵活?
For example having different environments like development
and production
.例如具有不同的环境,如
development
和production
。 So let say I have another appsettings.Production.json
, the project would automatically pick up this file instead of appsettings.Development.json
if I set the environment to Production
.所以假设我有另一个
appsettings.Production.json
,如果我将环境设置为Production
,项目将自动获取此文件而不是appsettings.Development.json
。
Is there anyway to do this or am I understanding this whole flow wrong?无论如何要这样做还是我理解这整个流程是错误的? Thank you.
谢谢你。
Check the answer in this case: Inject env variable into build stage of image在这种情况下检查答案: Inject env variable into build stage of image
You can set an ARG var_name
and reference ENV to the ARG variables.您可以设置
ARG var_name
并将 ENV 引用到 ARG 变量。 Then you can replace those variables when docker build the image $ docker build --build-arg var_name=$(VARIABLE_NAME)
然后您可以在 docker 构建映像时替换这些变量
$ docker build --build-arg var_name=$(VARIABLE_NAME)
For example the add ARG in dockerfile, and have the ENV variable refer to it:例如在 dockerfile 中添加 ARG,并让 ENV 变量引用它:
ARG SECRET
ENV ASPNETCORE_ENVIRONMENT=$SECRET
You can use dock build task and dock push task separately, as buildandpush command cannot accept arguments.您可以分别使用停靠构建任务和停靠推送任务,因为 buildandpush 命令不能接受 arguments。 And set a variable
SECRET
in your pipeline.并在您的管道中设置一个变量
SECRET
。
The set the Build Arguments SECRET= $(SECRET)
to replace the ARG SECRET设置 Build Arguments
SECRET= $(SECRET)
来替换 ARG SECRET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.