dsheiko/puppetry

How to access Environment Variables within the App

seanbuscay opened this issue · 3 comments

I understand I can use Environment Variables via template expressions like {{ env( NAME ) }} as shown here: https://docs.puppetry.app/template#environment-variables

I know how to tweak the cli export to use a .env file to load variables, however, is there a way to use ENV variables when running tests via the app GUI?

I don't see any settings to reference an .env file or how to run and possibly export the env files before running the tests in the GUI.

Thanks much. This is a great app.

Hm, environment variables are OS related (e.g. https://en.wikipedia.org/wiki/Environment_variable). For example you can set them for the session on LINUX/MACOS as:

setenv FOO value

or you can secure them system wide in file /etc/environment.
The dot file, you mentioned, is rather a hack for development.

So I believe if you set some env variables for you system, puppetry will access them.

Hi @dsheiko Thanks for your response. I realize now I should have asked about what I was trying to achieve rather than the method I was trying to use.

I apologize for the long comment. I thought I’d do my best to provide as much detail as possible, and, like Stack Exchange, share as much previous research I put into finding an answer before asking others.

My question was about how to achieve the following:

Objective

I want to keep secret variables out of a shared version control repo containing either the Puppetry project files or the exported Jest project files.

Below details the background, what I tried, and how I finally achieved the objective. It’s long, but to sum up what I am asking now in this comment is, “Do you know of a better way to achieve the objective than the solution I used?”

Background

(I may be wrong.) It appears when using template variables , though the app interface provides a way to not show sensitive data like passwords, the plain text value of those sensitive values are still saved in the Puppetry project files and when exported to a Jest project for use in CI.

The excellent Puppetry documentation describes how to use environment variables in template expressions. I chose to use this method for tests requiring things like HTTP authentication or passwords. Such as: {{ env( “EMAIL_PASSWORD” ) }} .

Once the tests were exported as a Jest project I could easily export the environment variables from the command like export EMAIL_PASSWORD=“passwordtext” jest when running the Jest tests.

This works well, but I still wanted to be able to run those tests via the Puppetry app and share the project files in GitHub with other devs on the team.

My initial question was about trying to find a way to provide secret variables to the Puppetry app when launching the app (typically on Macs). I did not want other devs to have to go through a cumbersome process of setting a system-wide path environment variable and then possibly requiring them to restart for them to take affect. Plus, these test variables are really just temporary and may change on the fly.

At the time I asked my initial question I did not know about adding and removing environment variables on the fly like:

launchctl setenv variable "value"

launchctl getenv variable

launchctl unsetenv variable

So the above was probably the answer to my initial question.

Before I found the above option, I ended up just opening the Puppetry app via my shell and just passing the environment variable prior to the open command. I’ll illustrate below.

Current Solution

  1. When building tests in the Puppetry app that must access secret data, use template expressions for environment variables for any sensitive data in the test, like: {{ env( SECRET_VALUE ) }} . No sensitive data is stored anywhere in plain text (either in the Puppetry project files or the Jest project exports) and project files are safe to store in a shared repo.
  2. Prior to running tests in the Puppetry app GUI, export the environment variables and launch the app via the command line, like: export EMAIL_PASSWORD=“passwordtext” && open /Applications/puppetry.app. This allows for running all the tests in the GUI with access to the environment variables during runtime.
  3. When running exported Jest project tests from the command line, launch as previously described: export EMAIL_PASSWORD=“passwordtext” jest . Note, our current use of the Jest project test cases is less for an automated CI process and more for developers to run regression tests against their local server, the integration server, or the QA server.

An Additional Note:

We don’t actually have to remember all environment variables and manually export them for testing. For most projects we keep all sensitive data out of the repo and store the values in a local copy of a git ignored .env file. Then we have a wrapper to launch whatever tests and test tools we are using which will import the environment variables from the .env file and export when launching the test tool. We also keep some local specific configuration in the .env files and check for them in our test configurations. This allows developers to change some test configuration without overwriting the repo stored defaults or accidentally committing something like their local paths back to the repo. Developers can, for example change a test report output path, snapshots path, or something like their path to Chrome in the .env file.

Thanks again for your great app. I’m hoping to use it a lot more in a team collaboration workflow. I’d appreciate any additional tips you may have from your experience.

All the best - Sean

Hey Sean,
Hidden template variables hides them only visually in UI, so the only option is really dealing with env variables.
You can also keep user-specific env variables in ~/.bash_profile (or ~/.profile) on MacOS.
As for Jest project you can go as:

npx cross-env SECRET1="some secret" SECRET2="some secret"  jest

If you wish to rely on .env file. It can be done for Jest project by using https://www.npmjs.com/package/dotenv
but it probably won't work with Puppetry GUI because the modules searches for .env in app directory which in our case within the package