Build variables not accessible in my react native app - azure

I need to inject env variables into my code.
I'm using azure pipelines to build my android app in react native.
I have set env variables in the build configuration and I have created a file called appcenter-post-clone.sh. The contents of this file are as follows:
ENV ADMIN_HOST= $ADMIN_HOST
And in my build configuration I have defined
ADMIN_HOST = https://example.com.
But I'm getting this error, [command]/bin/bash /Users/runner/runners/2.160.1/work/1/s/appcenter-post-clone.sh
ENV: https://example.com: No such file or directory. What I fail to understand here is, why is azure treating the value of my env variables as a file? How do I make this work?

The blunder I made here is, I should have used
ENV ADMIN_HOST=$ADMIN_HOST
Without the space. That solved it for me.

Related

How can I get rid of the "Warning: Invalid configuration encountered" in serverless.yml?

Currently I'm trying to breakdown my serverless service into multiple services to get over the cloudFormation resource limit.
My current project structure is as follows:
aws-backend
functions
workers
serverless.yml // workers service
.env.local
.env.dev
serverless.yml // Rest of the functions in here
In my workers service, I'm trying to reference the .env.* files in the root folder using variables.
My issue is when i use the following syntax
${env:SLS_AWS_REGION}
I get a
Error:Cannot resolve serverless.yml: Variables resolution errored with:
- Cannot resolve variable at "provider.region": Value not found at "env" source
but when I use the following syntax:
${../../env:SLS_AWS_REGION}
It works but I get a warning:
Warning: Invalid configuration encountered
at 'package.individually': must be boolean
at 'provider.region': must be equal to one of the allowed values [use-east-1, etc...]
How can I get rid of this error? Am I even using the correct syntax?
Thanks
as for this error
Error:Cannot resolve serverless.yml: Variables resolution errored with:
- Cannot resolve variable at "provider.region": Value not found at "env" source
You get this error because the Framework cannot find SLS_AWS_REGION environment variable. The env variable source doesn't read from .env files, but from environment variables from the process.
As for this syntax:
${../../env:SLS_AWS_REGION}
This does not work because env is a correct variable source, not ../../env.
You have two options here:
Ensure that content of the .env file(s) is exported so the variables defined in these files are actually exported as environment variables before running serverless commands.
Set useDotenv: true in your serverless.yml file so the .env files will be loaded automatically with dotenv. Please see the docs for reference on how it works: https://www.serverless.com/framework/docs/environment-variables
According to the plugin documentation, you should run sls deploy with --stage or --env (deprecated in Serverless >=3.0.0) or NODE_ENV corresponding to the .env file name. If you run it without any of those flags, it will default to development, and the plugin will look for the plugin will look for files named .env, .env.development, .env.development.local
Note that Serverless Framework .env files resolution works differently to the plugin.
The framework looks for .env and .env.{stage} files in service directory and then tries to load them using dotenv. If .env.{stage} is found, .env will not be loaded. If stage is not explicitly defined, it defaults to dev.
I believe the plugin takes precedence here.

Using dotenv in cloud run

I have created a .env file in my local system while developing a project. If I upload my project along with .env file, will it work fine or do i have to assign env variables separately?
According to the documentation it won't work like that.
You can set them in Console or provide with --set-env-vars flags during deployment from command line or set id Dockerfile with ENV parameter.

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?

Not able to access Gitlab env variables either in node.js or react

I have two different projects checked in Gitlab, frontend and backend.
For both the projects i have a Dockerfile each.
I have set the env variables in gitlab ci/cd .
I am running docker container in kubernetes,
but i am not able to access the gitlab env variables either in my react or node.js (express) application.
I was thinking that those env variable would be available to me when i do process.env.variable_name, but i am not able to access them.
What’s the best way to access Gitlab env variables in kubernetes (deployment.yaml) env variables ?
UPDATE
I have found that we can specify env variables in kubernetes, deployment.yaml file (under env section). How can i pass gitlab env variables to deployment.yaml?
Docker containers require that you set environment variables when you run them:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
--env , -e Set environment variables
I was able to access environment variable in Node js application using Gitlab K8S_SECRET_ variable naming convention.
For example define a variable like K8S_SECRET_MY_TEST_API in gitlab.
In Node js, you can access this variable using process.env.MY_TEST_API.
Only issue I am facing is that those variables are not available in React app. Still trying to figure that out. I will update here once I resolve that issue.
To inject environment variables to a react app created with create-react-app you should add the prefix REACT_APP_ to every env var.
During the build, webpack will pick all the environment variables with that prefix and will add them to environment.

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).

Resources