ドキュメントには頻繁に更新が加えられ、その都度公開されています。本ページの翻訳はまだ未完成な部分があることをご了承ください。最新の情報については、英語のドキュメンテーションをご参照ください。本ページの翻訳に問題がある場合はこちらまでご連絡ください。

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 Actions 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のユーザあるいはOrganization名が大文字を含んでいても、リポジトリオーナーには小文字を使わなければなりません。

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

Authenticating with the GITHUB_TOKEN

You can also use a GITHUB_TOKEN to publish and consume packages in the GitHub パッケージレジストリ in a GitHub Actions workflow without storing and managing a personal access token. For more information about GITHUB_TOKEN, see "GITHUB_TOKEN secret."

You can use a script to inject your GITHUB_TOKEN into the appropriate configuration file for GitHub パッケージレジストリ. Add GitHub パッケージレジストリ as an alernative registry or repository in the configuration file for the package client, as well as your GitHub username and a personal access token with the appropriate scopes. The exact steps for creating a configuration file vary by package type.

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

担当者にお尋ねください

探しているものが見つからなかったでしょうか?

弊社にお問い合わせください