NuGet V3 源配置、发布与还原
NuGet 从 V3 服务索引发现发布、注册元数据与 flat container 地址。Arka 支持 .NET 8 原生 push 使用的 /api/v2/package/,发布路径也可省略尾斜杠。
创建仓库
在已登录管理员 profile 的环境中运行以下 Bash 命令,需安装 jq。将示例地址换成实际 HTTPS 入口。Hosted 接收发布,Proxy 缓存上游,Group 按 Hosted、Proxy 的顺序读取。发布身份需要目标 Hosted 的 write,消费身份需要 Group 和命中成员的 read。
首次发放凭据时,使用各自账号的 profile 执行 arkactl --json tokens create --name nuget-ci --expires-in-days 30 | jq -er .plaintext,将输出直接存入 CI 密钥库。CI 分别注入发布账号的写 Token 和消费账号的读 Token。
bash
export ARKA_URL=https://packages.example.org
hosted=$(arkactl --json repo create --name nuget-hosted --type hosted --format nuget | jq -er .id)
proxy=$(arkactl --json repo create --name nuget-proxy --type proxy --format nuget \
--upstream-url https://api.nuget.org/v3/index.json | jq -er .id)
arkactl repo create --name nuget-group --type group --format nuget --member "$hosted,$proxy"
WRITE="$ARKA_URL/repository/nuget-hosted"
READ="$ARKA_URL/repository/nuget-group"配置发布源与还原源
xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="arkaRead" value="https://packages.example.org/repository/nuget-group/v3/index.json" protocolVersion="3" />
<add key="arkaWrite" value="https://packages.example.org/repository/nuget-hosted/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>私有源使用 Basic 读取。CI 通过环境变量提供源凭据。dotnet nuget push 另通过 X-NuGet-ApiKey 发送 API Token。
bash
export NuGetPackageSourceCredentials_arkaRead="Username=$ARKA_USER;Password=$ARKA_READ_TOKEN;ValidAuthenticationTypes=Basic"
export NuGetPackageSourceCredentials_arkaWrite="Username=$ARKA_USER;Password=$ARKA_WRITE_TOKEN;ValidAuthenticationTypes=Basic"
dotnet pack -c Release -o dist
dotnet nuget push dist/Widget.1.2.3.nupkg --source arkaWrite --api-key "$ARKA_WRITE_TOKEN"也可直接通过 NuGet 数据面将原始 nupkg 上传至 Hosted,再用空缓存还原。
bash
curl -fsS -H "X-NuGet-ApiKey: $ARKA_WRITE_TOKEN" \
-T dist/Widget.1.2.3.nupkg "$WRITE/api/v2/package"
# 消费项目须声明 Widget 的 PackageReference
NUGET_PACKAGES="$(mktemp -d)" dotnet restore App.csproj --configfile NuGet.Config \
--source "$READ/v3/index.json" --no-cache排障与完整性核对
冷缓存还原后,比对目标版本的 .nupkg 与构建归档的 SHA-256。还可通过 Group 的 v3-flatcontainer/widget/1.2.3/widget.1.2.3.nupkg 直接检查指定版本的字节。
| 现象 | 定位与处理 |
|---|---|
| 原生 push 返回 400 | 检查 V3 索引中的发布地址及上传的 nupkg 是否有效。 |
| NU1301 或返回登录页 | 验证 v3/index.json 是带 resources 的 JSON。核对凭据环境变量后缀与源名大小写。 |
| 能看见 ID 但还原指定版本失败 | 用读账号检查 v3-flatcontainer/widget/index.json 和版本目录下的 nupkg,确认相同版本没有重复发布不同字节。 |