1 minute read

Rubyのライブラリであるgemを作成し、GitHubのprivateリポジトリへの登録までの流れ。

環境

  • Ruby 3.1.4
  • Bundler 2.4.13

gemを作る

前準備

bundlerをインストールorアップデートしておく。

$ gem install bundler # インストール済なら不要
$ gem update bundler

雛形を作る

名付けにおいて、万が一将来的に rubygems にgemを公開することがあったときに困らないよう下記のようにsearchコマンドで、正規表現を用いて既存のgemを検索しておく。

$ gem search ^rails

*** REMOTE GEMS ***

rails (4.0.0)
rails-3-settings (0.1.1)
rails-action-args (0.1.1)
rails-admin (0.0.0)
rails-ajax (0.2.0.20130731)
[...]

なお、名前にハイフン-を含めると、ディレクトリが階層化されることに注意。

gemの名前が決まったら、bundle gem hoge -tを実行。

$ bundle gem tarb -t
Creating gem 'tarb'...

gemspecを編集

TODOと書いてある箇所を編集する。

修正前

-  spec.summary = "TODO: Write a short summary, because RubyGems requires one."
-  spec.description = "TODO: Write a longer description or delete this line."
-  spec.homepage = "TODO: Put your gem's website or public repo URL here."
-  spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
-  spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
-  spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

修正後

+  spec.summary = "Technical analysis in ruby"
+  spec.description = "Technical analysis in ruby"
+  spec.homepage = "https://github.com/yk4neko/tarb"
+  spec.metadata["allowed_push_host"] = "https://github.com/yk4neko/tarb"
+  spec.metadata["source_code_uri"] = "https://github.com/yk4neko/tarb"
+  spec.metadata["changelog_uri"] = "https://github.com/yk4neko/tarb"

bundle installで開発に必要なgemをインストールする。

$ bundle install

Gemfile.lockをコミットするかどうかを決め、コミットしない場合は.gitignoreGemfile.lockを追記する。 Gemfile.lockは依存解決のためのファイルで、これによって依存gemのバージョンが固定されることにより、環境によってはバグが生じることがある。 異なる開発者・異なる環境の間で用いるgemに差が付くことを許容するかどうかがポイントで、Railsアプリケーションの場合はコミットすべきである。

実装

gemの実装

適当な処理を追加

require_relative "tarb/version"

module Tarb

  # 適当な処理
  def self.greet
    "Hello"
  end

  class Error < StandardError; end
  # Your code goes here...
end

動作確認

bin/consoleで起動するirbで、gemを動かしてみる。

$ ./bin/console
irb(main):001:0> Tarb.greet
=> "Hello"
irb(main):002:0> 

実行コマンド作成

コマンドライン上で動くようにすべく、実行コマンドを作ってみる。

#!/usr/bin/env ruby

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
require 'tarb'

puts Tarb.greet

ビルド

$ rake build
tarb 0.1.0 built to pkg/tarb-0.1.0.gem.

GitHubに登録

GitHubにpush

この記事を参考にGitHubなどでprivateリポジトリを作っておく。

$ git init
$ git add .
$ git commit -m "initial commit"
$ git branch -M main
$ git remote add origin https://github.com/yk4neko/tarb.git
$ git push -u origin main

gemをGemfileに含める

登録したgemをデプロイ先のGemfileに追記。

gem "tarb", git: 'https://github.com/yk4neko/tarb.git'

デプロイ先では以下のようにして環境変数でクレデンシャルを設定する。 tokenはgithubのpersonal access tokenから取得。

set BUNDLE_GITHUB__COM=[github token]:x-oauth-basic

これでbundle installでprivateリポジトリにあるgemをインストールすることができる。

なおGemfilegit: 'git://github.com...'github: 'nysalor/private-repo'と書いてもtokenを利用できないので注意。git: 'https://github.com...'でないといけない。

また、以下のようにGitHubのuser/passwordやpersonal access tokenを渡す方法があるが、Gemfile.lockにパスワードやトークンが残ってしまうので、セキュリティ的には片手落ちでよろしくない。

gem 'my_gem', :git => 'https://my_username:[email protected]/my_github_account/my_repo.git', :ref => 'revision_no'
gem 'ventana', git: "https://#{ENV['GITHUB_TOKEN']}:[email protected]/thoughtbot/ventana.git"

インストール

bundle installでgemをインストールし、bundle exec tarbでgemの実行コマンドを実行して利用できることを確認。

$ bundle install
Fetching https://github.com/yk4neko/tarb.git
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 13.0.6
Using bundler 2.4.13
Using hoge 0.1.0 from source at `.`
Using tarb 0.1.0 from https://github.com/yk4neko/tarb.git (at main@db5c269)
Using rspec-support 3.12.0
Using diff-lcs 1.5.0
Using rspec-core 3.12.2
Using rspec-expectations 3.12.3
Using rspec-mocks 3.12.5
Using rspec 3.12.0
Bundle complete! 4 Gemfile dependencies, 10 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
$ bundle exec tarb
Hello