Skip to content

Commit

Permalink
Merge pull request #60 from AngelOnFira/revert-sort-keys-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelOnFira authored Jun 17, 2023
2 parents 6c8b7d4 + cdd0137 commit 9228ab2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 23 deletions.
73 changes: 53 additions & 20 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,38 +124,47 @@ jobs:
run: |
rm -rf subdir
- name: Test should fail absolute path
- name: Test empty envkey default option
uses: ./
with:
envkey_DEBUG: false
directory: /home
continue-on-error: true
envkey_SECRET_KEY: ''

- name: Test should fail bad secret
uses: ./
with:
fail_on_empty: true
envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }}
continue-on-error: true
- name: Verify envfile
shell: bash
run: |
TEST=$(cat <<-END
SECRET_KEY=
END
)
if [ "$TEST" != "$(cat .env)" ]
then
echo "Actual:"
cat .env
echo "Expected:"
echo "$TEST"
exit 1
fi
- name: Test should fail empty envkey
uses: ./
id: make_envfile
with:
envkey_SECRET_KEY: ''
fail_on_empty: true
continue-on-error: true
- name: Cleanup
shell: bash
run: |
rm .env
- name: Test empty envkey default option
- name: Test sorted envkeys works
uses: ./
with:
envkey_SECRET_KEY: ''
envkey_C: 'C'
envkey_A: 'A'
envkey_B: 'B'
sort_keys: true

- name: Verify envfile
shell: bash
run: |
TEST=$(cat <<-END
SECRET_KEY=
A=A
B=B
C=C
END
)
if [ "$TEST" != "$(cat .env)" ]
Expand All @@ -171,3 +180,27 @@ jobs:
shell: bash
run: |
rm .env
- name: Test should fail absolute path
uses: ./
with:
envkey_DEBUG: false
directory: /home
continue-on-error: true

- name: Test should fail bad secret
uses: ./
with:
fail_on_empty: true
envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }}
continue-on-error: true

- name: Test should fail empty envkey
uses: ./
id: make_envfile
with:
envkey_SECRET_KEY: ''
fail_on_empty: true
continue-on-error: true


2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
directory: <directory_name>
file_name: .env
fail_on_empty: false
sort_keys: false
```
## Inputs
Expand All @@ -54,6 +55,7 @@ the '.env' file:
| `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
that is created from the config above would contain:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
fail_on_empty:
description: "Fail if an env key is an empty string"
default: "false"
sort_keys:
description: "Sort the keys alphabetically"
default: "false"
runs:
using: 'node16'
main: 'dist/index.js'
8 changes: 7 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.

7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import * as path from 'path'

async function run(): Promise<void> {
try {
const envKeys = Object.keys(process.env).sort((a, b) => a.localeCompare(b))
let envKeys
if (core.getInput('sort_keys') === 'true') {
envKeys = Object.keys(process.env).sort((a, b) => a.localeCompare(b))
} else {
envKeys = Object.keys(process.env)
}

let outFile = ''

Expand Down

0 comments on commit 9228ab2

Please sign in to comment.