2

How do I inject secrets and environment variables residing in my config.json to GitHub action in nodejs?

For instance

{
   apiKey: "blablabla",
   apiId: "anotherblabla"
}
  • You can't read a JSON natively in Github Actions (yet?). Here, you would have to use a customized **script** or **shell commands** to extract the variables from the JSON, and then inject them as an env variable using something like this: `echo "apiKey=$API_KEY_EXTRACTED_VALUE" >> $GITHUB_ENV` (or inject them as secrets using for example the Github CLI [gh secret set](https://cli.github.com/manual/gh_secret_set)). – GuiFalourd Apr 07 '22 at 14:38
  • Actually, I read from [this thread answer](https://stackoverflow.com/questions/61919141/read-json-file-in-github-actions) that there is a [fromJson](https://docs.github.com/en/actions/learn-github-actions/expressions#fromjson) function that can be used to provide a JSON object as an evaluated expression or to convert environment variables from a string. That may be the solution in your case. – GuiFalourd Apr 07 '22 at 14:54

1 Answers1

0

Use github CLI and you get many options on how to inject secrets:

Paste secret value for the current repository in an interactive prompt

$ gh secret set MYSECRET

Read secret value from an environment variable

$ gh secret set MYSECRET --body "$ENV_VALUE"

Read secret value from a file

$ gh secret set MYSECRET < myfile.txt

Set secret for a deployment environment in the current repository

$ gh secret set MYSECRET --env myenvironment

Set organization-level secret visible to both public and private repositories

$ gh secret set MYSECRET --org myOrg --visibility all

Set organization-level secret visible to specific repositories

$ gh secret set MYSECRET --org myOrg --repos repo1,repo2,repo3

Set user-level secret for Codespaces

$ gh secret set MYSECRET --user

Set repository-level secret for Dependabot

$ gh secret set MYSECRET --app dependabot

Set multiple secrets imported from the ".env" file

$ gh secret set -f .env
Tyler2P
  • 2,324
  • 26
  • 22
  • 31