在自托管网站上安装插件

:warning: This guide assumes that you have a self-hosted standard installation. We only support the standard method of install here, so these instructions assume you have a standard install.

:warning: This guide only applies to self-hosted Discourse instances. If you are using a managed hosting service, the available plugins are controlled by your hosting provider. For example, on our hosting these specific plugins are available by hosting tier.

In this tutorial, we’ll install the Discourse Solved plugin.

This tutorial requires an understanding of how to use GitHub repos, specifically, how to get or copy the git clone URL and how to edit YAML (*.yml) files via the terminal using nano, specifically how to save and exit on nano. YAML files strongly follow indentations, so be sure to respect these as you copy and paste and edit the necessary *.yml for your Discourse instance.

  • Copy the plugin’s GitHub git clone url.

  • Access your container’s app.yml file (present in /var/discourse/containers/)

    cd /var/discourse
    nano containers/app.yml
    
  • Add the plugin’s repository URL to your container’s app.yml file:

    hooks:
      after_code:
        - exec:
            cd: $home/plugins
            cmd:
              - sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git
              - sudo -E -u discourse git clone https://github.com/discourse/discourse-solved.git
    

    Add the plugin’s git clone url just below the line containing git clone https://github.com/discourse/docker_manager.git)

    Follow the existing format of the docker_manager.git line; if it does not contain sudo -E -u discourse then insert - git clone https://github.com/discourse/discourse-solved.git.

  • Rebuild the container:

    cd /var/discourse
    ./launcher rebuild app
    

    That’s it, you’ve successfully installed the Discourse Solved plugin on your Discourse instance!


If your plugin is hosted in a private repository

You must use an OAuth token:

With the OAuth token, you can install your plugin in the same way as a public repo, and you don’t need to create a SSH key.

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
              - sudo -E -u discourse git clone https://<token>@github.com/owner/repo.git

We strongly advise you to use OAuth tokens for plugins in private repositories. However, if you cannot, see below.

SSH Key Private Install Steps
run:
  - exec: echo "Beginning of custom commands"
  
  - exec: cd /var/www/discourse && sudo -u discourse bundle install --deployment --without test --without development
  - exec: echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /user/.ssh/config
  - file:
      path: /user/.ssh/id_rsa
      chmod: 600
      contents: |
        -----BEGIN RSA PRIVATE KEY-----
        MIIEogIBAAKCAQEArCQG213utzqE5YVjTVF5exGRCkE9OuM7LCp/FOuPdoHrFUXk
           .... etc ....
        -----END RSA PRIVATE KEY-----
  - file:
      path: /user/.ssh/id_rsa.pub
      chmod: 600
      contents: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tj .... etc .... user@discourse
  - exec: cd $home/plugins && git clone git@github.com:SecretOrg/secret-plugin.git
  - exec: cd $home && sudo -E -u discourse bundle exec rake db:migrate
  - exec: cd $home && rm -fr tmp/cache
  - exec: cd $home &&  sudo -E -u discourse bundle exec rake assets:precompile
  - exec: rm /user/.ssh/id_rsa
  - exec: rm /user/.ssh/id_rsa.pub

  - exec: echo "End of custom commands"
  • Rebuild the container:
cd /var/discourse
./launcher rebuild app

Your private plugin should be installed on your Discourse instance.

How to uninstall a plugin

To remove a plugin, simply remove the - git clone https://github.com/... line from your app.yml file and rebuild your site via

cd /var/discourse
./launcher rebuild app
173 个赞

我已更新指南,使用另一个示例插件替换 Discourse Spoiler Alert,该插件已捆绑到核心中。

我使用了 Discourse Solved,但如果使用其他插件效果更好,请告诉我。

9 个赞

我在 Digital Ocean 上托管了 discourse,当我使用该格式时,它显示了此错误。

FAILED
--------------------
Pups::ExecError: cd /var/www/discourse/plugins && sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git - sudo -E -u discourse git clone https://github.com/discourse/discourse-solved.git failed with return #<Process::Status: pid 252 exit 129>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.2.1/lib/pups/exec_command.rb:132:in `spawn'
exec failed with the params {"cd"=>"$home/plugins", "cmd"=>["sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git - sudo -E -u discourse git clone https://github.com/discourse/discourse-solved.git"]}
bootstrap failed with exit code 129
** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
2a49220a4ff35feac3c1a9a4a9c1cad1094bde4c9ba321975ebc9ae7ead2bbee

然后我使用了此格式

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git && sudo -E -u discourse git clone https://github.com/discourse/discourse-solved.git

而不是

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git
          - sudo -E -u discourse git clone https://github.com/discourse/discourse-solved.git
4 个赞

8 篇帖子已拆分为新主题:Formatting issues adding plugins to app.yml

抱歉问了一个愚蠢的问题,但我该如何知道是否需要为 git clone 语句包含 sudo -E -u discourse

3 个赞

你的 docker_manager 行看起来是怎样的?

4 个赞

我的 app.yml 中的其他条目都没有 sudo 部分,所以我省略了它,到目前为止一切正常。

5 个赞
- exec: echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /user/.ssh/config

看起来这种方法不再有效了。

sh: 1: cannot create /user/.ssh/config: Directory nonexistent

对于我自托管的 GIT 存储库,我真的很想使用 SSH 密钥。

3 个赞

您不需要容器中的 ssh 密钥,只需删除此内容:

1 个赞

抱歉,我不太明白。
我的仓库可以通过 SSH 访问。如何在没有 SSH 密钥的情况下克隆插件?

1 个赞

