AWS CodeBuild构建Rails项目失败,无法安装pg gem

4

我试图使用一个简单的三步骤CodePipeline流程将Rails部署到弹性Beanstalk。第一步从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:
    - '**/*'

在我的本地机器上,打包流程运行无误。


错误信息"Your PostgreSQL is too old. Either install an older version of this gem or upgrade your database to at least PostgreSQL-9.3"已经解释了你的问题,是吗?你的本地机器上使用的是哪个版本的 PostgreSQL - Riz
@Riz 在我的本地机器上,我有14.1版本,并且RDS也比9.3更新--看起来CodeBuild使用的虚拟机出了问题,但是我之前已经通过这种方式部署过Rails + pg设置,而且在构建过程中从未手动安装过新版本的pg,这似乎浪费了很多时间/资源。 - zcserei
也许你之前更新了操作系统的镜像?你可以在EC2中检查PostgreSQL版本,如果是9.3+,那么你需要检查路径或者在构建过程中提供完整路径而不仅仅是pg - Riz
@Riz,这不是最终的EC2实例,应用程序将部署在其中,这只是在CodeBuild内部。安装postgres没有必要让Bundler能够安装“pg”宝石,然后将整个bundle传递给EB。 - zcserei
也许您需要提供有关您的CodeBuild环境的更多详细信息。操作系统和运行时以及源代码以查看要求等。 - Riz
1个回答

8
我也遇到了你上面提到的问题(但没有使用CodeBuild)。问题在于你试图安装版本为1.3.5的pg gem。在撰写本文时,Amazon Linux 2(AL2)上的Elastic Beanstalk默认使用Postgres 9.2(非常旧)。然而,pg gem在版本1.2.3之后停止支持9.2。请参见此处
所以你有两个选择:
  1. 加强 EB,在构建和部署过程中安装一个更新的 Postgres 版本,例如在这里描述的方式:https://dev59.com/uFIH5IYBdhLWcg3wHZNA#63204453
  2. 或者你可以通过在 Gemfile 中配置该版本,使用 pg gem 的版本 1.2.3。该版本于 2020-03-18 发布。

以上两种选项都可以让你解决该错误。

请注意,你的应用程序还可能连接到亚马逊的 RDS,该 RDS 拥有更近期的 Postgres 版本。但是,该 RDS 位于另一台服务器上,请不要混淆!如果你使用 eb ssh 连接到服务器并在 shell 上直接运行,你就可以看到区别,例如:

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

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

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"

3
尝试使用gem“pg”,“= 1.2.3”这个第二个选项成功了。感谢详细的说明! - rick
AWS文档 从Extras Library安装软件包 的说明文档。 - Miguel Hargreaves Pimenta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接