在我的 Azure Devops 项目中,在“Artifacts”选项卡下,我有一个包
MyPackage
。
在我的构建管道中,我有这一步:
- stage: Build
displayName: "Build"
jobs:
- job:
steps:
- task: Docker@2
inputs:
containerRegistry: 'TEST container registry'
repository: 'mycontainerregistry/backend'
command: 'buildAndPush'
buildContext: '$(System.DefaultWorkingDirectory)'
Dockerfile: '**/Dockerfile'
tags: |
$(Build.BuildId)
latest
正在构建的 Dockerfile 是 Visual Studio 生成的标准文件:
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 ["MyProject.API.csproj", "MyProject.API/"]
RUN dotnet restore "MyProject.API/MyProject.API.csproj"
COPY . .
WORKDIR "/src/MyProject.API"
RUN dotnet build "MyProject.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyProject.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.API.dll"]
现在,问题出在
dotnet restore
命令上。此步骤失败,因为恢复命令无法从 docker 构建上下文中找到 MyPackage nuget。如何让dotnet restore
在运行MyPackage
时找到docker build
?
如果您有私人提要,则需要使用
dotnet nuget add source
添加来源
dotnet nuget sources add -name "SomeName" -source https://pkgs.dev.azure.com/YourFeed/nuget/v3/index.json -username anything -password $TOKEN
要传递 System.AccessToken,您需要使用
ARG
FROM alpine
ARG TOKEN
RUN dotnet nuget sources add -name "SomeName" -source https://pkgs.dev.azure.com/YourFeed/nuget/v3/index.json -username anything -password $TOKEN
然后在YMAL
- task: Docker@2
inputs:
containerRegistry: 'devopsmanual-acr'
command: 'build'
Dockerfile: 'stackoverflow/85-docker/DOCKERFILE'
arguments: '--build-arg TOKEN=$(System.AccessToken)'
请拆分您的 buildAndPush,因为它不允许将参数传递到两个单独的任务中。欲了解更多详情,请查看这个问题。
另请确保您可以
Build Service
在 Feed 设置中具有贡献者角色。
Krystof Madey的解决方案对我不起作用。最后,我按照指南如何使用 Azure DevOps 在 Docker 构建中使用机密和使用 Azure DevOps System.AccessToken 进行 Docker 构建…安全地成功完成工作。
最后我的结果如下所示: 工作:
- job: create_image_and_push_to_acr
displayName: "Create image and push to ACR"
variables:
DOCKER_BUILDKIT: 1
steps:
- script: echo $(System.AccessToken) >> azure_devops_pat
displayName: Get PAT
- task: Docker@2
displayName: "Build"
inputs:
command: build
containerRegistry: $(connection_name)
Dockerfile: $(Build.SourcesDirectory)/Dockerfile
repository: "my_repository"
tags: $(applicationComponentVersion)
arguments: '--secret id=AZURE_DEVOPS_PAT,src=./azure_devops_pat'
- task: Docker@2
displayName: "Push"
inputs:
command: push
containerRegistry: $(connection_name)
repository: "my_repository"
tags: $(applicationComponentVersion)
在 docker 文件中:
RUN --mount=type=secret,id=AZURE_DEVOPS_PAT,dst=/azure_devops_pat \
dotnet nuget add source --username this_value_could_be_anything --password `cat /azure_devops_pat` --store-password-in-clear-text --name my_name "https://pkgs.dev.azure.com/.../nuget/v3/index.json" && \
dotnet restore "src/MyProject.csproj"