From bb4fa16b34e20a29e1185935393f5957f917a6d8 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 16 Feb 2022 14:13:51 -0500 Subject: [PATCH 1/4] Added input to toggle failing on empty env key --- .github/workflows/test.yml | 45 ++++++++++++++++++++++++++++++++++++++ action.yml | 35 +++++++++++++++-------------- src/create-envfile.py | 2 +- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c1560f..915b483 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -170,3 +170,48 @@ jobs: uses: ./ with: envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }} + + # Test empty envkeys + should-fail-test-empty-envkey: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create folder + run: | + mkdir -p subdir + + - 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 \ No newline at end of file diff --git a/action.yml b/action.yml index 3b3341f..31cffdc 100644 --- a/action.yml +++ b/action.yml @@ -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' \ No newline at end of file +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" diff --git a/src/create-envfile.py b/src/create-envfile.py index 59954f8..5a5bb96 100755 --- a/src/create-envfile.py +++ b/src/create-envfile.py @@ -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) From 1d48d8a6dd0ac0a9111f1bcb0e164154e219e732 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 16 Feb 2022 14:17:33 -0500 Subject: [PATCH 2/4] Updated readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6cb8730..e2ca33a 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ jobs: some_other_variable: foobar directory: file_name: .env + fail_on_empty: false ``` ## Inputs @@ -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: From be1d2fee382a8d24f7ca9bd227380995a98ee8f9 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 16 Feb 2022 14:18:08 -0500 Subject: [PATCH 3/4] Fixed test to fail properly --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 915b483..3cfb3c4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -169,6 +169,7 @@ jobs: - name: Make envfile uses: ./ with: + fail_on_empty: true envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }} # Test empty envkeys From 5cbd3d9d1bcd3826e5e3b27f6087763133249c33 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 16 Feb 2022 14:20:18 -0500 Subject: [PATCH 4/4] Formatting tests --- .github/workflows/test.yml | 315 ++++++++++++++++++------------------- 1 file changed, 152 insertions(+), 163 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3cfb3c4..4790b4d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,218 +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 - - - name: Create folder - run: | - mkdir -p subdir + - uses: actions/checkout@v2 - - 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 + - uses: actions/checkout@v2 - - name: Create folder - run: | - mkdir -p subdir + - name: Create folder + run: | + mkdir -p subdir - - name: Make envfile - uses: ./ - with: - fail_on_empty: true - envkey_SECRET_KEY: ${{ secrets.NON_EXISTENT_SECRET }} + - 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: "" - fail_on_empty: true + - 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 \ No newline at end of file + - 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