How to pass variables from Azure Pipeline to JavaScript? - javascript

I'm attempting to run an Azure Pipeline, with some environment variables defined in the pipeline itself, and I am using the Maven Build task to build my project with Maven to launch various tests. A JS file automatically runs before each test, and I'm supposed to configure these variables in this file, which would make them available to use in the tests themselves. Currently, I have something like this:
# azure-pipelines.yml
trigger:
- none
pool:
vmImage: ubuntu-latest
variables:
- name: user_secret
value: "foo"
steps:
- task: Maven#3
inputs:
mavenPomFile: 'pom.xml'
goals: test
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
// config.js
function fn(){
return {
secret: user_secret
};
}
The error I get is "user_secret" is not defined
I am being told I can debug it locally because the pipeline is providing the values as environment variables, and that I should be able to validate that locally by setting the variables when I run my code. Unfortunately, I can't figure out how to do it.
Similar to the above example, I also tried to get the variable process.env.user_secret, but this didn't work for me either.
How can I access Azure variables in my code?

Related

Change .page URL based on environment I need to run the suite in

We have been building our automation suite using our staging environment, but are going live soon and want to be ready to tell the project where to run (staging, production).
The only difference between the sites in the environments is the URL. My question is, from start to finish, how can I set the .page URL via a CLI option?
Right now, I have created an environment config file that holds our staging and production URLS and then I call the data into my test files. This is fine for now, but I will need to create a script with an option to set the environment at runtime without having to do a manual find and replace before kicking it off.
I've looked around online and find, what I believe, to be code snippets and general instructions, but I'm not a dev at heart and go crossed eyed. If I could get an ELI5 for this, that would be awesome.
Example of what I'm doing now:
const env = require('../environment_variables.json')
fixture `blog`
.page `${env.production}`
And then I change production to staging or vice versa manually before kicking off the suite.
Since the project will run from CICD, I would like to be able to do something like this in my CLI and script:
testcafe env=production
The env value will then be set where the .page call is for every test file.
Thanks!
There are different ways of doing this. I've used environment variables successfully in this situation, so I'll share this solution since it will solve your problem.
I create config.json in the root of the project:
{
"baseUrl": {
"dev": "https://dev.com/",
"staging": "https://staging.com/",
"prod": "https://prod.com/"
}
}
Then I create two helper functions somewhere like Helpers/env.js:
import config from '../config';
function getEnv () {
return process.env.TESTCAFE_ENV;
}
function getBaseUrl () {
return config.baseUrl[getEnv()];
}
export { getEnv, getBaseUrl };
Then in my test files in Tests/:
import { getBaseUrl } from '../Helpers/env';
const baseUrl = getBaseUrl();
fixture `Test Suite`
.page(baseUrl);
And that's it. Then when I need to run tests on the dev, I execute:
$ TESTCAFE_ENV=dev testcafe
for staging:
$ TESTCAFE_ENV=staging testcafe
and for production:
$ TESTCAFE_ENV=prod testcafe
In v1.20.0 and later, TestCafe offers a way to specify the baseUrl in the test run configuration. You can use this approach along with environment variables, see the following example:
.testcaferc.js
const BASE_URL_MAP = {
dev: 'https://dev.com/',
staging: 'https://staging.com/',
prod: 'https://prod.com/'
};
module.exports = {
baseUrl: BASE_URL_MAP[process.env.TESTCAFE_ENV]
};
Alternatively, you can use different configuration files for each of the required setups using the --config-file option.

npm-config Environment Variables

I saw that best way to store some secret strings is using config package and environment variables. This is how I set it up.
Created a config folder with 2 files (default.json, custom-environment-variables.json)
In default.json I created this:
{
"passPrivateKey": ""
}
In custom-environment-variables.json I created this:
{
"passPrivateKey": "nodeProject_passPrivateKey"
}
After I set the variable in terminal with this command:
npm config set nodeProject_passPrivateKey=randomKey
When I am reading the variable from terminal with command below it works fine and shows the correct value
npm config get nodeProject_passPrivateKey
However in code I have these lines:
if (!config.get("nodeProject_passPrivateKey")) {
console.error("nodeProject_passPrivateKey has not been set");
}
So yeah the problem is this method config.get() is not reading the value and I am getting the error not set from above. I tried doing everything in vs code as admin, and using config.get on "nodeProject_passPrivateKey" and "passPrivateKey" but the method is still not reading any value.
Why not use dotenv?
You create an .env file where you store all your secrets and you access them through
process.env.myvar

next.js environment variables are undefined (Next.js 10.0.5)

