Vue. How to pass a variable I have on server (process.env.SERVER) to the browser - vue.js

On the frontend server I have a file I can read with:
if(!!process.env.SERVER) {
require("dotenv").config( { path: '/hw/.env', debug: true } )
console.log(process.env.myvar)
}
But how can I pass the vlue of process.env.myvar to the browser?
Thanks.

If you want to get some variables for all cases you can create an enviroment file with just .env as the name next to package.json in your project.
To get variables from that file you need to declare them first in the .env like this:
VUE_APP_MYVAR = 'myvar'
In your app you can use it like this:
process.env.VUE_APP_MYVAR
If you need some variables depending on the process, respectively for your enviroment, your myvar can't be called with process.env.SERVER. If SERVER is the process, you need a .env.server file next to your package.json.
You can then, again, declare myvar in .env.server like this:
VUE_APP_MYVAR = 'myvar'
To get process.env.VUE_APP_MYVAR this time you have to ensure, that you build or serve your enviroment named server. Else, your app trying to get process.env.VUE_APP_MYVAR with the process you used.
For Example: If you have .env.development and .env.server, both can contain VUE_APP_MYVAR = 'myvar' with different assigned values. Which one is picked depends on the enviroment you build or serve.
For more information, see the docs: Modes and Environment Variables
#Modes

Related

Use env variable in VueJS SPA components at runtime?

I'm building a simple SPA with VueJs and Webpack, and would like to use/access env variables from my VueJS components.
I know I will not be able to change those variables "on the fly" but instead I need to recompile-rebuild-redeploy the entire application to see the changes, but for now it's ok since the actual need is to show different info and/or sections base on the deploy environment (local, staging, production).
To be more specific, for now I'm trying to get the env var COMMIT_HASH just to display it as some applications already do just for development info.
This variable would primarily used by the pipeline for the staging deployment, as in local development would be not that useful and probably not used at all for production deployment.
The problem is that I cannot figure out how to access env variables values from my VueJS components scripts sections at runtime, as process.env is a Node method accessible only during webpack compilation and not at runtime.
I thought about using string replacement and found this question, but then I would need to update Webpack script for any new env variable I need to use, so it looks quite inelegant to me.
I also tought of loading all the env variables in a js object and somehow pass it down to the files being compiled by Webpack, but this also looks inelegant/inefficient to me.
So now I'm stuck as can't figure out how to access env vars from my VueJS components at runtime.
I ended up using string-replace-loader with regex matching and a callback for dynamic replacement.
Basically I'll use env.SOME_VARIABLE in my code and search/replace it with the variable value.
For some reason I couldn't make the regex work with the \w to match for word characters and used [a-zA-Z_] instead.
{
test: /resources.*\.js$/,
loader: 'string-replace-loader',
options: {
search: 'env\.([a-zA-Z_]+)',
replace(match, p1, offset, string) {
return process.env[p1];
},
flags: 'g'
}
}
Notes
Since it is text replacement I cannot simply assign env variable values to vue component data properties as it would be interpreted as a variable:
// E.g. given env `COMMIT_HASH=some_hash`
data() {
return {
/**
* WRONG:
* this would be parsed as
* hash: some_hash
* leading to an
* Uncaught ReferenceError: some_hash is not defined
*/
hash: env.COMMIT_HASH,
/**
* RIGHT:
* wrap string to be replaced in single/double quotes so after
* replacement it became a string literal assignment.
* hash: "some_hash"
*/
hash: "env.COMMIT_HASH",
};
},

Not able to use variable defined in karate-config.js in my feature file

I want to set a Global variable for Base Test Data location which can be used in all of my feature files.
In karate-config.js i have made below changes -
var config = {
env: env,
INPUT_JSON_PATH: 'com/company/project/module/TestData'
}
And in my feature file I am trying to use it as -
Given path '/myService'
And request read('classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json')
When method POST
Then status 400
But somehow its not getting resolved and I am getting error -
could not find or read file: classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json
Any idea, what am i missing here?
Thanks,
Abhi
Just keep in mind that read() and other parts of the right-hand-side are treated as ordinary JavaScript. Maybe you were intending to do this:
And request read('classpath:' + INPUT_JSON_PATH + '/Exception_Handling/Mandatory_Fields_missing.json')

How to set Terraform workspace variables via CLI?

