Pregunta

I am working on a project that is hosted in Heroku. The app is hard coded to use Amazon S3 and looks for the keys in environment variables. This is what I wrote after looking at some examples and I am not sure why its not working.

echo $1
if [ $1 != "unset" ]; then
        echo "set"
        export AMAZON_ACCESS_KEY_ID=XXXXXXXXXXXX
        export AMAZON_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        export S3_BUCKET_NAME=XXXXXXXXX
else
        echo "unset"
        export AMAZON_ACCESS_KEY_ID=''
        export AMAZON_SECRET_ACCESS_KEY=''
        export S3_BUCKET_NAME=''
fi

When running the script it goes to the set section. But when inspecting through echo $AMAZON_ACCESS_KEY_ID # => ''.

I am not sure what is causing the issue. I will be interested in...

  1. A fix for this...
  2. A way to extract and add heroku config variables in the the env in an easier way.
¿Fue útil?

Solución

You need to source the script, not run it as a child. If you run the script directly, its environment disappears when it ends. Sourcing the script causes it to be executed in the current environment. help source for more information.

Example:

$ VAR=old_value

$ cat script.sh
#!/bin/bash
export VAR=new_value

$ ./script.sh
$ echo $VAR
old_value

$ source script.sh
$ echo $VAR
new_value

Scripts executed with source don't need to be executable nor do they need the "shebang" line (#!/bin/bash) because they are not run as separate processes. In fact, it is probably a good idea to not make them executable in order to avoid them being run as commands, since that won't work as expected.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top