Access GitHub Secrets in local env file

1.8k views Asked by At

Is there a way to access GitHub secrets in a local env file? So instead of having a secret saved directly in the env file it would reference a GitHub secret. I've seen that the GitHub secrets can be used within a workflow but I mainly just need to have the values in the env file.

1

There are 1 answers

0
oNaiPs On

I think you might be confusing things.

A local env file does not access variables, instead, your code reads the env file and uses those variables.

If you use something like dotenv, this tool will read your env files, and optionally override them if you defined the variables directly on the environment.

You can use an action workflow (disclosure: i'm the author) that exports your github secrets as env variables, so last part (overrides) would just work.

An example would be:

- run: echo "Value of MY_SECRET1: $MY_SECRET1"
  env:
    MY_SECRET1: ${{ secrets.MY_SECRET1 }}
    MY_SECRET2: ${{ secrets.MY_SECRET2 }}
    MY_SECRET3: ${{ secrets.MY_SECRET3 }}
    MY_SECRET4: ${{ secrets.MY_SECRET4 }}
    MY_SECRET5: ${{ secrets.MY_SECRET5 }}
    MY_SECRET6: ${{ secrets.MY_SECRET6 }}
    ...

You could convert it to:

- uses: oNaiPs/secrets-to-env-action@v1
  with:
    secrets: ${{ toJSON(secrets) }}
- run: echo "Value of MY_SECRET1: $MY_SECRET1"

Link to the action, which contains more documentation about configuration: https://github.com/oNaiPs/secrets-to-env-action

See this related SO post.