heroku / heroku-buildpack-python

Heroku's buildpack for Python applications.
https://www.heroku.com/python
MIT License
2 stars 3 forks source link

using `set_default_env` in a post_compile script #1670

Open guybowden opened 3 days ago

guybowden commented 3 days ago

Not sure if this is the right place to write this. Please let me know if not.

I used to be able to run set_default_env in a post_compile script:

source $BIN_DIR/utils
set_default_env ENV_VAR default_value

I was using v238 at the time.

Now this is no longer possible – $BIN_DIR no longer exists.

Just copying the function fails as $PROFILE_PATH is not present in the env where the post_compile script is run.

Is there another way to use the function?

My use case:

I am using review apps. I currently use my post compile script to do the following:

  1. Check if it's a review app
  2. Create a new database for the review app in an AWS RDS using a pre-seeded template database
  3. Use set_default_env to set the DATABASE_URL config var to point to this new database

I am also using it to set up a stripe webhook per review app and then store the webhook secret as a config variable.

Thanks

edmorley commented 3 days ago

@guybowden Hi!

I'd recommend writing the profile file directly - the buildpack's own scripts are considered an internal implementation detail and not part of the public API. Also, I would recommend using a separate profile script instead of writing into the file created by this buildpack (.profile.d/python.sh is considered owned by this buildpack and not really meant for modification by anything else).

eg use:

#!/usr/bin/env bash

set -euo pipefail

mkdir -p .profile.d/
echo "DATABASE_URL='...'" > .profile.d/rds_database_config.sh
edmorley commented 3 days ago

(The behaviour here changed as of #1595, which intentionally stopped some env vars leaking into subprocesses: https://github.com/heroku/heroku-buildpack-python/blob/main/CHANGELOG.md#v252---2024-06-17)

edmorley commented 3 days ago

The docs for .profile.d/ are here, in case it helps :-) https://devcenter.heroku.com/articles/buildpack-api#profile-d-scripts

guybowden commented 3 days ago

Thankyou @edmorley