Skip to content

Commit 1e12712

Browse files
authored
Merge pull request #61 from AngelOnFira/add-multiline-secrets
2 parents 9228ab2 + 9aeaba5 commit 1e12712

File tree

7 files changed

+99
-28
lines changed

7 files changed

+99
-28
lines changed

.github/workflows/test-main.yml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v3
1919

20-
- name: Make envfile
20+
- name: Test use GitHub Action secret
2121
uses: ./
2222
with:
2323
envkey_DEBUG: false
2424
envkey_SOME_API_KEY: '123456abcdef'
25-
# We use a variable instead of a secret here so that CI can run
26-
# properly on forks.
2725
envkey_SECRET_KEY: ${{ secrets.SECRET_KEY }}
2826
some_other_variable: foobar
2927
file_name: .env
@@ -45,3 +43,30 @@ jobs:
4543
echo "$TEST"
4644
exit 1
4745
fi
46+
47+
- name: Cleanup
48+
run: rm .env
49+
50+
- name: Test use GitHub Action multiline secret
51+
uses: ./
52+
with:
53+
envkey_MULTILINE_SECRET: ${{ secrets.MULTILINE_SECRET }}
54+
55+
- name: Verify envfile
56+
shell: bash
57+
run: |
58+
TEST=$(cat <<-END
59+
MULTILINE_SECRET="line 1\nline 2"
60+
END
61+
)
62+
if [ "$TEST" != "$(cat .env)" ]
63+
then
64+
echo "Actual:"
65+
cat .env
66+
echo "Expected:"
67+
echo "$TEST"
68+
exit 1
69+
fi
70+
71+
- name: Cleanup
72+
run: rm .env

.github/workflows/test-pr.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,3 @@ jobs:
202202
envkey_SECRET_KEY: ''
203203
fail_on_empty: true
204204
continue-on-error: true
205-
206-

README.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Create .Env File Github Action
1+
# Create .Env File GitHub Action
22

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

99
## About
1010

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

1515
## Usage
@@ -31,7 +31,7 @@ jobs:
3131

3232
steps:
3333
- name: Make envfile
34-
uses: SpicyPizza/create-envfile@v1.3
34+
uses: SpicyPizza/create-envfile@v2.0
3535
with:
3636
envkey_DEBUG: false
3737
envkey_SOME_API_KEY: "123456abcdef"
@@ -51,13 +51,13 @@ the '.env' file:
5151
| Name | Description |
5252
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
5353
| `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` . |
54-
| `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` |
54+
| `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` |
5555
| `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.** |
5656
| `file_name` (**Optional**) | Set the name of the output '.env' file. Defaults to `.env` |
5757
| `fail_on_empty` (**Optional**) | If set to true, the Action will fail if any env key is empty. Default to `false`. |
5858
| `sort_keys` (**Optional**) | If set to true, the Action will sort the keys in the output '.env' file. Default to `false`. |
5959

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

6363
```text
@@ -66,12 +66,36 @@ SOME_API_KEY="123456abcdef"
6666
SECRET_KEY=password123
6767
```
6868

69+
### Multiline Secrets
70+
71+
This Action supports multiline secrets, as described in [the nodejs dotenv
72+
readme](https://github.com/motdotla/dotenv#multiline-values).
73+
74+
You may have a secret that requres multiple lines, like a private key. You can
75+
store this in a GitHub Secret, and use it as any other secret in this Action:
76+
77+
```sh
78+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
79+
...
80+
Kh9NV...
81+
...
82+
-----END RSA PRIVATE KEY-----"
83+
```
84+
85+
It will get stored as a single line in the '.env' file. This line will start and
86+
end with a `"` character, and will contain `\n` characters to represent the
87+
newlines:
88+
89+
```sh
90+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
91+
```
92+
6993
## Potential Issues
7094

7195
### Warnings
7296

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

action.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
name: "Create .env file"
2-
description: "Github Action to create a .env file with Github Secrets"
3-
author: "Forest Anderson"
1+
name: 'Create .env file'
2+
description: 'GitHub Action to create a .env file with GitHub Secrets'
3+
author: 'Forest Anderson'
44
branding:
5-
icon: "briefcase"
6-
color: "gray-dark"
5+
icon: 'briefcase'
6+
color: 'gray-dark'
77
inputs:
88
file_name:
9-
description: "The filename for the envfile"
10-
default: ".env"
9+
description: 'The filename for the envfile'
10+
default: '.env'
1111
directory:
12-
description: "The directory to put the envfile in"
13-
default: ""
12+
description: 'The directory to put the envfile in'
13+
default: ''
1414
fail_on_empty:
15-
description: "Fail if an env key is an empty string"
16-
default: "false"
15+
description: 'Fail if an env key is an empty string'
16+
default: 'false'
1717
sort_keys:
18-
description: "Sort the keys alphabetically"
19-
default: "false"
18+
description: 'Sort the keys alphabetically'
19+
default: 'false'
2020
runs:
2121
using: 'node16'
2222
main: 'dist/index.js'

dist/index.js

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ async function run(): Promise<void> {
2121
throw new Error(`Empty env key found: ${key}`)
2222
}
2323

24-
outFile += `${key.split('INPUT_ENVKEY_')[1]}=${value}\n`
24+
// If the value contains newlines, replace them with the string `\n` and
25+
// add double quotes around the value.
26+
//
27+
// Reference from dotenv:
28+
// https://github.com/motdotla/dotenv#multiline-values
29+
if (value.includes('\n')) {
30+
const new_value = `${key.split('INPUT_ENVKEY_')[1]}="${value.replace(
31+
/\r?\n/g,
32+
'\\n'
33+
)}"\n`
34+
outFile += new_value
35+
} else {
36+
outFile += `${key.split('INPUT_ENVKEY_')[1]}=${value}\n`
37+
}
2538
}
2639
}
2740

0 commit comments

Comments
 (0)