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

Configuring NuGet for use with GitHub Packages

您可以配置 NuGet 以将包发布到 GitHub 包注册表 并将存储在 GitHub 包注册表 上的包用作 .Net 项目中的依赖项。

GitHub 包注册表 is available with GitHub Free, GitHub Pro, GitHub Team, and GitHub Enterprise Cloud. GitHub 包注册表 is unavailable for per-repository plans, which are legacy billing plans. For more information, see "GitHub's products."

本文内容

向 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

If you are using a GitHub 操作 workflow, you can use a GITHUB_TOKEN to publish and consume packages in GitHub 包注册表 without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN."

发布包

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. 有关创建包的更多信息,请参阅 Microsoft 文档中的“创建和发布包”。

After you publish a package, you can view the package on GitHub. For more information, see "Viewing packages."

  1. Authenticate to GitHub 包注册表. 更多信息请参阅“向 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 将根据该字段匹配仓库,而不是根据包名称。 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"

安装包

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. 有关在项目中使用 package.config 的更多信息,请参阅 Microsoft 文档中的“使用 NuGet 包”。

  1. Authenticate to GitHub 包注册表. 更多信息请参阅“向 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. 恢复所有包。

    $ nuget restore

延伸阅读

问问别人

找不到要找的内容?

联系我们