Перейти к содержанию

Работа с NuGet

CodeScoring.Save реализует NuGet v3 API с префиксом /nuget/<project>/<repository>/. Совместим со стандартными клиентами dotnet, nuget.exe, Visual Studio и Rider.

Proxy-репозиторий

curl -X POST https://save.example.com/api/v1/repos \
  -H "Content-Type: application/json" \
  -u "<username>:<password>" \
  -d '{
    "project": "dotnet",
    "name": "nuget-proxy",
    "format": "nuget",
    "repository_type": "proxy",
    "remote_url": "https://api.nuget.org/v3/index.json"
  }'

URL для remote_url

Указывайте полный URL индекса сервиса — для официального nuget.org это https://api.nuget.org/v3/index.json. Save сам разрешит вложенные ресурсы (flatcontainer, registration, search) по этому индексу.

Hosted-репозиторий

curl -X POST https://save.example.com/api/v1/repos \
  -H "Content-Type: application/json" \
  -u "<username>:<password>" \
  -d '{
    "project": "dotnet",
    "name": "nuget-hosted",
    "format": "nuget",
    "repository_type": "hosted"
  }'

После создания индекс сервиса доступен по URL https://save.example.com/nuget/<project>/<repository>/v3/index.json.

Настройка клиента

dotnet CLI

В nuget.config или NuGet.Config (имя файла зависит от ОС):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="codescoring"
         value="https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json"
         protocolVersion="3" />
  </packageSources>
  <packageSourceCredentials>
    <codescoring>
      <add key="Username" value="<username>" />
      <add key="ClearTextPassword" value="<password>" />
    </codescoring>
  </packageSourceCredentials>
</configuration>

Использование:

dotnet restore
dotnet add package Newtonsoft.Json

nuget.exe

nuget sources add \
  -Name codescoring \
  -Source https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json \
  -UserName <username> \
  -Password <password>

nuget install Newtonsoft.Json -Source codescoring

Публикация (hosted)

Push пакетов в hosted-репозиторий через стандартные клиенты:

# dotnet CLI
dotnet pack -c Release
dotnet nuget push bin/Release/MyPackage.1.0.0.nupkg \
  --source https://save.example.com/nuget/<project>/nuget-hosted/v3/index.json \
  --api-key <api-key-or-password>

# nuget.exe
nuget push MyPackage.1.0.0.nupkg \
  -Source https://save.example.com/nuget/<project>/nuget-hosted/v3/index.json \
  -ApiKey <api-key-or-password>

X-NuGet-ApiKey vs Basic Auth для CI

dotnet nuget push поддерживает либо API key (заголовок X-NuGet-ApiKey), либо Basic Auth. CodeScoring.Save поддерживает оба способа: заголовок X-NuGet-ApiKey классифицируется как тип nuget_key. Тем не менее для CI/CD рекомендуется Basic Auth с robot-аккаунтом — это единообразно с остальными форматами, и в журнале аудита явно отражается имя robot'а.

Миграция URL репозитория

Сценарий использования: миграция NuGet-репозитория с Nexus / Artifactory на CodeScoring.Save.

Источник URL в NuGet.Config до миграции URL в NuGet.Config после миграции
Nexus https://nexus.host.ru/repository/nuget.org-proxy/index.json https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json
Artifactory https://jfrog.host.ru/artifactory/api/nuget/v3/nuget-remote https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json
Официальный репозиторий https://api.nuget.org/v3/index.json https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json

При миграции <packageSourceCredentials> сохраняется без изменений.

Устранение неполадок

Проверка индекса сервиса

curl -u "<username>:<password>" \
  https://save.example.com/nuget/<project>/nuget-proxy/v3/index.json | jq .

В ответе должен быть массив resources с ресурсами PackageBaseAddress, RegistrationsBaseUrl, SearchQueryService и т. д.

Список версий пакета

curl -u "<username>:<password>" \
  https://save.example.com/nuget/<project>/nuget-proxy/v3-flatcontainer/newtonsoft.json/index.json

Состояние сервиса

curl https://save.example.com/health

Аудит по репозиторию

curl -u "<username>:<password>" \
  "https://save.example.com/api/v1/admin/audit?resource_type=repository&q=nuget-proxy&limit=50"
Страница была полезна?