Azure Artifacts--全平台的程序包管理仓库(支持nuget)

发布时间 2023-03-22 19:10:58作者: 乔达摩(嘿~)

写在前面

大部分一定规模的团队都有搭建私有nuget的需求;例如:

而我们使用的Azure DevOps 平台本身就提供了Artifacts, Artifacts不单只支持nuget包,还支持Npm、Maven、pip等;Share packages publicly from Azure Artifacts - Public Preview - Azure DevOps  Blog

这里简单说说nuget的Azure Devops Artifacts的集成;

先创建Artifacts Feed

Feed就是仓库的集合;也就是nuget、npmjs、pip等仓库都是一个feed的:

image-20230202181259695

我这里创建了一个Feed: samm-feed

点击“Connect to feed” 可以看到支持的仓库类型;

image-20230202181408249

接下来我们nuget的怎么用;

vs新增nuget包源

vs2022为例:工具-》nuget 包管理器-》nuget包源,新增一个Feed:

名称

samm-feed

Source

https://pkgs.dev.azure.com/jack4it/_packaging/samm-feed/nuget/v3/index.json

Note: 每台机器都要设置一次

发布包

Nuget.exe方式

1、先下载nuget.exe并配置到环境变量(直接放c盘windows目录也行)

https://go.microsoft.com/fwlink/?linkid=2099732

2、创建一个 nuget.config 文件到 .csproj or .sln 所在目录

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="samm-feed" value="https://pkgs.dev.azure.com/jack4it/_packaging/samm-feed/nuget/v3/index.json" />
  </packageSources>
</configuration>

3、restore包

Run this command in your project directory

nuget restore

4、发布包

Publish a package by providing the package path, an API Key (any string will do), and the feed URL

nuget.exe push -Source "samm-feed" -ApiKey az Siluzan.Infrastructure.0.0.1.nupkg

5、设置其他人publish权限

azure feed创建的源其他同事是只有只读权限的;

需要在feed-setting-》permission这里加上最少contributor权限才行;

1671606688710

vs插件方式

Nupack暂不支持vs2022,待更新

Azure Pipeline 拉取私有仓库镜像

背景

一般项目用了samm-feed私有镜像的包后,直接用原来的Pipeline yaml构建会报如下错:

/src/src/*.Cutapi.WebApi/*.Cutapi.WebApi.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/jack4it/_packaging/samm-feed/nuget/v3/index.json.        

image-20230202124406057

1、先获取private feed的个人令牌

按链接步骤获取获取个人令牌

https://learn.microsoft.com/zh-cn/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows

2、新增nuget.config

在解决方案目录新增文件nuget.config,内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <packageSources>
    <add key="public" value="https://api.nuget.org/v3/index.json" />
    <add key="samm-feed" value="https://pkgs.dev.azure.com/jack4it/_packaging/samm-feed/nuget/v3/index.json" />   
  </packageSources>
</configuration>

3、修改Dockerfile

net6为例,其他版本参考本节开头的文件解决;

修改基础镜像

#FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM gebiwangshushu/hei-dotnet-sdk6-azurefeed-certprovider AS build #改为这个

RUN dotnet restore指令改为如下指令:

...

COPY nuget.config .
ARG FEED_USERNAME
ARG FEED_ACCESSTOKEN
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{\"endpointCredentials\": [{\"endpoint\":\"https://pkgs.dev.azure.com/jack4it/_packaging/samm-feed/nuget/v3/index.json\", \"username\":\"${FEED_USERNAME}\", \"password\":\"${FEED_ACCESSTOKEN}\"}]}"
RUN echo $VSS_NUGET_EXTERNAL_FEED_ENDPOINTS \
&& dotnet restore "src/Siluzan.Cutapi.WebApi/Siluzan.Cutapi.WebApi.csproj"  

...

4、修改Azure DevOps Pipeline

新增个人令牌参数

variables:
- name: azureFeedUsename
  value: <个人令牌用户名> eg:wangsiheng@gmail.com
- name: azureFeedToken
  value: <步骤1的个人令牌>

镜像构建和推送要改为如下逻辑

  - task: Docker@2 build
    inputs:
      containerRegistry: '**.azurecr.cn'
      repository: '<你的镜像名>'
      command: 'build'
      Dockerfile: 'src/***/Dockerfile' #你Dockerfile的目录
      buildContext: './'
      arguments: '--build-arg FEED_USERNAME=$(azureFeedUsename) --build-arg  FEED_ACCESSTOKEN=$(azureFeedToken)'

  - task: Docker@2 push
    inputs:
      containerRegistry: '**.azurecr.cn'
      repository: '<你的镜像名>'
      command: 'push'

总结

azure pipeline 的拉取feed的nuget的问题花了不少时间踩坑,留个记录;

总体来说使用Azure Artifacts 来做私有仓库比自己搭建的好用;

收费上的话:

每个订阅有2G的免费存储,2G以上部分$2/1G/一个月,更多...

参考

azure pipeline 的拉取feed的nuget的参考文档:https://github.com/dotnet/dotnet-docker/blob/main/documentation/scenarios/nuget-credentials.md

大家遇到问题可参考以上文档解决;