I am coding a website with Next.js and I tried to add google Tag Manager.
I followed the tutorial on the Next.js Github example but for some reasons I can't access to my environment variables.
It says my variable is undefined.
I created a file .env.local on my project folder (at the same level as components, node_modules, pages, etc)
In this file I created a variable like this (test purpose) :
NEXT_PUBLIC_DB_HOST=localhost
And on my index page I tried this code :
console.log("test ", process.env.NEXT_PUBLIC_DB_HOST);
But in my console I get a "test undefined".
I tried to put my variable into an .env file instead, without success.
What I am doing wrong ?
This envs just works in Server Side. To access this envs in Client Side, you need declare in the next.config.js
This way:
module.exports = {
reactStrictMode: true,
env: {
BASE_URL: process.env.BASE_URL,
}
}
Create .env (all environments), .env.development (development environment), and .env.production (production environment).
Add the prefix NEXT_PUBLIC to all of your environment variables.
NEXT_PUBLIC_API_URL=http://localhost:3000/
Use with prefix process.env
process.env.NEXT_PUBLIC_API_URL
Stop the server and restart it:
npm run dev
I hope it works.
This solution for latest version of nextJs (above 9)
Restarting the server worked for me.
Edit & save .env.local
Stop the server and restart it, npm run dev
You should get an output on the next line like this:
> klout#0.1.0 dev
> next dev
Loaded env from [path]/.env.local
For those using NextJS +9 and looking for environment variables in the browser, you should use the NEXT_PUBLIC_ prefix. Example:
NEXT_PUBLIC_ANALYTICS_ID=123456789
See documentation for reference.
After spending countless hours on this, I found that there is a tiny little paragraph in both the pre and post nextjs 9.4 documentation:
(Pre-9.4) https://nextjs.org/docs/api-reference/next.config.js/environment-variables (same as this answer)
Next.js will replace process.env.customKey with 'my-value' at build time.
(^9.4) https://nextjs.org/docs/basic-features/environment-variables
In order to keep server-only secrets safe, Next.js replaces process.env.* with the correct values at build time.
Key words being BUILD TIME. This means you must have set these variables when running next build and not (just) at next start to be available for the client side to access these variables.
This is my next.config.js file.
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
env: {
BASE_URL: process.env.NEXT_PUBLIC_SITE_URL,
},
};
module.exports = nextConfig;
Restart the server and it worked fine. using Nextjs 12.1.0 with typescript
In my case, Im pasting REACT_APP_API_URL instead of NEXT_PUBLIC_API_URL.
Adding with the most recent version of the documentation on this, v12+.
Using the next.config.js file you can specify server and client variables:
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
},
}
You can still use an env.local file, and pass the variable in to the next.config.js file. For example:
publicRuntimeConfig: {
DB_URL: process.env.DB_URL
}
And then you can access the variable like this:
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
publicRuntimeConfig.DB_URL;

How to use Digitalocean's app platform component environmental variables?

I am using Digital Ocean's app platform to host a NodeJS app. I do not understand the documentation for using environmental variables within my NodeJS code. https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables when I try to add the environmental variable like I think the documentation wants me to I get a syntax error: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true});
What is the correct usage?
Step 1:
In your App settings you should find the App-Level Environment Variables.
You could set a variable there like this:
Keys: APP_LEVEL_EXAMPLE_VAR
Values: rafiki
Don´t forget to save.
Now you have an App-Level Environment Variable.
Step 2:
In your app´s project.yml file, you can set a
variable that reads the App-Level variable like this:
environment: {
EXAMPLE_VAR: ${APP_LEVEL_EXAMPLE_VAR}
}
Step 3:
Now you can access the value of APP_LEVEL_EXAMPLE_VAR in your app like this:
const value = process.env.EXAMPLE_VAR

Access system variable (Environment Variable) while building Angular 2 application

I need to build my Angular application using one system variable.
System Variable
server_url = http://google.com
My Environment.ts file look like
export const environment = {
production: false,
serveUrl: 'http://someurl.com',
mleServerUrl: 'http://someurl2.com',
botServerUrl: 'http://someurl2.com',
DEBUG_LOG: true,
azureAD: true,
mleEnvironment: 'dev',
multtenant: true,
e2eTest: false
};
I need to replace serveUrl value with http://google.com while executing ng build command.
You need a environment.prod.ts (in the same folder as environment.ts) with
export const environment = {
production: true,
serveUrl: 'http://google.com',
};
then you have to do a ng build --prod
See https://github.com/angular/angular-cli/wiki/build
If you want to use system environment variables instead of the standard angular approach here are a few options:
Create a env service that is available through your Angular app; leverages a env.js file which can be modified at deploy time. https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/
Modify your webpack process with a custom angular build which allows you to read system environment variables at build time https://blog.usejournal.com/system-environment-variables-in-angular-1f4a922c7b4c

Categories