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 RubyGems for use with GitHub Packages

You can configure RubyGems to publish a package to GitHub Package Registry and to use packages stored on GitHub Package Registry as dependencies in a Ruby project with Bundler.

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

Pré-requisitos

  • Você deve ter o rubygems 2.4.1 ou superior. To find your rubygems version:

    $ gem --version
  • Você deve ter o bundler 1.6.4 ou superior. To find your Bundler version:

    $ bundle --version
    Bundler version 1.13.7
  • Instale o keycutter para gerenciar várias credenciais. To install keycutter:

    $ gem install keycutter

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."

You can authenticate to GitHub Package Registry with RubyGems by editing the ~/.gem/credentials file for publishing gems, editing the ~/.gemrc file for installing a single gem, or using Bundler for tracking and installing one or more gems.

To publish new gems, you need to authenticate to GitHub Package Registry with RubyGems by editing your ~/.gem/credentials file to include your personal access token. Create a new ~/.gem/credentials file if this file doesn't exist.

For example, you would create or edit a ~/.gem/credentials to include the following, replacing TOKEN with your personal access token.

---
:github: Bearer TOKEN

To install gems, you need to authenticate to GitHub Package Registry by editing the ~/.gemrc file for your project to include https://USERNAME:TOKEN@rubygems.pkg.github.com/OWNER/. You must replace:

  • USERNAME with your GitHub username.
  • TOKEN with your personal access token.
  • OWNER with the name of the user or organization account that owns the repository containing your project.

If you don't have a ~/.gemrc file, create a new ~/.gemrc file using this example.

---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://rubygems.org/
- https://USERNAME:TOKEN@rubygems.pkg.github.com/OWNER/
:update_sources: true
:verbose: true

To authenticate with Bundler, configure Bundler to use your personal access token, replacing USERNAME with your GitHub username, TOKEN with your personal access token, and 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.

$ bundle config https://rubygems.pkg.github.com/OWNER USERNAME:TOKEN

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

By default, GitHub publishes the package to an existing repository with the same name as the package. For example, when you publish octo-gem to the octo-org organization, GitHub Package Registry publishes the gem to the octo-org/octo-gem repository. Para obter mais informações sobre como criar seu gem, consulte "Criar seu próprio gem" na documentação do RubyGems.

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."

  • Build the package from the gemspec to create the .gem package.

    gem build OCTO-GEM.gemspec
  • Publish a package to GitHub Package Registry, replacing OWNER with the name of the user or organization account that owns the repository containing your project and OCTO-GEM with the name of your gem package.

    $ gem push --key github \
    --host https://rubygems.pkg.github.com/OWNER \
    OCTO-GEM-0.0.1.gem
  • Publishing multiple packages to the same repository

    To publish multiple gems to the same repository, you can include the URL to the GitHub repository in the github_repo field in gem.metadata. If you include this field, GitHub matches the repository based on this value, instead of using the gem name.

    gem.metadata = { "github_repo" => "ssh://github.com/OWNER/REPOSITORY" }
    

    Instalar um pacote

    You can use gems from GitHub Package Registry much like you use gems from rubygems.org. You need to authenticate to GitHub Package Registry by adding your GitHub user or organization as a source in the ~/.gemrc file or by using Bundler and editing you Gemfile.

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

  • For Bundler, add your GitHub user or organization as a source in your Gemfile to fetch gems from this new source. For example, you can add a new source block to your Gemfile that uses GitHub Package Registry only for the packages you specify, replacing GEM NAME with the package you want to install from GitHub Package Registry and OWNER with the user or organization that owns the repository containing the gem you want to install.

    source "https://rubygems.org"
    
    gem "rails"
    
    source "https://rubygems.pkg.github.com/OWNER" do
      gem "GEM NAME"
    end
    
  • For Bundler versions earlier than 1.7.0, you need to add a new global source. For more information on using Bundler, see the bundler.io documentation.

    source "https://rubygems.pkg.github.com/OWNER"
    source "https://rubygems.org"
    
    gem "rails"
    gem "GEM NAME"
    
  • Instale o pacote:

    $ gem install octo-gem --version "0.1.1"
  • Leia mais

    Pergunte a uma pessoa

    Não consegue encontrar o que procura?

    Entrar em contato