heroku-buildpack-python icon indicating copy to clipboard operation
heroku-buildpack-python copied to clipboard

using `set_default_env` in a post_compile script

Open guybowden opened this issue 1 year ago • 4 comments

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

guybowden avatar Oct 24 '24 10:10 guybowden

@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 avatar Oct 24 '24 11:10 edmorley

(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 avatar Oct 24 '24 11:10 edmorley

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

edmorley avatar Oct 24 '24 11:10 edmorley

Thankyou @edmorley

guybowden avatar Oct 24 '24 11:10 guybowden

Closing since this is answered, but feel free to comment if you have any more questions :-)

edmorley avatar Nov 04 '24 12:11 edmorley