Skip to content

Latest commit

 

History

History
203 lines (162 loc) · 4.26 KB

README.md

File metadata and controls

203 lines (162 loc) · 4.26 KB

secrets-to-dotenv

e2e

This is a fork of secrets-to-env-action

This action provides the following functionality for GitHub Actions users:

  • Read Github secrets and export all of them as environment variables and write them to a file
  • Optionally including, excluding and manipulating variables as needed before importing
Before After
- run: echo "Value of MY_SECRET1: $MY_SECRET1"
  env:
    MY_SECRET1: ${{ secrets.MY_SECRET1 }}
    MY_SECRET2: ${{ secrets.MY_SECRET2 }}
    MY_SECRET3: ${{ secrets.MY_SECRET3 }}
- run: |
    echo "MY_SECRET1=${{ secrets.MY_SECRET1 }}" >> .env
    echo "MY_SECRET2=${{ secrets.MY_SECRET2 }}" >> .env
    echo "MY_SECRET3=${{ secrets.MY_SECRET3 }}" >> .env
    ...
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
- run: echo "Value of MY_SECRET1: $MY_SECRET1"

Usage

Add the following action to your workflow:

- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}

After running this action, subsequent actions will be able to access the secrets as env variables. Note the secrets key. It is mandatory so the action can read and export the secrets.

Basic:

steps:
- uses: actions/checkout@v3
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
- run: echo "Value of MY_SECRET: $MY_SECRET"

Custom file:

steps:
  - uses: actions/checkout@v3
  - uses: 0ndt/envfile@v2
    with:
      secrets: ${{ toJSON(secrets) }}
      file: .prod.env

No environment variables:

steps:
  - uses: actions/checkout@v3
  - uses: 0ndt/envfile@v2
    with:
      secrets: ${{ toJSON(secrets) }}
      no_env: true

No file:

steps:
  - uses: actions/checkout@v3
  - uses: 0ndt/envfile@v2
    with:
      secrets: ${{ toJSON(secrets) }}
      file:

Include or exclude secrets:

Exclude defined secret(s) from list of secrets (comma separated, supports regex).

steps:
  - uses: actions/checkout@v3
  - uses: 0ndt/envfile@v2
    with:
      secrets: ${{ toJSON(secrets) }}
      exclude: MY_SECRET, MY_OTHER_SECRETS*
# MY_SECRET is not exported

Only include secret(s) from list of secrets (comma separated, supports regex).

steps:
- uses: actions/checkout@v3
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
    include: MY_SECRET, MY_OTHER_SECRETS*
- run: echo "Value of MY_SECRET: $MY_SECRET"

To export secrets that start with a given string, you can use include: PREFIX_*.

NOTE: If specified secret does not exist, it is ignored.

Add a prefix:

Adds a prefix to all exported secrets.

steps:
- uses: actions/checkout@v3
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
    prefix: PREFIXED_
- run: echo "Value of PREFIXED_MY_SECRET: $PREFIXED_MY_SECRET"

Override:

Overrides already existing variables (default is true)

env:
  MY_SECRET: DONT_OVERRIDE
steps:
- uses: actions/checkout@v3
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
    override: false
- run: echo "Value of MY_SECRET: $MY_SECRET"
Value of MY_SECRET: DONT_OVERRIDE

Convert:

Converts all exported secrets according to a template. Available: lower, upper, camel, constant, pascal, snake.

steps:
- uses: actions/checkout@v3
- uses: 0ndt/envfile@v2
  with:
    secrets: ${{ toJSON(secrets) }}
    convert: lower
- run: echo "Value of my_secret: $my_secret"

Include or skip the prefix on conversion (default is true):

steps:
  - uses: actions/checkout@v3
  - uses: 0ndt/envfile@v2
    with:
      secrets: ${{ toJSON(secrets) }}
      prefix: PREFIX_
      convert: lower
      convert_prefix: false
  - run: env
# E.g. secret with MY_SECRET would become PREFIX_my_secret

How it works

This action uses the input in secrets to read all the secrets in the JSON format, and exporting all the variables one by one.

License

The scripts and documentation in this project are released under the MIT License