Azure Artifacts--全平臺的程序包管理倉庫(支持nuget)

寫在前面

大部分一定規模的團隊都有搭建私有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:[email protected]
- 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

大家遇到問題可參考以上文檔解決;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章