diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c1560f..4790b4d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 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: 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)