I'm using workspaces in terraform to separate environments at runtime, so that I can separate deployments using a different configs.
However, I'm trying to figure out how to set CLI variables.
my variables.tf:
locals {
environment = "${terraform.workspace}"
lambda_vars = {
deploy_version = "0.1"
deploy_name = "deployment"
deploy_secret_1 = "somesupersecretsecret"
}
}
These variables are used throughout my config.
I'm trying to set new variables using the CLI, but it doesnt work, and I can't seem to find any reference to how to achieve this. I've tried:
terraform apply -var 'local.lambda_vars={ deploy_secret_1 = "somesupersecretsecret1" }'
I feel like I'm going about this the wrong way.
Can anyone help?
If you want to be able to configure the variables then you should use actual variables instead of locals. Locals are a way of composing things so you avoid repeating yourself or can use interpolation. Variables don't allow for any interpolation at all.
So in your case you should declare separate variables for things you want to be able configure. If this is just the deploy_secret_1 then you can do this with something like the following:
variable "deploy_secret_1" {
default = "somesupersecretsecret"
}
locals {
environment = terraform.workspace
lambda_vars = {
deploy_version = "0.1"
deploy_name = "deployment"
deploy_secret_1 = var.deploy_secret_1
}
}
Now if you run the following command:
terraform apply -var 'deploy_secret_1=overriding-secret'
It should overwrite the deploy_secret_1 part but leave the rest as is. If you don't specify the deploy_secret_1 variable either by the command line, environment variables or a tfvars file then it will default to somesupersecretsecret. If you'd rather force it to be defined and error if you don't specify the variable then just omit the default argument to the variable declaration.
If you wanted to be able to override more things then you should declare more variables.

.env VUE_APP_ variable json?

Starting to use the vue cli 3 and ran into a use-case I can't seem to find an answer for.
How can I set an environment variable via a .env file (ie, .env.development, .env.production, etc) that exposes a JSON object? Additionally, is there a way to load an external files contents into an environment variable (i.e., require)?
Appreciate the assistance!
I came up with a solution to solve my own issue...
Although the previous answers are viable I didn't want to have to JSON.parse every time I wanted to use the JSON in the environment variable.
The approach I took was to store in each environment-specific file (i.e., .env-development, .env-production, .env-test) a file path to a file containing the JSON. For example...
VUE_APP_JSON_FILE=./.env.development.json-data
This file would contain the raw JSON...
Then in my vue.config.js I used the webpack DefinePlugin to load up the file and make it available via a global variable. For example...
new webpack.DefinePlugin({
VUE_APP_JSON: JSON.stringify(process.env.VUE_APP_JSON_FILE)
})
Defining the new variable will make the json available as an object where throughout my app I can simply reference VUE_APP_JSON.property. This saves me from having to JSON.parse the variable all throughout my app.
You can
stringObj = JSON.stringfy(YourJson),
Then save this string inside the VUE_APP_SOME_KEY_NAME.
but when you'll use it you'll have to JSON.parse () it first.
So, you cannot directly store a json object in a key value .dotEnv file.
Another option is to load these json files Base on process.env.NODE_ENV.
like: require (`config.${process.env.NODE_ENV}.js)
You should distinguish which profile using dot . (as .env.production or .env.development) and the format must be KEY=value, you can't put a json object here, only strings, but you can use JSON.parse in your code to unserialize any string in the file.
Vue cli will only allow access to environment variables that starts with VUE_APP_ and to access, process.env.VUE_APP_SECRET
You can find it and more in the docs

List all bamboo variables in inline script

I have alot of Bamboo variables defined due the fact that i have a system with alot of legacy and config at places where it does not belong. Getting rid of all this will take a bit longer on the roadmap so i need to find a way to auto replace all these values.
The number im talking about is that there are 8 customer config files with each about 100 variables. Indeed, there was a maniac who added all of those in Bamboo because as you might thought most of them are variable for each environment.
At this moment i want to automate the deployment process and all is going fine exact the fact that i need to replace 100 variables and i dont want to maintain it in my script itself all the time.
I am looking for a way to retrieve all the variables in an array so i can just iterate through all the keys and try to replace them at the config files.
echo "${bamboo.application.myvalue}" will replace the value as expected. The only problem is, how can i get all the keys under bamboo.*
I tried it with the following functions but all without success:
printenv
env
declare
All above without success. How can i retrieve a list of all those variables as inline script in Bamboo.
Thanks alot
I think it is not possible to change the value of the variables on the fly. Instead, you can use the "Inject Bamboo variables" task in order to be able to change the variable value.
This task reads a file to create the variables. So, all you have to do is to create this file with the values you need, and then use this variables.
E.g.: Creating a file from a powershell script:
$path = 'bambooVariaveis.properties'
$connectionstringX = 'connectionstring="Data Source=XXXX;"'
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($path, $connectionstringX, $Utf8NoBomEncoding)
E.g: Inject Bamboo Variables config
Using it (in a subsequent script task):
echo ${bamboo.inject.connectionstring}