使用 Azure DevOps 和 DockerFile 设置网络核心应用的程序集版本



我正在使用Visual Studio创建的DockerFile来创建我的图像。我想使用 Azure DevOps 进行生成,并将映像中的程序集版本设置为管道中的版本。我觉得我必须改变这条线,但我不知道该怎么做。

RUN dotnet build "ImportBatch.Api.csproj" -c Release -o /app/build

我的 DockerFile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ImportBatch.Api/ImportBatch.Api.csproj", "ImportBatch.Api/"]
COPY ["ImportBach.Dto/ImportBatch.Dto.csproj", "ImportBach.Dto/"]
COPY ["Common/Common.csproj", "Common/"]
COPY ["Common.Dto/Common.Dto.csproj", "Common.Dto/"]
COPY ["Common.Azure.Storage/Common.Azure.Storage.csproj", "Common.Azure.Storage/"]
RUN dotnet restore "ImportBatch.Api/ImportBatch.Api.csproj"
COPY . .
WORKDIR "/src/ImportBatch.Api"
RUN dotnet build "ImportBatch.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ImportBatch.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Cardinal.Software.ImportBatch.Api.dll"]

从管道:

- task: Docker@1
displayName: 'Build the image'
inputs:
dockerfile: 'Dockerfile'
imageName: '$(imageRepoName):$(GitVersion.SemVer)'
arguments: '--build-arg version=$(GitVersion.SemVer)'
continueOnError: true

在 DockerFile 上:

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /src
COPY . .
ARG version  #define the argument
#use this argument while running build or publish, for example
RUN dotnet publish "./yourProject.csproj" -c Release /p:Version=${version} -o /app/publish --no-restore

对于 SDK 样式的项目,程序集版本属性是自动生成的,因此您可以立即使用p:Version=1.2.3.4。请在此处查看示例(底部(

dotnet build -p:Version=1.2.3.4

最新更新