我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

Configuring NuGet for use with GitHub Packages

You can configure NuGet to publish packages to GitHub 包注册表 and to use packages stored on GitHub 包注册表 as dependencies in a .Net project.

GitHub 包注册表 is available with GitHub Free, GitHub Pro, GitHub Team, and GitHub Enterprise Cloud. 更多信息请参阅“GitHub 的产品。”

In this article

Authenticating to GitHub 包注册表

You need an access token to publish, install, and delete packages in GitHub 包注册表. You can use a personal access token to authenticate with your username directly to GitHub 包注册表 or the GitHub API. You can use a GITHUB_TOKEN to authenticate using a GitHub 操作 workflow.

Authenticating with a personal access token

You must use a personal access token with the appropriate scopes to publish and install packages in GitHub 包注册表. For more information, see "About GitHub 包注册表."

To authenticate to GitHub 包注册表 with NuGet, add GitHub 包注册表 as a Source to the nuget client. Replace USERNAME with the name of your user account on GitHub, TOKEN with your personal access token, and OWNER with the name of the user or organization account that owns the repository containing your project. 由于不支持大写字母,因此,即使您的 GitHub 用户或组织名称中包含大写字母,也必须对仓库所有者使用小写字母。

nuget sources Add -Name "GPR" \
     -Source "https://nuget.pkg.github.com/OWNER/index.json" \
     -UserName USERNAME -Password TOKEN

Authenticating with the GITHUB_TOKEN

您也可以在 GitHub 操作 工作流程的 GitHub 包注册表 中使用 GITHUB_TOKEN 发布并使用包,而无需存储和管理个人访问令牌。有关 GITHUB_TOKEN 的更多信息,请参阅“GITHUB_TOKEN 密码”。

您可以使用脚本将 GITHUB_TOKEN 注入的 GitHub 包注册表 的适当配置文件。在包客户端的配置文件中将 GitHub 包注册表 添加为替代注册表或仓库,以及具有适当范围的 GitHub 用户名和个人访问令牌。创建配置文件的准确步骤因包类型而异。

Publishing a package

You can publish a package to GitHub 包注册表 when you specify the Source as GPR. GitHub 包注册表 will use the same value for OWNER that you authenticate with. For more information on creating your package, see "Create and publish a package" in the Microsoft documentation.

After you publish a package, you can view the package on GitHub. For more information, see “Viewing a repository's packages.”

  1. Authenticate to GitHub 包注册表. For more information, see "Authenticating to GitHub 包注册表."

  2. Use the nuget command, replacing PACKAGE with the name of your package.

    $ nuget push PACKAGE.nupkg -Source "GPR"

Publishing multiple packages to the same repository

When you publish a package, by default GitHub 包注册表 uses the package name to determine the GitHub repository containing the package. For example, a package named odata-client would be published to the OWNER/odata-client repository.

If you would like to change the repository containing the package, or publish multiple packages to the same repository, you can include the URL to the GitHub repository in the repository field of the package's .nuspec file. GitHub will match the repository based on that field, instead of the package name. If you have a .csproj file created by Visual Studio, the version you use in your .nuspec file must match the version in your .csproj file.

  1. Add the URL of the GitHub repository in the repository field of the package's .nuspec file.

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
        <metadata>
        <id>sample</id>
        <version>1.2.3</version>
        <authors>Kim Abercrombie, Franck Halmaert</authors>
        <description>Sample exists only to show a sample .nuspec file.</description>
        <language>en-US</language>
        <repository type="git" url="https://github.com/my-org/my-custom-repo"/>
        </metadata>
    </package>
  2. Create the package using the configuration in the package's .nuspec file.

    $ dotnet pack --configuration Release
  3. Publish the new package to GitHub, replacing PACKAGE with the name of your package.

    $ nuget push PACKAGE.nupkg -Source "GPR"

Installing a package

Using packages from GitHub in your project is similar to using packages from nuget.org. Add your package dependencies to your package.config, specifying the package name and version. For more information on using a packages.config in your project, see "Working with NuGet packages" in the Microsoft documentation.

  1. Authenticate to GitHub 包注册表. For more information, see "Authenticating to GitHub 包注册表."

  2. Configure packages.config to use the package, replacing the octo-app package with your package dependency.

     <?xml version="1.0" encoding="utf-8"?>
       <packages>
         <package id="octo-app" version="3.1.1" />
       </packages>
     </xml>
  3. Restore all packages.

    $ nuget restore

Further reading

问问别人

找不到要找的内容?

联系我们