cuber-cloud / cuber-gem

An automation tool that simplify the deployment of your apps on Kubernetes.
https://cuber.cloud
Apache License 2.0
656 stars 24 forks source link

How can we make an env variable available at build time? #9

Closed lorismaz closed 1 year ago

lorismaz commented 1 year ago

Hello,

I'm trying to build a rails app that checks an API key in a config yml file (Honeybadger API key).

I set up the Cuberfile in /cuber/production/Cuberfile (btw is there a flag for cuber cli to define the Cuberfile location?) Please see my Cuberfile template attached.

When I go to /cuber/production/ and run cuber deploy, the buildpack is running but fail when trying to obtain the api key from the credential files:

Running: rake assets:precompile                                                                                                  
rake aborted!                                                                                                                    
Honeybadger::Config::ConfigError: undefined method `[]' for nil:NilClass
/workspace/config/honeybadger.yml:4:in `<main>' 

My honeybadger.yml file request the api key like so: api_key: "<%= Rails.application.credentials.dig(:honeybadger, :key) %>"

Thanks for your help!

My Cuberfile

# The name of your app (it is used as a namespace on Kubernetes)
app 'simple-test'

# The name of an existing release (not necessary for normal deployments)
# It's only for rollbacks to a previous version or to lock to a specific version
# release 'abcdefg-20211201170000'

# The Git repository URL (or local path)
# This repository will be cloned on your machine in order build your app
repo '../../.'

# Build a Docker image of your app automatically using Cloud Native Buildpacks
buildpacks 'heroku/buildpacks:20'

# Alternatively, if you need more customization, you can provide a Dockerfile
# dockerfile 'Dockerfile'

# The Docker repository where the image will be stored
# Include only the image name, without any tag
image 'xxx/xxx'

# You can disable the cache to refresh the base image and all the cache layers
# cache false

# The Docker config, which must contain the credentials (auth) to access the registry
# The default is ~/.docker/config.json, otherwise you can set it here
# dockerconfig 'dockerconfig.json'
dockerconfig 'dockerconfig.json'

# The kubeconfig file contains the credentials to access to the Kubernetes cluster
kubeconfig 'xxx'

# Optionally you can run a migration command (e.g. database) during each release
migrate 'rails db:migrate', check: 'rake db:abort_if_pending_migrations'

# Run and scale any process (web server, background workers, etc.) on Kubernetes
proc :web, 'bundle exec puma', scale: 4
# proc :web, 'bundle exec puma', cpu: 1, ram: 2
proc :worker, 'bundle exec sidekiq', scale: 2

# Add cron jobs (they spin up a new pod with your app image)
# cron :example, '* * * * *', 'example command'

# Set some environment variables
env 'RACK_ENV', 'production'
env 'RAILS_ENV', 'production'
env 'RAILS_LOG_TO_STDOUT', 'enabled'
env 'RAILS_SERVE_STATIC_FILES', 'enabled'
# Set some secrets that will be available as environment variables
env 'RAILS_MASTER_KEY', File.read('../../config/credentials/production.key').strip, secret: true
env 'DATABASE_URL', ENV['DATABASE_URL'], secret: true
env 'REDIS_URL', ENV['REDIS_URL'], secret: true

# Depending on your provider, you can configure specific settings of the load balancer
# lb 'kubernetes.io/ingress.global-static-ip-name', 'my-app-static-ip'

# Depending on your provider, you may want to use an Ingress instead of a Load Balancer
# You should use this on GKE for example
# ingress true

# You can provide a custom SSL/TLS certificate for HTTPS if you want
# ssl 'ssl/cert.pem', 'ssl/key.pem'
collimarco commented 1 year ago

is there a flag for cuber cli to define the Cuberfile location?

No (by design). You need to cd in the directory where the Cuberfile is located.

the buildpack is running but fail when trying to obtain the api key from the credential files

For buildpacks errors you can report them here: https://github.com/buildpacks/pack

Or you can also report the problem to Rails for rake assets:precompile... It should not need to know the credentials for compiling assets.

An alternative is to use your own Dockerfile and the Cuber dockerfile command (instead of buildpacks).

lorismaz commented 1 year ago

Thanks for the answers, I'll try with a Dockerfile !