ruby-on-rails - Rails 的 AWS CodeBuild 进程无法安装 pg gem

标签 ruby-on-rails postgresql amazon-web-services aws-codepipeline aws-codebuild

我正在尝试使用简单的 3 步 CodePipeline 流程将 Rails 部署到 Elastic Beanstalk。第 1 步从 GitHub 获取源代码,将其提供给 CodeBuild,后者应创建构建并将其传递给 EB。

由于某种原因,当 CodeBuild 进程到达 pg gem 时,它失败并出现以下错误:

Fetching pg 1.3.5
Installing pg 1.3.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5/ext
/root/.rbenv/versions/2.7.2/bin/ruby -I
/root/.rbenv/versions/2.7.2/lib/ruby/2.7.0 -r ./siteconf20220419-56-10cqcfm.rb
extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /usr/bin/pg_config
Using libpq from /usr/lib64
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconninfo() in libpq-fe.h... no
Your PostgreSQL is too old. Either install an older version of this gem or
upgrade your database to at least PostgreSQL-9.3.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/root/.rbenv/versions/2.7.2/bin/$(RUBY_BASE_NAME)
    --with-pg
    --without-pg
    --enable-gvl-unlock
    --disable-gvl-unlock
    --enable-windows-cross
    --disable-windows-cross
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pq-dir
    --without-pq-dir
    --with-pq-include
    --without-pq-include=${pq-dir}/include
    --with-pq-lib
    --without-pq-lib=${pq-dir}/lib
    --with-pqlib
    --without-pqlib

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5 for inspection.
Results logged to
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/gem_make.out

An error occurred while installing pg (1.3.5), and Bundler cannot continue.
Make sure that `gem install pg -v '1.3.5' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  pg

[Container] 2022/04/19 14:56:37 Command did not exit successfully bundle install exit status 5
[Container] 2022/04/19 14:56:37 Phase complete: INSTALL State: FAILED
[Container] 2022/04/19 14:56:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: bundle install. Reason: exit status 5

我有以下准系统buildspec.yml:

version: 0.2

phases:
  install:
    commands: 
      - bundle install
artifacts:
  files:
    - '**/*'

捆绑过程在我的本地机器上运行没有错误。

最佳答案

我也遇到了你上面遇到的同样问题(除了没有使用 CodeBuild)。问题是您正在尝试安装 1.3.5 版本的 pg gem。 在撰写本文时,Amazon Linux 2 (AL2) 上的 Elastic Beanstalk 默认附带 Postgres 版本 9.2(非常旧 😔)。而 pg gem 在 1.2.3 版本之后放弃了对 9.2 的支持。参见 here .

所以你的 2 个选项是:

  1. 增强 EB 以在构建和部署过程中安装更新版本的 Postgres,例如此处所述:https://stackoverflow.com/a/63204453/1852005 .
  2. 或者您可以通过在 Gemfile 中配置 1.2.3 版本的 pg gem 来使用该版本。该版本于 2020 年 3 月 18 日发布。

这些选项中的任何一个都可以让您克服该错误。

请注意,您的应用程序还可以连接到具有更新的 Postgres 版本的 Amazon 的 RDS。然而,RDS 位于另一台服务器上,所以不要混淆两者!如果您 eb ssh 到服务器并运行例如:直接在 shell 上运行,您可以看到不同之处:

[root@ip-XXX-XX-XX-XXX current]# psql --version
psql (PostgreSQL) 9.2.24

与在显示 RDS 版本的 Rails 控制台上运行相比:

irb(main):001:0> ActiveRecord::Base.connection.select_value('SELECT version()')
=> "PostgreSQL 11.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit"

关于ruby-on-rails - Rails 的 AWS CodeBuild 进程无法安装 pg gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71927100/

相关文章:

mysql - 在 Rails 中建立连接的 MySQL 数据库 url 格式是什么?

css - Rails 4 + bower-rails gem + bower + bootstrap sass

Postgresql:如何授予设置角色的权限?

sql - postgresql:tableA 或 tableB 的外键

java - JDBC:检查存储过程返回值

ruby-on-rails - 在具有可变间隔的日期之间搜索 w/rails

python - 如何打开流而不是将整个文件加载到 python lambda 内的内存中

html - 如何将特定的 Internet 服务提供商重定向到我的网站之外

amazon-web-services - 在 Solr 客户端上指定 ZooKeeper 主机的最佳方法?

ruby-on-rails - 尝试在 Windows 上通过 Cygwin 调用 rails 命令时出错(通过 railsinstaller 安装)