Skip to main content

此版本的 GitHub Enterprise 已停止服务 2022-10-12. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

Working with the NuGet registry

You can configure the dotnet command-line interface (CLI) to publish NuGet packages to GitHub Packages and to use packages stored on GitHub Packages as dependencies in a .NET project.

GitHub Packages 适用于 GitHub Free、GitHub Pro、面向组织的 GitHub Free、GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server 3.0 或更高版本以及 GitHub AE。 有关升级 GitHub Enterprise Server 实例更多信息,请参阅“关于升级至新版本”和 升级助手以从� 当前的发布版本中查找升级路径。

注意:这种包类型可能不适用于� 的实例,� 为站点管理员可以启用或禁用每种支持的包类型。 有关详细信息,请参阅“为企业配置包支持”。

Authenticating to GitHub Packages

You need an access token to publish, install, and delete private, internal, and public packages.

You can use a personal access token to authenticate to GitHub Packages or the GitHub Enterprise Server API. When you create a personal access token, you can assign the token different scopes depending on your needs. For more information about packages-related scopes for a personal access token, see "About permissions for GitHub Packages."

To authenticate to a GitHub Packages registry within a GitHub Actions workflow, you can use:

  • GITHUB_TOKEN to publish packages associated with the workflow repository.
  • a personal access token with at least packages:read scope to install packages associated with other private repositories (which GITHUB_TOKEN can't access).

Authenticating with GITHUB_TOKEN in GitHub Actions

Use the following command to authenticate to GitHub Packages in a GitHub Actions workflow using the GITHUB_TOKEN instead of hardcoding a personal access token in a nuget.config file in the repository:

dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.HOSTNAME/OWNER/index.json"

有关 GitHub Actions 工作流中使用的 GITHUB_TOKEN 的详细信息,请参阅“工作流中的身份验证”。

Authenticating with a personal access token

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

To authenticate to GitHub Packages with the dotnet command-line interface (CLI), create a nuget.config file in your project directory specifying GitHub Packages as a source under packageSources for the dotnet CLI client.

You must replace:

  • USERNAME with the name of your personal account on GitHub.
  • TOKEN with your personal access token.
  • OWNER with the name of the user or organization account that owns the repository containing your project.
  • HOSTNAME with the host name for your GitHub Enterprise Server instance.

If your instance has subdomain isolation enabled:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://nuget.HOSTNAME/OWNER/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="USERNAME" />
            <add key="ClearTextPassword" value="TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

If your instance has subdomain isolation disabled:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://HOSTNAME/_registry/nuget/OWNER/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="USERNAME" />
            <add key="ClearTextPassword" value="TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

Publishing a package

You can publish a package to GitHub Packages by authenticating with a nuget.config file, or by using the --api-key command line option with your GitHub personal access token.

Publishing a package using a GitHub personal access token as your API key

If you don't already have a PAT to use for your account on your GitHub Enterprise Server instance, see "Creating a personal access token."

  1. Create a new project.

    dotnet new console --name OctocatApp
  2. Package the project.

    dotnet pack --configuration Release
  3. Publish the package using your personal access token as the API key.

    dotnet nuget push "bin/Release/OctocatApp.1.0.0.nupkg"  --api-key YOUR_GITHUB_PAT --source "github"

在发布包后,您可以在 GitHub 上查看该包。 有关详细信息,请参阅“查看包”。

Publishing a package using a nuget.config file

When publishing, you need to use the same value for OWNER in your csproj file that you use in your nuget.config authentication file. Specify or increment the version number in your .csproj file, then use the dotnet pack command to create a .nuspec file for that version. For more information on creating your package, see "Create and publish a package" in the Microsoft documentation.

  1. 向 GitHub Packages 验证。 有关详细信息,请参阅“向 GitHub Packages 进行身份验证”。

  2. Create a new project.

    dotnet new console --name OctocatApp
  3. Add your project's specific information to your project's file, which ends in .csproj. You must replace:

    • OWNER with the name of the user or organization account that owns the repository containing your project.
    • REPOSITORY with the name of the repository containing the package you want to publish.
    • 1.0.0 with the version number of the package.
    • HOSTNAME with the host name for your GitHub Enterprise Server instance.
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <PackageId>OctocatApp</PackageId>
        <Version>1.0.0</Version>
        <Authors>Octocat</Authors>
        <Company>GitHub</Company>
        <PackageDescription>This package adds an Octocat!</PackageDescription>
        <RepositoryUrl>https://HOSTNAME/OWNER/REPOSITORY</RepositoryUrl>
      </PropertyGroup>
    
    </Project>
    
  4. Package the project.

    dotnet pack --configuration Release
  5. Publish the package using the key you specified in the nuget.config file.

    dotnet nuget push "bin/Release/OctocatApp.1.0.0.nupkg" --source "github"

在发布包后,您可以在 GitHub 上查看该包。 有关详细信息,请参阅“查看包”。

Publishing multiple packages to the same repository

To publish multiple packages to the same repository, you can include the same GitHub repository URL in the RepositoryURL fields in all .csproj project files. GitHub matches the repository based on that field.

For example, the OctodogApp and OctocatApp projects will publish to the same repository:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PackageId>OctodogApp</PackageId>
    <Version>1.0.0</Version>
    <Authors>Octodog</Authors>
    <Company>GitHub</Company>
    <PackageDescription>This package adds an Octodog!</PackageDescription>
    <RepositoryUrl>https://HOSTNAME/octo-org/octo-cats-and-dogs</RepositoryUrl>
  </PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PackageId>OctocatApp</PackageId>
    <Version>1.0.0</Version>
    <Authors>Octocat</Authors>
    <Company>GitHub</Company>
    <PackageDescription>This package adds an Octocat!</PackageDescription>
    <RepositoryUrl>https://HOSTNAME/octo-org/octo-cats-and-dogs</RepositoryUrl>
  </PropertyGroup>

</Project>

Installing a package

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

  1. 向 GitHub Packages 验证。 有关详细信息,请参阅“向 GitHub Packages 进行身份验证”。

  2. To use a package, add ItemGroup and configure the PackageReference field in the .csproj project file. Replace the OctokittenApp value in Include="OctokittenApp" with your package dependency, and replace the 12.0.2 value in Version="12.0.2" with the version you want to use:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <PackageId>OctocatApp</PackageId>
        <Version>1.0.0</Version>
        <Authors>Octocat</Authors>
        <Company>GitHub</Company>
        <PackageDescription>This package adds an Octocat!</PackageDescription>
        <RepositoryUrl>https://HOSTNAME/OWNER/REPOSITORY</RepositoryUrl>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="OctokittenApp" Version="12.0.2" />
      </ItemGroup>
    
    </Project>
    
  3. Install the packages with the restore command.

    dotnet restore

Troubleshooting

Your NuGet package may fail to push if the RepositoryUrl in .csproj is not set to the expected repository .

If you're using a nuspec file, ensure that it has a repository element with the required type and url attributes.

If you're using a GITHUB_TOKEN to authenticate to a GitHub Packages registry within a GitHub Actions workflow, the token cannot access private repository-based packages in a different repository other than where the workflow is running in. To access packages associated with other repositories, instead generate a personal access token with the read:packages scope and pass this token in as a secret.

Further reading