[英]Azure Devops nuget artifact feed and docker
有没有一种好的方法可以为 Devops 创建身份验证机制,以便能够访问工件 NuGet 提要? 我想为我的团队创建一个基础镜像,这样他们就可以从我们的 Azure Container Registry 中提取一个镜像,该镜像可以访问我们的 devops nuget 提要。 理想情况下,人们不必在每个从其主机构建系统获取 PAT 的项目中都拥有相同的 dockerfile 代码。 这也能让我们更好地使用 CICD。
我目前的解决方案
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
ARG IT_PAT
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\": \"https://pkgs.dev.azure.com/MNPIT/_packaging/MNP/nuget/v3/index.json\",\"username\": \"build\",\"password\": \"${IT_PAT}\"}]}"
RUN mkdir -p $HOME/.nuget/plugins
WORKDIR /deps
# Downloads and installs the NuGet credential plugin so we can login to the private NuGet feed
RUN curl https://github.com/microsoft/artifacts-credprovider/releases/download/v0.1.24/Microsoft.NetCore2.NuGet.CredentialProvider.tar.gz -L -o creds.tar.gz -s
RUN tar -xzf creds.tar.gz
RUN cp -r plugins/netcore/ ~/.nuget/plugins
NuGetAuthenticate
任务以将VSS_NUGET_ACCESSTOKEN
添加到环境变量( 更多信息)Docker
任务- task: NuGetAuthenticate@0
- task: Docker@2
displayName: 'build docker image'
inputs:
command: build
containerRegistry: 'happycodeacr'
repository: 'hc-app-sample-api-dev'
buildContext: '$(Pipeline.Workspace)/app'
Dockerfile: '$(Pipeline.Workspace)/app/src/HappyCode.Api/Dockerfile'
arguments: '--build-arg FEED_ACCESSTOKEN=$(VSS_NUGET_ACCESSTOKEN)'
tags: |
latest
$(Build.BuildId)
VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
环境变量NuGet.config
文件do.net restore
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /work
RUN curl -L https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | sh
ARG FEED_ACCESSTOKEN
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS \
"{\"endpointCredentials\": [{\"endpoint\":\"https://happycode.pkgs.visualstudio.com/_packaging/hc-nuget-feed/nuget/v3/index.json\", \"password\":\"${FEED_ACCESSTOKEN}\"}]}"
COPY ["NuGet.config", "./"]
COPY ["src/*/*.csproj", "./"]
RUN for projectFile in $(ls *.csproj); \
do \
mkdir -p ${projectFile%.*}/ && mv $projectFile ${projectFile%.*}/; \
done
RUN dotnet restore /work/HappyCode.Api/HappyCode.Api.csproj
# further instructions
我想为我的团队创建一个基础镜像,这样他们就可以从我们的 Azure Container Registry 中提取一个镜像,该镜像可以访问我们的 devops nuget 提要。
您可以在图像中包含凭据以实现此目的,但出于安全考虑,您最好添加一些额外的步骤或代码以从图像外部传递凭据。
根据您当前的解决方案,您可以使用系统预定义变量$(System.AccessToken)
来获取 azure devops CICD 管道中的安全令牌。 然后在 docker 构建任务中,将访问令牌作为参数传递给 ARG IT_PAT,
--build-arg IT_PAT=$(System.AccessToken)
除了使用 NuGet 凭证插件,您还可以使用 do.net cli 将凭证添加到 nuget 源。 然后在构建参数中传递$(System.AccessToken)
。 见下文:
ARG PAT
COPY . .
RUN dotnet nuget add source "your-source-url" --name "source-name" --username "useless" --password "$PAT" --store-password-in-clear-text
RUN dotnet restore
另一个解决方法是在构建上下文中包含 nuget.config。 但是您需要首先包含一个没有凭据的 nuget.config 文件,然后添加一个额外的nuget 任务以将凭据添加到配置文件中。 然后将 nuget.config 复制到您的 docker 文件中。 见下文:
添加 nuget 任务以在自定义命令下运行,以将凭据添加到 nuget.config 文件。
sources Add -Name "MyPackages" -Source "https://my.pkgs.visualstudio.com/_packaging/MyPackages/nuget/v3/index.json" -username any -password $(System.AccessToken) -ConfigFile Source/Nuget.config -StorePasswordInClearText
复制docker文件中的nuget.config,还原完成后不要忘记删除nuget.config文件:
COPY *.csproj .
COPY ./nuget.config .
RUN dotnet restore
RUN rm nuget.config
如果您使用的是基于 Yaml 的管道。 您还可以查看容器作业。 然后通过设置容器端点使用您的私有容器。 然后您可以直接在管道中使用还原任务。 参见下面的示例,nuget 恢复任务将在您的私有容器中运行,它可以通过为您的 nuget 提要指定属性vstsFeed
直接访问您的 azure 提要:
当您在管道中指定容器时,代理将首先获取并启动容器。 然后,作业的每一步都将在容器内运行。
container:
image: myprivate/registry:ubuntu1604
endpoint: private_dockerhub_connection
steps:
- task: NuGetCommand@2
inputs:
command: 'restore'
feedsToUse: 'select'
vstsFeed: 'my-azure-nuget-feed'
restoreSolution: '**/*.sln'
有关详细信息,您可以查看 此线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.