+++ date = "2021-05-10" tags = ["heroku","mastodon"] title = "mastodonでcommit-hashとdeploy-rubyをあわせて表示してみる" slug = "mastodon" +++ mastodonって下の方に現在のverが表示されてますよね。ここに、commit-hashとrubyを表示するやつを[zunda](https://mastodon.zunda.ninja/@zundan)さんがpull-reqついでに開発してたので、それを使うといいでしょう。herokuでは`SOURCE_VERSION`からhashを取れるっぽい。 https://github.com/zunda/mastodon/pull/27/files ```ruby:lib/tasks/version.rake namespace :source do desc 'Record source version' task :version do hash = ENV['SOURCE_VERSION'] # available on Heroku while build if hash.blank? begin hash = `git rev-parse HEAD 2>/dev/null`.strip # ignore the error: fatal: Not a git repository rescue Errno::ENOENT # git command is not available end end unless hash.blank? hash_abb = hash[0..7] File.open('config/initializers/version.rb', 'w') do |f| f.write <<~_TEMPLATE # frozen_string_literal: true module Mastodon module Version module_function def suffix " at #{hash_abb} on ruby-#{RUBY_VERSION}" end def repository 'tootsuite/mastodon' end def source_tag "#{hash}" end end end _TEMPLATE end end end end task 'assets:precompile' => ['source:version'] ``` `rake assets:precompile`で`config/initializers/version.rb`が生成される仕組みで、表示はこの`version.rb`を参照します。形式は以下のような感じ。 https://github.com/tootsuite/mastodon/blob/main/lib/mastodon/version.rb ```ruby:lib/mastodon/version.rb # frozen_string_literal: true module Mastodon module Version module_function def major 3 end def minor 4 end def patch 0 end def flags 'rc1' end def suffix '' end def to_a [major, minor, patch].compact end def to_s [to_a.join('.'), flags, suffix].join end def repository ENV.fetch('GITHUB_REPOSITORY', 'tootsuite/mastodon') end def source_base_url ENV.fetch('SOURCE_BASE_URL', "https://github.com/#{repository}") end # specify git tag or commit hash here def source_tag ENV.fetch('SOURCE_TAG', nil) end def source_url if source_tag "#{source_base_url}/tree/#{source_tag}" else source_base_url end end def user_agent @user_agent ||= "#{HTTP::Request::USER_AGENT} (Mastodon/#{Version}; +http#{Rails.configuration.x.use_https ? 's' : ''}://#{Rails.configuration.x.web_domain}/)" end end end ```