How to interpolate environment variables within netlify.toml config - netlify

I want to proxy to a different api depending on the environment - I've tried a few variations on the following theme without any luck. What's the correct way of doing this, if its even possible?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
This does not work.
Although the above config works when I hardcode a URI into the to field, it just fails when I try to interpolate an env var.

It's not supported, but Netlify suggest a work-around in their documentation (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your
netlify.toml file is not supported. However, the following workflow
can be used to substitute values based on environment variables in the
file, assuming you are only trying to change headers or redirects. The
rest of the file is read BEFORE your build - but those sections are
read AFTER the build process.
Add a placeholder like
API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers
sections.
Create an Build Environment Variable, for example API_KEY,
with the desired value. You can do this in the toml file or in our UI
in the Build and Deploy Settings section of your configuration. You
might use the latter to keep sensitive values out of your repository.
Add a command like this one to your build command: sed -i
s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.

Answering my own question - it's not supported, you have to manually interpolate env vars yourself as part of the build on Netlify.

Yes. It's possible. Here is the detailed docs: https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
In my case, I need to set a REACT_APP_API_URL separate for production and all other branches. Here is what I use:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"

Related

Setting environmental variables for node from host machine for build server

I'm using bitbucket pipelines as a build server.
I need to pass environmental variables from a host machine into a .env file which will then set the var values to be used in the build.
For example, lets say an environmental variable in a docker container running the build is AWS_ACCESS_KEY_ID.
In my .env file I'd like something like the following:
ACCESS_KEY=${AWS_ACCESS_KEY_ID}
I would then run the build and the ACCESS_KEY var would have a value equal to the env var in the docker container.
My current idea for a solution right now involves replacing values with sed, but that feels pretty hacky. Example:
.env file contains the following line:
ACCESS_KEY=<_access_key_replace_me_>
sed "s/<_access_key_replace_me_>/${AWS_ACCESS_KEY_ID}/g" .env
Any better solution than this?

how to setup multiple paths in user environmental variables in windows 10

I want to set up multiple paths in the user environmental variables in windows 10. but can only set a single path, how to overcome this issue?
If you want to add user defined environment variable then use react-native-config library. So in short if there are variables defined in .env file then you can use it anywhere in the react-native app like below:-
.env file
API_URL = "https://baseurl/endpoint"
//To use the above environment variable
import config react-native-config
function apiCall() { axios(config) }
With the help of react-native-config you can use environment variable in native code also and vise-versa.
For better use define .env.staging file. By default config lib finds .env file. To run the application with .env.staging file see below command.
$ ENVFILE=.env.staging react-native run-ios # bash
$ SET ENVFILE=.env.staging && react-native run-ios # windows
$ env:ENVFILE=".env.staging"; react-native run-ios # powershell
For more information checkout :- https://github.com/luggit/react-native-config
Happy coding mate :)
to add multiple paths for a single environment variable, list them all with a semicolon(";") in between like that:
C:\Cpp_Headers\pybind11;C:\Cpp_Headers\cpython\Include;C:\Users\admin\Cpp_Headers\cpython\PC
(and so forth). From this input Windows will automatically recognize multiple definitions.

Environment variables are not found in Jenkins

I want to set quite a few variables in Jenkins. I have tried putting them in .bashrc, .bash_profile and .profile of the jenkins user but Jenkins cannot find them when a build is happening.
The only way that is working is to put all the env variables inside the Jenkinsfile like this:
env.INTERCOM_APP_ID = '12312'
env.INTERCOM_PERSONAL_ACCESS_TOKEN = '1231'
env.INTERCOM_IDENTITY_VERIFICATION_KEY='asadfas'
But I don't think this is a good way of doing it.
What is the correct way of setting env variables in Jenkins?
To me, it seems very normal. INTERCOM_PERSONAL_ACCESS_TOKEN and INTERCOM_IDENTITY_VERIFICATION_KEY should be considered as text credentials and you can use the environment directive to add environment variables.
stages {
stage('Example') {
environment {
INTERCOM_APP_ID = '12312'
INTERCOM_PERSONAL_ACCESS_TOKEN = credentials('TokenCrednetialsID')
INTERCOM_IDENTITY_VERIFICATION_KEY = credentials('VerificationCrednetialsID')
}
steps {
echo "Hello ${env.INTERCOM_APP_ID}"
}
}
}
If you need to keep environment variables separate from JenkinsFile you can create a groovy file which contains all of those and then load that file into Jenkinsfile using
load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"
For more information take a look at following links
https://jenkins.io/doc/pipeline/steps/workflow-cps/
SO: Load file with environment variables ...
Jenkins resets environment variables to some defaults for their jobs. Best way to set them is in jenkins configuration. You can set global vars, local for project or local for node.
Now i do not remember if this feature is build in or provided by some plugin.

What is the best approch to handle env file?

I have .env file at my project root directory.
How should I handle .env file for dev, qa, stage and prod?
Should include them in git repo? if not where I put them? different folder on external drive for example?
What is the correct extensions? .env.qa or .qa.env?
If I want to build my bundle using webpack to the dist folder (server side), should I include the env file or manually copy it to the dist folder?
You should not check-in your env files into any source control. Any of those secrets will be forever available to anyone having access to the repo until the history is rewritten to remove them.
If you use AWS services, for example, I would suggest using the Secrets Manager.
Any environment variables introduced to Webpack should not be secrets but be configuration values. Anyone who can view source can read those values. If you need to have environment-specific configurations, the Webpack DefinePlugin will replace vars like MY_API_HOST with their values with the following config:
const plugins = [
new webpack.DefinePlugin({
MY_API_HOST: JSON.stringify('https://my-domain.com/api/'),
MY_API_VERSION: JSON.stringify('v2')
})
]
Config module is a easy way to address the different env specific values. Read about config module at - https://www.npmjs.com/package/config. You will have a config folder in the repository with env specific files and I like this approach as the files are in the repository but very well separated.This provides a really easy way to set default values, override the environment specific values etc. It is also very convenient to use the different environment specific files by setting the appropriate node environment variable(export NODE_ENV=development or acceptance or production).

How can I run script after deploy to heroku

I have an application in node.js which depends on env variables. I made some changes in code and now one of this vars should be changed after deploy. I don't want to do this manually. What is the best practise to do this automatically.
I guess that running some script after deploy could be solution, but I want to run this script only once (with this one particular change).
My only idea is that I should have script that will be checking (after each deploy) some directory if there is another script to run and then run it and remove it. But how can I achieve that?
The best way to approach this is to use the Heroku Toolbelt to set your environment variables as described here:
heroku config:set GITHUB_USERNAME=joesmith
You can then refer to these variables in your Node.js application by using the following syntax:
var dbUsername = process.env.DB_USERNAME;
Assuming you set a DB_USERNAME variable like this:
heroku config:set DB_USERNAME=myAppUserName
I like to ensure there's a fallback if the environment variable is not set, you can achieve that like this:
var dbUsername = process.env.DB_USERNAME || 'fallbackUsername';
// The string after || will be used if the process.env.DB_USERNAME variable is undefined (not set)

Resources