aws / copilot-cli

The AWS Copilot CLI is a tool for developers to build, release and operate production ready containerized applications on AWS App Runner or Amazon ECS on AWS Fargate.
https://aws.github.io/copilot-cli/
Apache License 2.0
3.51k stars 414 forks source link

pipeline: make ruby version dynamic in buildspec #4446

Open fade2black opened 1 year ago

fade2black commented 1 year ago

My goal is to use Ruby 3.1.2 (since my RoR app uses 3.1.2), so I specify an image inside the pipeline manifest file like this

...
build:
  image: aws/codebuild/amazonlinux2-x86_64-standard:4.0
...

When I start build it fails with the following error:

...
Running command pipeline=$(cat $CODEBUILD_SRC_DIR/copilot/pipelines/liquandum/manifest.yml \| ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))')
--
/root/.rbenv/versions/3.1.2/lib/ruby/3.1.0/psych/class_loader.rb:99:in `find': Tried to load unspecified class: Date (Psych::DisallowedClass)
...

which is caused by parsing manifest file inside the buildspec.yml. It is a ruby script. The following fixes it:

- pipeline=$(cat $CODEBUILD_SRC_DIR/copilot/pipelines/liquandum/manifest.yml | ruby -ryaml -rjson -e 'require "date"; puts JSON.pretty_generate(YAML.load(ARGF, permitted_classes:[Date]))')

We simply need to require "date" and pass the class Date inside the permitted_classes array.

KollaAdithya commented 1 year ago

Thank you @fade2black for sharing us about the error you have found and also the workaround!

Cause:

Currently copilot pipeline buildspec Ruby runtime-version is 2.6 . But the customer is using Ruby 3.1.2, so the script ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF)) does not seem to be working well for Ruby 3.1.2.

What we can do:

  1. Codebuild already supports Ruby runtime version 3.x https://docs.aws.amazon.com/codebuild/latest/userguide/runtime-versions.html Update ruby runtime to 3.1 as default and also update the script to ruby -ryaml -rjson -e 'require "date"; puts JSON.pretty_generate(YAML.load(ARGF, permitted_classes:[Date]))

  2. Check if updating the version and script in buildspec.yml is backward compatible and works well with previous version 2.6