dev context in netlify.toml ignores environment variables - netlify

I'm trying to add environment variables per context using the netlify.toml file. I can add them through the netlify UI and they show up, but when I add them via the .toml file nothing appears under process.env.
This is how i'm doing it currently:
[dev]
environment = { FOO = "BAR" }

Not sure whether your context is correct, but here is the layout for context in the netlify.toml:
# Specific branch context: all deploys from this specific branch will inherit
# these settings.
[context.dev]
environment = { FOO = "BAR" }
or this would work also
[context.dev.environment]
FOO = "BAR"
Taken from the docs

Related

Terraform Cloud env vars not visible in registry module

I've written some custom modules which use the method described here to get the value of workspace environment variables. Testing the module code "locally" (the test code accessing the module in the same GitHub repo) works fine in TF Cloud. But once the module is published to our private registry and I try to reference it with other code, the module doesn't seem to be able to see the same env variables defined in that workspace. For example, I'm trying to get the value for this variable using the script env.sh:
cat <<EOF
{
"env": "$env"
}
EOF
I have this code in the shared module:
data "external" "env_vars" {
program = ["${path.module}/env.sh"]
}
And then I use this to get the env variable value: data.external.env_vars.result["env"]. The variable "env" is defined in the TF Cloud workspace:enter image description here.
However, when the new code runs in TF cloud, the module is not getting anything for "env". Is there a limitation on shared modules from a registry having visibility of workspace variables?
The module code should see the workspace-defined value for "env" but it's just empty.

how to prepare python code to run from command line on different environment?

how to run python(behave)code with environment parameters? e.g.
environment=X behave --tags #regression
what I have till now is
#given(u'user is on the firts page')
def step_impl(context):
context.first_page = FirstPage(context)
context.first_page.goto(url_config.URL["X env"])
and as dict URL
URL = {
"X env": "https://...",
"Y env": "https://..."
}
You should use environment variables for this. The pipeline script should include the following command to define which environment you want to run against:
export ENV=X_env
In your test scrip, get the environment variable and use that to get the appropriate url:
import os
#given(u'user is on the firts page')
def step_impl(context):
context.first_page = FirstPage(context)
execute_in_environment = os.environ.get("ENV")
context.first_page.goto(url_config.URL[execute_in_environment])
Note that reading the environment variable - so this line: execute_in_environment = os.environ.get("ENV") is typically done at a higher level in the test framework, somewhere along with other config stuff. But going strictly by what is shared in the question I have added it to the step implementation, which isn't best practice.
If you want to try it out on your Windows station first, then set the environment variable in the CMD prompt using:
set ENV=X_env
So to run your tests against a specific environment you would run these commands (this is a Linux example):
export ENV=X_env
behave --tags #regression

How to use gitlab CI environment variables in fastlane fastfile?

I am currently using a .env file to get environment variables in FASTFILE, but now I am trying to automate the fastlane using GitLab CI/CD.
Since the .env file which has all the keys can not be pushed to the branch I have to declare all the .env or the environment variables in the GitLab runner's environment variable.
I want to know how can I use the GitLab runners's environment variable in my fastfile.
lane :build_staging do |options|
environment_variable(set: { 'ENVFILE' => '.env.staging' }) // I want to use the GitLab environment variable
clean
gradle(task: options[:task], build_type: 'Staging', project_dir: 'android/')
end
In Settings > Variables, you can define the whole file as a variable with a specified scope :
In your gitlab-ci, you would use it by specifying the variable name (in my example $ENV_FILE) and the scope using stage keyword in your job :
build:
stage: staging
script:
# do your work here
You can find more info in the documentation for variable file type and scope.

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.

How to interpolate environment variables within netlify.toml config

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"

Resources