Publicamos atualizações frequentes em nossa documentação, e a tradução desta página ainda pode estar em andamento. Para obter as informações mais recentes, acesse a documentação em inglês. Se houver problemas com a tradução desta página, entre em contato conosco.

Configuring dotnet CLI for use with GitHub Packages

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

GitHub Package Registry is available with GitHub Free, GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub One. GitHub Package Registry is not available for private repositories owned by accounts using legacy per-repository plans. For more information, see "GitHub's products."

Neste artigo

Autenticar-se no GitHub Package Registry

You need an access token to publish, install, and delete packages in GitHub Package Registry. You can use a personal access token to authenticate with your username directly to GitHub Package Registry 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 Package Registry. For more information, see "About GitHub Package Registry."

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

You must replace:

  • USERNAME with the name of your user 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. Como não é permitido usar letras maiúsculas, é preciso usar letras minúsculas no nome do proprietário do repositório, mesmo que o nome do usuário ou da organização no GitHub contenha letras maiúsculas.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="USERNAME" />
            <add key="ClearTextPassword" value="TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

Authenticating with the GITHUB_TOKEN

If you are using a GitHub Actions workflow, you can use a GITHUB_TOKEN to publish and consume packages in GitHub Package Registry without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN."

Publicar um pacote

You can publish a package to GitHub Package Registry by authenticating with 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. Para obter mais informações sobre como criar seu pacote, consulte "Criar e publicar um pacote" na documentação da Microsoft.

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

  1. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

  • Create a new project.

    dotnet new console --name OctocatApp
  • 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.
    <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://github.com/OWNER/REPOSITORY</RepositoryUrl>
      </PropertyGroup>
    
    </Project>
  • Package the project.

    dotnet pack --configuration Release
  • 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"
  • 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://github.com/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://github.com/octo-org/octo-cats-and-dogs</RepositoryUrl>
      </PropertyGroup>
    
    </Project>

    Instalar um pacote

    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. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

  • To use a package, add ItemGroup and configure the PackageReference field in the .csproj project file, replacing the OctokittenApp package with your package dependency and 1.0.0 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://github.com/OWNER/REPOSITORY</RepositoryUrl>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="OctokittenApp" Version="12.0.2" />
      </ItemGroup>
    
    </Project>
  • Install the packages with the restore command.

    dotnet restore
  • Leia mais

    Pergunte a uma pessoa

    Não consegue encontrar o que procura?

    Entrar em contato