Skip to content

Commit

Permalink
Merge pull request #44 from SpicyPizza/fix-empty-envkey-backwards-inc…
Browse files Browse the repository at this point in the history
…ompatability

Added input to toggle failing on empty env key
  • Loading branch information
AngelOnFira authored Feb 16, 2022
2 parents a8e32e1 + 5cbd3d9 commit 041383d
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 146 deletions.
293 changes: 164 additions & 129 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,172 +1,207 @@
name: Test action

on:
push:
on: [ push ]

jobs:
test-general:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
envkey_SOME_API_KEY: "123456abcdef"
envkey_SECRET_KEY: ${{ secrets.SECRET_KEY }}
some_other_variable: foobar
file_name: .env

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
SECRET_KEY=password123
SOME_API_KEY=123456abcdef
END
)
if [ "$TEST" != "$(cat .env)" ]
then
echo "ERR"
fi
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
envkey_SOME_API_KEY: "123456abcdef"
envkey_SECRET_KEY: ${{ secrets.SECRET_KEY }}
some_other_variable: foobar
file_name: .env

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
SECRET_KEY=password123
SOME_API_KEY=123456abcdef
END
)
if [ "$TEST" != "$(cat .env)" ]
then
echo "ERR"
fi
test-filename:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
file_name: .other-file

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat .other-file)" ]
then
echo "ERR"
fi
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
file_name: .other-file

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat .other-file)" ]
then
echo "ERR"
fi
test-relative-path-above:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: ../

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat ../.env)" ]
then
echo "ERR"
fi
- uses: actions/checkout@v2

- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: ../

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat ../.env)" ]
then
echo "ERR"
fi
test-relative-path-subdirectory-1:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: subdir

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat subdir/.env)" ]
then
echo "ERR"
fi
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: subdir

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat subdir/.env)" ]
then
echo "ERR"
fi
test-relative-path-subdirectory-2:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: ./subdir

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat subdir/.env)" ]
then
echo "ERR"
fi
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: ./subdir

- name: Verify envfile
run: |
TEST=$(cat <<-END
DEBUG=false
END
)
if [ "$TEST" != "$(cat subdir/.env)" ]
then
echo "ERR"
fi
should-fail-test-absolute-path:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: /home
- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_DEBUG: false
directory: /home

should-fail-test-bad-secret:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
fail_on_empty: true
envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }}

# Test empty envkeys
should-fail-test-empty-envkey:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }}
- name: Make envfile
uses: ./
with:
envkey_SECRET_KEY: ""
fail_on_empty: true

test-empty-envkey-default-option:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Create folder
run: |
mkdir -p subdir
- name: Make envfile
uses: ./
with:
envkey_SECRET_KEY: ""

- name: Verify envfile
run: |
TEST=$(cat <<-END
SECRET_KEY=
END
)
if [ "$TEST" != "$(cat .env)" ]
then
echo "ERR"
fi
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
some_other_variable: foobar
directory: <directory_name>
file_name: .env
fail_on_empty: false
```
## Inputs
Expand All @@ -52,6 +53,7 @@ the '.env' file:
| `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`. |

Assuming that the Github Secret that was used is `password123`, the '.env' file
that is created from the config above would contain:
Expand Down
35 changes: 19 additions & 16 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
name: 'Create .env file'
description: 'Github Action to create a .env file with Github Secrets'
author: 'Forest Anderson'
branding:
icon: 'briefcase'
color: 'gray-dark'
inputs:
file_name:
description: 'The filename for the envfile'
default: '.env'
directory:
description: 'The directory to put the envfile in'
default: ''
runs:
using: 'docker'
image: 'Dockerfile'
name: "Create .env file"
description: "Github Action to create a .env file with Github Secrets"
author: "Forest Anderson"
branding:
icon: "briefcase"
color: "gray-dark"
inputs:
file_name:
description: "The filename for the envfile"
default: ".env"
directory:
description: "The directory to put the envfile in"
default: ""
fail_on_empty:
description: "Fail if an env key is an empty string"
default: "false"
runs:
using: "docker"
image: "Dockerfile"
2 changes: 1 addition & 1 deletion src/create-envfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
value = os.getenv(key, "")

# If the key is empty, throw an error.
if value == "":
if value == "" and os.getenv("INPUT_FAIL_ON_EMPTY", "false") == "true":
raise Exception(f"Empty env key found: {key}")

out_file += "{}={}\n".format(key.split("INPUT_ENVKEY_")[1], value)
Expand Down

0 comments on commit 041383d

Please sign in to comment.