Search code examples
cloud-foundrybuildpack

Are exported environment variables set in .profile.d scripts persistent in cloudfoundry container?


I am quite new in developing a custom buildpack. I want to set some container level environment variables. I added the export commands in a created script under /bin/supply .

# setup defaults
mkdir -p "${BUILD_PATH}/.profile.d/"
cat > "${BUILD_PATH}/.profile.d/defaults.sh" <<SH
export TEST_VARIABLE="hello"
SH

I read that the scripts added to .profile.d will be sourced during buildpack launch. My container starts as expected. I ssh to the container and verified that the script was inside /app/.profile.d/ . I can also see in app start logs that the variable was referenced. But when I tried to echo the variable inside the container, it was empty.

I know that I can setup the variable using cf set-env but I just want to know if it is possible in buildpack.


Solution

  • Believe it or not, things are working. Your buildpack is writing to the correct location, and the environment variable is being set for your application.

    The issue is with cf ssh. When you cf ssh into an application it does not source the .profile.d/ scripts. Those are only sourced before the application starts.

    The reason for this is that the system cannot know what is in those scripts, and it is possible that sourcing them when you cf ssh into the container could be harmful or cause side effects. Most of the time, that's not the case, and those scripts just set up the environment.

    As such, if you want to enter the container and have the environment setup for you then you can run cf ssh <app> -t -c "/tmp/lifecycle/launcher /home/vcap/app bash ‘’”. This will execute the launcher, which will source all those files.

    Alternatively, you can cf ssh like normal and then specifically source the .profile.d scripts you require.