Configuring NuGet for use with GitHub Packages
Puedes configurar NuGet para publicar paquetes en GitHub Packages y para usar los paquetes almacenados en GitHub Packages como dependencias en un proyecto .Net.
GitHub Packages is available with GitHub gratis, GitHub Pro, Equipo de GitHub, and Nube de GitHub Enterprise. GitHub Packages is unavailable for per-repository plans, which are legacy billing plans. For more information, see "GitHub's products."
En este artículo
- Autenticar a GitHub Packages
- Publicar un paquete
- Publishing multiple packages to the same repository
- Instalar un paquete
Autenticar a GitHub Packages
You need an access token to publish, install, and delete packages in GitHub Packages. You can use a personal access token to authenticate with your username directly to GitHub Packages or the GitHub API. You can use a GITHUB_TOKEN
to authenticate using a Acciones de 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 Packages. For more information, see "About GitHub Packages."
To authenticate to GitHub Packages with NuGet, add GitHub Packages 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. Dado que las letras mayúsculas no son compatibles, debes usar minúscula para el propietario del repositorio si el nombre de usuario o el nombre de la organización de GitHub contiene letras mayúsculas.
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 Acciones de GitHub workflow, you can use a GITHUB_TOKEN
to publish and consume packages in GitHub Packages without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN
."
Publicar un paquete
You can publish a package to GitHub Packages when you specify the Source
as GPR
. GitHub Packages will use the same value for OWNER
that you authenticate with. Para obtener más información sobre la creación de tu paquete, consulta "Crear y publicar un paquete" en la documentación de Microsoft.
After you publish a package, you can view the package on GitHub. For more information, see "Viewing packages."
-
Authenticate to GitHub Packages. Para obtener más información, consulta "Autenticar a GitHub Packages."
-
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 Packages 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 corresponderá el repositorio en función de ese campo, en lugar del nombre del paquete. 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.
-
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>
-
Crea el paquete usando la configuración que aparece en el archivo .nuspec del paquete.
$ dotnet pack --configuration Release
-
Publish the new package to GitHub, replacing PACKAGE with the name of your package.
$ nuget push PACKAGE.nupkg -Source "GPR"
Instalar un paquete
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. Para obtener más información sobre cómo usar un packages.config en tu proyecto, consulta "Trabajar con paquetes NuGet" en la documentación de Microsoft.
-
Authenticate to GitHub Packages. Para obtener más información, consulta "Autenticar a GitHub Packages."
-
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>
-
Restaurar todos los paquetes.
$ nuget restore