哦。也许我错了。那么也许你需要创建那个具有正确权限的目录。m

1 个赞

令人困惑的是,示例中的部分命令需要以 root 用户身份运行,部分则需要以 discourse 用户身份运行。这似乎不太一致。

  - exec: cd $home/plugins && git clone git@github.com:SecretOrg/secret-plugin.git

与此相对的是

  - exec: cd $home && sudo -E -u discourse bundle exec rake db:migrate

我将在运行容器的 shell 中进行一些挖掘。

这个插件安装有点复杂。如果能和安装主题一样简单就好了。

2 个赞

Dashboard.literatecomputing.com 将通过几次点击安装插件。您可以通过加入免费试用组来免费使用它。

4 个赞

@Discourse,如果您想更新指南,这是可行的代码:

要使用您自己的 SSH 密钥访问私有存储库,请按照以下步骤操作:

  1. 将私有 SSH 密钥放在 /var/discourse/shared/standalone/identity/id_rsa(或容器可访问的其他位置;如果您选择其他位置,请记住在下面的模板中相应地更新路径)。

  2. 使用以下模板添加您的插件:

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          # 设置私有存储库访问
          - chown $(whoami) /shared/identity/id_rsa && chmod 600 /shared/identity/id_rsa
          - git config --global core.sshCommand "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i /shared/identity/id_rsa"

          # 我的私有插件
          - git clone git@example.com:repo/private-discourse-plugin.git

          # 取消设置访问
          - git config --global --unset core.sshCommand
5 个赞

感谢您提供的内容,但我不需要这个,我习惯使用简单的原始版本。

您想解释一下为什么我们应该使用您的示例吗?是因为这个原因吗:

使用您自己的 SSH 密钥来访问私有存储库

1 个赞

为什么不直接在 URL 中使用令牌?

1 个赞

嗯,我们有工具来解决这个谜题 :winking_face_with_tongue:

理解容器配置代码段的实用性 AI

Jagster:

为什么有人想在 container/app.ym 中使用这个代码段?

gpt-4o:

为了有效地回答您的问题,我需要了解 container/app.yml 中特定代码段的上下文。您能否提供更多详细信息或您所指的实际代码段?这样我才能给您一个精确且有用的解释。

Jagster:

抱歉,但我需要这个组合器。

这是代码段:

”””

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          # 设置私有仓库访问
          - chown $(whoami) /shared/identity/id_rsa && chmod 600 /shared/identity/id_rsa
          - git config --global core.sshCommand "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i /shared/identity/id_rsa"

          # 我的私有插件
          - git clone git@example.com:repo/private-discourse-plugin.git

          # 取消设置访问
          - git config --global --unset core.sshCommand

”””

gpt-4o:

感谢您提供代码段。此 YAML 代码段旨在包含在 Discourse 实例的 container/app.yml 配置文件中。让我们分解一下它的作用:

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          # 设置私有仓库访问
          - chown $(whoami) /shared/identity/id_rsa && chmod 600 /shared/identity/id_rsa
          - git config --global core.sshCommand "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i /shared/identity/id_rsa"

          # 我的私有插件
          - git clone git@example.com:repo/private-discourse-plugin.git

          # 取消设置访问
          - git config --global --unset core.sshCommand

解释

  1. 设置目录

    cd: $home/plugins
    

    这会将工作目录更改为插件所在的位置($home/plugins)。

  2. 设置私有仓库访问

    - chown $(whoami) /shared/identity/id_rsa && chmod 600 /shared/identity/id_rsa
    - git config --global core.sshCommand "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i /shared/identity/id_rsa"
    
    • 将 SSH 私钥(id_rsa)的所有权更改为当前用户,并将其权限设置为 600
    • 配置 Git 使用自定义 SSH 命令,该命令使用指定的私钥并禁用严格的主机密钥检查。
  3. 克隆私有仓库

    - git clone git@example.com:repo/private-discourse-plugin.git
    

    克隆一个私有的 Git 仓库,其中包含一个 Discourse 插件。

  4. 取消设置 SSH 命令

    - git config --global --unset core.sshCommand
    

    将 Git SSH 命令配置重置为默认状态。

何时以及为何使用此代码段

  1. 私有仓库:如果您有一个私有的 Discourse 插件仓库,该仓库无法公开访问,并且需要基于 SSH 密钥的身份验证。

  2. 自动化:此脚本有助于自动化设置私有仓库访问、克隆仓库以及之后确保环境重置的过程。这对于持续部署设置或自动化服务器配置非常有用。

  3. 安全管理:它安全地处理 SSH 密钥(设置文件权限,然后取消设置自定义 SSH 命令),这对于在自动化部署期间维护安全态势至关重要。

重要注意事项

  • 确保您的私有 SSH 密钥(id_rsa)得到安全管理,并且不会暴露。
  • 根据您的安全策略验证所有权和权限是否设置正确。

有关 app.yml 和 Discourse 中 hooks 的更详细信息和上下文,您可以查看相关的 Discourse 文档

如果您有任何其他问题或需要有关配置的更多详细信息,请随时提问!

3 个赞

您好!听起来很棒!但我不知道加入免费试用组后该如何进行!请问,@pfaffman,您能帮帮我吗?我想安装 SAML 插件。如果能测试 Literate Computing 来安装和运行它,那就太好了!

谢谢!

2 个赞

我的模板片段适用于您除了 SSH 密钥之外没有其他任何方式可以访问 Git 存储库(例如,对于自托管的、位于 SSH 后面的 Git 存储库)。

5 个赞