Need to change Environment Variables on Startup

Is there a way I can run code before database connection?

I have a problem

Jenkins has a vault that stores the DB password. This vault property names do not accept lowercase, periods or dashes. and I believe it sets it on the OS system variables (I’m not very experienced with Docker)

So the setting main.datasource.username is now “MAIN_DATASOURCE_USERNAME”

I’m thinking on running some code as soon spring boot starts.
It would be right after the spring profile is read
later I get the keys and values of the sensitive variables from the OS (if possible), change the key back to lowercase with periods and update these variables before the database connection starts.

Does this sound like a feasible plan?

Perhaps you could use an EnvironmentPostProcessor.

There is an example of such post-processor in Jmix, see jmix/QuartzEnvironmentPostProcessor.java at master · jmix-framework/jmix.

1 Like

thank you so much for the response
devops suggested to prepare a shell script file for this

is it possible to set something like this

gradle bootRun -Pmain.datasource.url=jdbc:postgresql://localhost/myappdb

I run it and i get an error the URL is not recognized

I plan to read the value from the OS environment variable and start my application with
gradle bootRun -Pmain.datasource.url=${DB_URL_ENVVAR} -Pmain.datasource.username=${DB_USER_ENVVAR}
etc…

Thank you for the help.

See java - How to add command line properties with gradle bootRun? - Stack Overflow

gradle bootRun --args="--main.datasource.url=jdbc:postgresql://localhost/myappdb" should work for you.

1 Like

NICE!!! Thank you for saving the day Mr. Krivopustov !!!