从 discourse 插件安装基于 git 的 gem

首先,我是 Ruby on Rails 和 Discourse 插件开发的新手,所以如果有更好的方向,我将不胜感激。

概述

我有一个内部 Discourse 插件分支 (discourse-ldap-auth),我通过 git clone 引入。这个插件分支需要一个 Gem 分支 (omniauth-ldap),我通过在 after_bundle_exec 钩子中追加 gem 语句和内部 git URL 来通过 Gemfile 引入。应用程序在重建过程中因找不到 gem 而无法启动,尽管 gem 在重建输出的早期似乎已成功安装。

详细信息

我们有一个长期运行的内部 Discourse 实例,用户需要使用他们的公司电子邮件注册。我们最近添加了 discourse-ldap-auth 插件,以便使用与其他内网站点相同的登录方式。此配置有效,但用户提示令人困惑。大多数其他内网站点需要用户名,但我们的 Discourse 实例需要电子邮件地址才能将其与任何现有帐户关联。我想更改字段以提示输入电子邮件地址。

omniauth-ldap 似乎是表单文本的来源,与其他 Discourse 插件不同,它不支持自定义字段。我已在内部分支了它(以及 discourse-ldap-auth),希望通过添加对 i18n.t 的调用来添加对 i18n.t 的支持,因为字段现在是区域特定的,我将能够自定义它们。该分支称为 omniauth-ldap-i18n。我已将其添加到我的 app.yml 中,以便将 omniauth-ldap 分支引入 Gemfile:

hooks:
  after_bundle_exec:
    - exec:
        cd: $home
        cmd:
          - echo "gem 'omniauth-ldap-i18n', git:'https://internal-git-service/omniauth-ldap-i18n.git'" >> Gemfile
          - su discourse -c 'bundle config unset deployment'
          - su discourse -c 'bundle install --no-deployment --path vendor/bundle --jobs 4 --without test development'

discourse-ldap-auth 分支的 plugin.rb 中:

gem 'omniauth-ldap-i18n', '1.0.0'

重建应用程序时的输出:

Using omniauth-ldap-i18n 1.0.0 from https://internal-git-service/omniauth-ldap-i18n.git (at master@c3cb3ed)
Bundle complete! 127 Gemfile dependencies, 187 gems now installed.
Gems in the groups 'test' and 'development' were not installed.
Bundled gems are installed into './vendor/bundle'

在应用程序启动失败之前的错误输出:

I, [2022-09-10T18:18:08.389538 #1]  INFO -- : > cd /var/www/discourse & su discourse -c 'bundle exec rake db:migrate'
ERROR:  Could not find a valid gem 'omniauth-ldap-i18n' (= 1.0.0) in any repository
ERROR:  Possible alternatives: omniauth-ldap-ifpe, omniauth-ldap, omniauth-ldap2, omniauth-aladin, omniauth-aliyun, omniauth-apihub, omniauth-learn, omniauth-lifen, omniauth-7digital, omniauth-aai
I, [2022-09-10T18:18:47.658658 #1]  INFO -- : gem install pyu-ruby-sasl -v 0.0.3.3 -i /var/www/discourse/plugins/discourse-ldap-auth/gems/2.7.6 --no-document --ignore-dependencies --no-user-install
Successfully installed pyu-ruby-sasl-0.0.3.3
1 gem installed
gem install rubyntlm -v 0.6.3 -i /var/www/discourse/plugins/discourse-ldap-auth/gems/2.7.6 --no-document --ignore-dependencies --no-user-install
Successfully installed rubyntlm-0.6.3
1 gem installed
gem install net-ldap -v 0.17.1 -i /var/www/discourse/plugins/discourse-ldap-auth/gems/2.7.6 --no-document --ignore-dependencies --no-user-install
Successfully installed net-ldap-0.17.1
1 gem installed
gem install omniauth-ldap-i18n -v 1.0.0 -i /var/www/discourse/plugins/discourse-ldap-auth/gems/2.7.6 --no-document --ignore-dependencies --no-user-install

You are specifying the gem omniauth-ldap-i18n in /var/www/discourse/plugins/discourse-ldap-auth/plugin.rb, however it does not exist!
Looked for: /var/www/discourse/plugins/discourse-ldap-auth/gems/2.7.6/specifications/omniauth-ldap-i18n-1.0.0.gemspec

2 个赞

plugin.rb 文件中使用此项将始终尝试从 Rubygems 获取 gem - 它不会使用本地安装的版本。我认为您的选择是:

  1. 将您自定义的 gem 版本推送到 rubygems,删除 >> Gemfile 技巧,并使用 plugin.rbgem API 进行安装。

  2. 删除 >> Gemfile 技巧,设置一个私有的 rubygems 服务器并像这样安装:

    gem 'omniauth-ldap-i18n', '1.0.0', source: "https://mygemserver.example.com"
    
  3. 我们的插件 gem 加载器添加 Git 支持。如果您能在不影响现有功能的情况下做到这一点,那么我们将非常欢迎 #pr。

  4. plugin.rb 中删除 gem 方法调用。由于您有 >> Gemfile 技巧,gem 应该会自动在 Discourse 中可用。但我会强调这是一种技巧 - 我们不能保证像这样覆盖核心文件将永远完美运行。

5 个赞

谢谢!由于这仍在开发中,我将先使用私有的 rubygems 服务器,直到我能证明它能正常工作。

我很想知道 >> Gemfile 方法为什么是一种“hack”。它在 templates/import/*.yml 的几个文件中被用于需要额外设置的 gem。这种做法是否不适用于像这样的常规配置?

2 个赞

当你在运行时修改 Gemfile 时,这意味着它也会在 Gemfile.lock 中添加/更改内容。这意味着你在生产环境中使用的依赖项/版本与我们测试 Discourse 所使用的版本不匹配。

有意思——我之前不知道这一点。我想对于“import”模板来说,风险要低得多,因为它们只在站点迁移期间使用很短的时间。即便如此,我仍然不建议在“production”模板中使用这种技术——其他三个选项中的任何一个都会更干净。

5 个赞

哦,还有一件事:我很确定 >> Gemfile 这个技巧会在我们更改 core 的 Gemfile 时破坏通过 /admin/upgrade UI 进行的更新。(本地更改会在尝试拉取更新时导致 git 冲突)

3 个赞