harre.dev

Tags: Cloudfoundry Buildpack

Cloud Foundry buildpack environment variables

I recently ran into a situation I hadn’t dealt with before.

A Python app I was working on was loading modules from a location outside of Pythons known locations. My first instinct was to update the manifest.yml env section with a PYTHONPATH variable that would add this additional location to the Python path but that would not stick. Using cf set-env to update the variable after the fact also did not work.

The environment variable was already set and consumed before we could apply our changes! The cause for this is the buildpack. The python_buildpack I was using was explicitly setting the PYTHONPATH and by the time we applied our changes it was already to late for our changes to take effect.

Thankfully, Cloud Foundry buildpacks have a solution for this. You can add a .profile file to the root of your project (from where cf push runs) and use that to run scripts during the release stage of the buildpack.

The contents of the .profile ended up looking like this:

export PYTHONPATH=$PYTHONPATH:$HOME/submodules/my-module

When the app is now deployed the buildpack will ensure that the PYTHONPATH is set correctly and the rest of the app can load the additional submodules.

Resources