Using env variables in swagger.yaml in nodejs - node.js

Trying to figure out how can i access the env variable inside swagger.yaml configuration file.
The variable can be access inside the nodejs application using process.env.VARNAME. I want to use the same variable inside swagger.yaml file.
something like
definations:
myvariabledetail: "${process.env.VARNAME}"
. I already tried different combinations including "${process.env.VARNAME}",${process.env.VARNAME},${VARNAME} etc.

YAML as a text file format doesn't know anything about environment variables. A solution would be to load the YAML and then have code that uses a regex to find the environment variables and replace them with the current values. Then finally pass that resulting string into your YAML parser.

You can use envsub:
const envsub = require('envsub');
envsub({
templateFile: `${__dirname}/input.yml`,
outputFile: '/dev/null', // or filename to save result
})
.then(({ outputContents }) => console.log(outputContents));

Related

How to split the variable in Gitlab pipline (.gitlab-ci.yml)

I have a branch name, for example, release-1.1.3, how I can split and extract the version by writing some logic in gitlab-ci file. Is there some method available for it?
before_script
export BRANCH_NAME=$CI_COMMIT_REF_NAME
// here I want to do something to extract the version
export Major=BRANCH_NAME.someoperation
export Minor=BRANCH_NAME.someoperation
export PATCH=BRANCH_NAME.someoperation
any other approaches are also welcome but I required the value in a separate variable which I can pass it to other operations.
I am new to gitlab-ci and working on it, thanks in advance.
Just split your string
You will find some examples here.
How to cut a string after a specific character in unix

.gitlab-ci.yml Override Variable Defined in appsetings.json

I am trying to replace env variables used by AspnetCore by defining them in .gitlab-ci.yml file.
appsettings.json file looks like this:
"ConnectionStrings": {
"DatabaseConnection": "my-connection-string"
},
Here are some things i tried.
Invalid Yml:
ConnectionStrings:
DatabaseConnection: "my-connection-string"
Valid Yml (Not working):
"ConnectionStrings:DatabaseConnection": "my-connection-string"
"ConnectionStrings::DatabaseConnection": "my-connection-string"
For last two i ger this error and as you can see it is all transformed into one long string instead of key value pair for env varijable:
/bin/bash: line 87: export:
`ConnectionStrings::DatabaseConnection=Server=mssql,1433;Database=MyDatabase;User
Id=SA;Password=MyPassword;Trusted_Connection=False;MultipleActiveResultSets=True':
not a valid identifier
I am using linux runner for CI.
Looks like you're trying to replace the database connection depending on the environment. You could use some strategies like configure variables
"ConnectionStrings": {
"DatabaseConnection": "$CONNECT_ENV"
},
where you could have one per environment for instance $CONNECT_INT, $CONNECT_QA, $CONNECT_PROD and configure each one of these variables properly (variables part of .gitlab-ci.yml)
variables:
CONNECT_INT : "ConnectionStrings::DatabaseConnection=Server=mssql,1433;Database=DB_INTEGRATION_PROD;User Id=SA;Password=MyPassword;Trusted_Connection=False;MultipleActiveResultSets=True"
Or you could have multiple files per environment, CONSTRING_INT.conf, CONSTRING_PROD, and replace it depending on the environment.
Or you could replace the connection string using shell commands. https://unix.stackexchange.com/questions/226005/how-can-i-replace-a-specific-string-within-a-line-inside-a-text-file
An example of a real project we're using 1
[Real example of one project]1

How to programmatically write a value to a variable in my .env file?

So for my Node.js app I created a seed script that can be called from the command line.
This creates a user and a password based on a JWT secret.
When running the script I want the secret to be a custom generated string and write it to the APP_SECRET variable in my .env file.
Is there any way to programmatically fill the variable with this custom string in the .env file?
So, you need do 2 things: generate random string and write it to your .env file.
For generating random string use npm package or your handmade generator based on node crypto module.
Than use fs nodule to write variable to file.
For example,
fs.appendFileSync('.env', 'APP_SECRET=var');
or
fs.appendFile('.env', 'APP_SECRET=var', function (err) {
if (err) throw err;
});
However, this would append string to file. If you want replace ENV variable every time, use fs.writefile().

Is there a way to input variable values from outside to terraform main file?

Is there a way I can input variable values from outside to terraform main file. It can be a excel sheet or sql db. Is it possible to do so ?
What you can't currently do is point you cmdline at a db i.e. to replace a tfvars file, but what you can set up in Terraform is to use a number of different key value stores:
consul
https://www.terraform.io/intro/examples/consul.html
aws parameter store (using a resource or data)
https://www.terraform.io/docs/providers/aws/d/ssm_parameter.html
There are quite a number of other key/value stores to choose from but there's no zero code solution and you will end up with lots of these statements:
Setup a key in Consul to provide inputs
data "consul_keys" "input" {
key {
name = "size"
path = "tf_test/size"
default = "m1.small"
}
}
There are many ways to do that;
You can use a tfvars file with all your inputs and you can use one file customer, user, environment
You can pass the variables to terraform executable on the command line
You can define environment files prefixed wit TF_VAR_[variable]
You can use https://www.terraform.io/docs/providers/aws/d/ssm_parameter.html as suggested above
You can even store variables in DynamoDB or any other database
You can use Consult+Vault as well

Passing Main script variables into Perl Modules

I am writing a Perl script that is run by a user and makes use of the current Linux environment as variables and other variables as well. The environment settings may change and be different from what they were originally.
However, I'm trying to use self-contained Perl Modules and need to be able to access these variables. What is the best practice to go about doing this? I can just pass along 10 variables when I create an object using the Perl Module, but that seems excessive...
Thanks
The environment variables are accessible from anywhere in the global %ENV hash:
print $ENV{HOME};
If you are creating objects, they probably have some attributes (being the objects hashes, arrays or even inside out objects...) Just store the relevant values into the attributes, e.g.
my $obj = Some::Package->new( name => 'Homer',
surname => 'Simpson',
city => 'Springfield',
# ... 7 more
);

Resources