Assume that I’ve a complex json file that is used to configurate my project.
Like the json below:
{
"apis": {
"payment": {
"base_url": "https://example.com/"
},
"order": {
"base_url": "https://example.com/"
},
},
"features": {
"authentication": {
"authProviders": true,
"registration": false
}
},
"availableLocales": [
"en",
"es"
]
}
With .Net there’s a feature that allows us to override the values based on environment variables.
If I wanted to override the value of apis.payment.base_url I could pass an environment variable: APIS__PAYMENT__BASE_URL and the value would be replaced.
Since I’m currently not using .Net is there any alternatives?
This is what I’m using right now, but this does not fit my needs
FROM code as prepare-build
ENV JQ_VERSION=1.6
RUN wget --no-check-certificate
https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64
-O /tmp/jq-linux64
RUN cp /tmp/jq-linux64 /usr/bin/jq
RUN chmod +x /usr/bin/jq
WORKDIR /code/public
RUN jq 'reduce path(recurse | scalars) as $p (.;setpath($p; "$" + ($p | join("_"))))'
./configurations/settings.json > ./configurations/settings.temp.json &&
yez | cp ./configurations/settings.temp.json ./configurations/settings.json
WORKDIR /code/deploy
RUN echo "#!/usr/bin/env sh" | tee -a /code/deploy/start.sh > /dev/null &&
echo 'export EXISTING_VARS=$(printenv | awk -F= '''{print $1}''' | sed '''s/^/$/g''' | paste -sd,);' | tee -a /code/deploy/start.sh > /dev/null &&
echo 'for file in $CONFIGURATIONS_FOLDER;' | tee -a /code/deploy/start.sh > /dev/null &&
echo 'do' | tee -a /code/deploy/start.sh > /dev/null &&
echo ' cat $file | envsubst $EXISTING_VARS | tee $file' | tee -a /code/deploy/start.sh > /dev/null &&
echo 'done' | tee -a /code/deploy/start.sh > /dev/null &&
echo 'nginx -g '''daemon off;'''' | tee -a /code/deploy/start.sh > /dev/null
WORKDIR /code
This was I have a problem that, I need to pass all the json paths as environment variables, to override it correctly. If not, the variables will be replaced with the path of it, only.
I think the best approach would be:
Read the environment variables and create a json file with their values, then override the existing json file with the values of the created one.
Does anyone have any thing that could help me achieve this?
To summarize.
In order to make easy to identify which environment variables I should use, let’s assume it will have a prefix of SETTINGS.
Example of how I would override values.
JSON PATH | EQUIVALENT ENVIRONMENT VARIABLE |
---|---|
APIS.PAYMENT.BASE_URL | SETTINGS__APIS__PAYMENT__BASE_URL |
AVAILABLELOCALES[0] | SETTINGS__AVAILABLELOCALES__0 |
2
Answers
I’m a
jq
novice, and I’d be very interested in a betterjq
script, but here’s one way to use environment variables to modify asettings.json
file.See it work on jqplay.org.
The task can be solved using jq.
The version is robust against settings that do not match a path in the document.
Variables
Code
Output