Skip to content

Commit

Permalink
Merge pull request #61 from AngelOnFira/add-multiline-secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelOnFira committed Jun 17, 2023
2 parents 9228ab2 + 9aeaba5 commit 1e12712
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 28 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/test-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Make envfile
- name: Test use GitHub Action secret
uses: ./
with:
envkey_DEBUG: false
envkey_SOME_API_KEY: '123456abcdef'
# We use a variable instead of a secret here so that CI can run
# properly on forks.
envkey_SECRET_KEY: ${{ secrets.SECRET_KEY }}
some_other_variable: foobar
file_name: .env
Expand All @@ -45,3 +43,30 @@ jobs:
echo "$TEST"
exit 1
fi
- name: Cleanup
run: rm .env

- name: Test use GitHub Action multiline secret
uses: ./
with:
envkey_MULTILINE_SECRET: ${{ secrets.MULTILINE_SECRET }}

- name: Verify envfile
shell: bash
run: |
TEST=$(cat <<-END
MULTILINE_SECRET="line 1\nline 2"
END
)
if [ "$TEST" != "$(cat .env)" ]
then
echo "Actual:"
cat .env
echo "Expected:"
echo "$TEST"
exit 1
fi
- name: Cleanup
run: rm .env
2 changes: 0 additions & 2 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,3 @@ jobs:
envkey_SECRET_KEY: ''
fail_on_empty: true
continue-on-error: true


38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Create .Env File Github Action
# Create .Env File GitHub Action

[![GitHub
release](https://img.shields.io/github/release/SpicyPizza/create-envfile.svg?style=flat-square)](https://github.com/SpicyPizza/create-envfile/releases/latest)
Expand All @@ -8,8 +8,8 @@ marketplace](https://img.shields.io/badge/marketplace-create--env--file-blue?log

## About

A Github Action to create an '.env' file with Github Secrets. This is useful
when you are creating artifacts that contain values stored in Github Secrets.
A GitHub Action to create an '.env' file with GitHub Secrets. This is useful
when you are creating artifacts that contain values stored in GitHub Secrets.
This creates a file with variables that are defined in the Action config.

## Usage
Expand All @@ -31,7 +31,7 @@ jobs:

steps:
- name: Make envfile
uses: SpicyPizza/create-envfile@v1.3
uses: SpicyPizza/create-envfile@v2.0
with:
envkey_DEBUG: false
envkey_SOME_API_KEY: "123456abcdef"
Expand All @@ -51,13 +51,13 @@ the '.env' file:
| Name | Description |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `envkey_DEBUG`, `envkey_SOME_API_KEY` | These values can be whatever, and they will be added to the '.env' file as `DEBUG` and `SOME_API_KEY` . |
| `envkey_SECRET_KEY` | This one will use a secret stored in the repository's Github Secrets, and add it to the file as `SECRET_KEY` |
| `envkey_SECRET_KEY` | This one will use a secret stored in the repository's GitHub Secrets, and add it to the file as `SECRET_KEY` |
| `directory` (**Optional**) | This key will set the directory in which you want to create `env` file. **Important: cannot start with `/`. Action will fail if the specified directory doesn't exist.** |
| `file_name` (**Optional**) | Set the name of the output '.env' file. Defaults to `.env` |
| `fail_on_empty` (**Optional**) | If set to true, the Action will fail if any env key is empty. Default to `false`. |
| `sort_keys` (**Optional**) | If set to true, the Action will sort the keys in the output '.env' file. Default to `false`. |

Assuming that the Github Secret that was used is `password123`, the '.env' file
Assuming that the GitHub Secret that was used is `password123`, the '.env' file
that is created from the config above would contain:

```text
Expand All @@ -66,12 +66,36 @@ SOME_API_KEY="123456abcdef"
SECRET_KEY=password123
```

### Multiline Secrets

This Action supports multiline secrets, as described in [the nodejs dotenv
readme](https://github.com/motdotla/dotenv#multiline-values).

You may have a secret that requres multiple lines, like a private key. You can
store this in a GitHub Secret, and use it as any other secret in this Action:

```sh
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
...
Kh9NV...
...
-----END RSA PRIVATE KEY-----"
```

It will get stored as a single line in the '.env' file. This line will start and
end with a `"` character, and will contain `\n` characters to represent the
newlines:

```sh
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
```

## Potential Issues

### Warnings

When the Action runs, it will show `Warning: Unexpected input(s) ...`. This is
because Github is expecting all the potential input variables to be defined by
because GitHub is expecting all the potential input variables to be defined by
the Action's definition. You can read more about it in [this
issue](https://github.com/SpicyPizza/create-envfile/issues/10).

Expand Down
26 changes: 13 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: "Create .env file"
description: "Github Action to create a .env file with Github Secrets"
author: "Forest Anderson"
name: 'Create .env file'
description: 'GitHub Action to create a .env file with GitHub Secrets'
author: 'Forest Anderson'
branding:
icon: "briefcase"
color: "gray-dark"
icon: 'briefcase'
color: 'gray-dark'
inputs:
file_name:
description: "The filename for the envfile"
default: ".env"
description: 'The filename for the envfile'
default: '.env'
directory:
description: "The directory to put the envfile in"
default: ""
description: 'The directory to put the envfile in'
default: ''
fail_on_empty:
description: "Fail if an env key is an empty string"
default: "false"
description: 'Fail if an env key is an empty string'
default: 'false'
sort_keys:
description: "Sort the keys alphabetically"
default: "false"
description: 'Sort the keys alphabetically'
default: 'false'
runs:
using: 'node16'
main: 'dist/index.js'
13 changes: 12 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ async function run(): Promise<void> {
throw new Error(`Empty env key found: ${key}`)
}

outFile += `${key.split('INPUT_ENVKEY_')[1]}=${value}\n`
// If the value contains newlines, replace them with the string `\n` and
// add double quotes around the value.
//
// Reference from dotenv:
// https://github.com/motdotla/dotenv#multiline-values
if (value.includes('\n')) {
const new_value = `${key.split('INPUT_ENVKEY_')[1]}="${value.replace(
/\r?\n/g,
'\\n'
)}"\n`
outFile += new_value
} else {
outFile += `${key.split('INPUT_ENVKEY_')[1]}=${value}\n`
}
}
}

Expand Down

0 comments on commit 1e12712

Please sign in to comment.