From 7613685607e1ee7fa1377ffa07dd1d3f27885ab3 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Mon, 4 Nov 2024 18:16:54 -0600 Subject: [PATCH 01/15] removing debug, adding more outputs, stuff --- .github/workflows/tests.yaml | 6 +-- README.md | 64 ++++++++++++++++++++++--------- action.yaml | 19 +++++++--- scripts/yaml-to-env.sh | 73 +++++++++++++++++------------------- 4 files changed, 97 insertions(+), 65 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 73f2c9f..563104e 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -20,13 +20,13 @@ jobs: name: Test Action runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Execute action - uses: dcarbone/yaml-to-env-action@main + id: y2e + uses: ./ with: yaml-file: test-values.yaml - debug: 'true' - name: Test env run: | diff --git a/README.md b/README.md index 0685eb1..f445f5c 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,24 @@ [GitHub Action](https://docs.github.com/en/actions) that reads values from a YAML file, setting them into the `$GITHUB_ENV` of a job. -# Index - -1. [Example Workflow](#example-workflow) -2. [Conversion Rules](#conversion-rules) -3. [Inputs](#action-inputs) -4. [Outputs](#action-outputs) + +* [YAML to ENV GitHub Action](#yaml-to-env-github-action) + * [Example Workflow](#example-workflow) + * [Conversion Rules](#conversion-rules) + * [Names](#names) + * [Simple](#simple) + * [Nested Object](#nested-object) + * [Multiline value](#multiline-value) + * [Action Inputs](#action-inputs) + * [yaml-file](#yaml-file) + * [yq-version](#yq-version) + * [mask-vars](#mask-vars) + * [Action Outputs](#action-outputs) + * [yq-installed](#yq-installed) + * [var-count](#var-count) + * [yaml-keys](#yaml-keys) + * [env-names](#env-names) + ## Example Workflow @@ -26,23 +38,21 @@ on: type: string required: false description: "Version of yq to install, if not already" - default: "4.27.5" - debug: - type: boolean + mask-values: + type: string required: false - description: "Enable debug logging" - default: false + default: 'false' + description: 'If "true", masks all extracted values.' jobs: yaml-to-env: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set ENV values from YAML - uses: dcarbone/yaml-to-env-action@v1.0.0 + uses: dcarbone/yaml-to-env-action@v2 with: - debug: '${{ inputs.debug }}' yaml-file: '${{ inputs.yaml-file }}' yq-version: '${{ inputs.yq-version }}' @@ -62,7 +72,7 @@ Key to env name happens using the following script: tr '[:lower:]' '[:upper:]' | sed -E 's/[^a-zA-Z0-9_]/_/g'; ``` -You can see the exact logic [here](./scripts/yaml-to-env.sh#L9) +You can see the exact logic [here](./scripts/yaml-to-env.sh) #### Simple @@ -130,12 +140,12 @@ EOF description: "Version of yq to install, if not already in path. Tested with >= 4.25." ``` -#### debug +#### mask-vars ```yaml - debug: + mask-vars: required: false default: "false" - description: "Enable debug logging. This WILL print un-masked secrets to stdout, so use with caution." + description: "Add value masking to all exported envvars (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" ``` ## Action Outputs @@ -145,3 +155,21 @@ EOF yq-installed: description: "'true' if yq was installed by this action" ``` + +#### var-count +```yaml + var-count: + description: "Number of environment variables defined from source YAML file." +``` + +#### yaml-keys +```yaml + yaml-keys: + description: "Comma-separated string of keys extracted from source YAML file." +``` + +#### env-names +```yaml + env-names: + description: "Comma-separated string of exported environment variable names" +``` diff --git a/action.yaml b/action.yaml index 7650e7e..bae5759 100644 --- a/action.yaml +++ b/action.yaml @@ -12,17 +12,25 @@ inputs: description: "Filepath to YAML to parse" yq-version: required: false - default: "v4.27.5" description: "Version of yq to install, if not already in path. Tested with >= 4.25." - debug: + mask-vars: required: false default: "false" - description: "Enable debug logging. This WILL print un-masked secrets to stdout, so use with caution." + description: "Add value masking to all exported envvars (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" outputs: yq-installed: description: "'true' if yq was installed by this action" value: "${{ steps.install-yq.outputs.installed == 'false' }}" + var-count: + description: "Number of environment variables defined from source YAML file." + value: "${{ steps.y2e.outputs.var-count }}" + yaml-keys: + description: "Comma-separated string of keys extracted from source YAML file." + value: "${{ steps.y2e.outputs.yaml-keys }}" + env-names: + description: "Comma-separated string of exported environment variable names" + value: "${{ steps.y2e.outputs.env-names }}" runs: using: composite @@ -37,13 +45,14 @@ runs: - name: 'Install yq (${{ inputs.yq-version }})' id: install-yq - uses: dcarbone/install-yq-action@v1.0.0 + uses: dcarbone/install-yq-action@v1 with: version: '${{ inputs.yq-version }}' - name: YAML to ENV + id: y2e shell: bash run: $GITHUB_ACTION_PATH/scripts/yaml-to-env.sh env: - YAMLTOENV_DEBUG: '${{ inputs.debug }}' + YAMLTOENV_MASK_VARS: '${{ inputs.mask-vars }}' YAMLTOENV_YAML_FILE: '${{ inputs.yaml-file }}' diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 7346699..307afd5 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash -function _debug_log () { if [[ $YAMLTOENV_DEBUG == 'true' ]]; then printf '[DEBUG] - %s' "${@}"; echo ""; fi; } function _join_by { local IFS="$1"; shift; echo "$*"; } function _trim() { printf '%s' "${1}" | sed 's/^[[:blank:]]*//; s/[[:blank:]]*$//'; } function _ltrim_one { printf '%s' "${1}" | sed 's/^[[:blank:]]//'; } @@ -9,7 +8,6 @@ function _upper() { printf '%s' "${1}" | tr '[:lower:]' '[:upper:]'; } function _env_name() { _upper "${1}" | sed -E 's/[^a-zA-Z0-9_]/_/g'; } _write_env_file_line() { - _debug_log "${1} >> $GITHUB_ENV" printf '%s' "${1}" | sed -e 's/\\n/\n/g' >> $GITHUB_ENV printf '\n' >> $GITHUB_ENV } @@ -30,52 +28,49 @@ _write_kv_to_env_file() { fi } -_parse_yq_output() { - local _lines - local _line - local _split - local _key - local _value - local _clean_value +# sourced from https://stackoverflow.com/a/17841619 +_join_by() { + local IFS="$1" + shift + echo "$*" +} - _lines=() - readarray -t _lines <<< "${1}" +_yaml_keys=() +_env_names=() - _debug_log "${#_lines[@]} possible value(s) and / or comments seen in file" +_all_fields="$(yq -o p '.' "$YAMLTOENV_YAML_FILE")" +_parse_yq_output "${_all_fields}" - for _line in "${_lines[@]}"; do - _debug_log "_line='${_line}'" +_lines=() +readarray -t _lines <<< "${1}" - if [ -z "${_line}" ]; then - _debug_log "Skipping empty line" - continue - fi +for _line in "${_lines[@]}"; do + if [ -z "${_line}" ]; then + continue + fi - if [[ "${_line}" =~ ^# ]]; then - _debug_log "Skipping comment line \"${_line}\"" - continue - fi + if [[ "${_line}" =~ ^# ]]; then + continue + fi - _split=() - IFS=$'='; read -r -a _split <<< "${_line}" + _split=() + IFS=$'='; read -r -a _split <<< "${_line}" - for (( i=0; i < "${#_split[@]}"; i++ )); do - _debug_log "_split.${i}='${_split[$i]}'" - done + _key="$(_rtrim_one "${_split[0]}")" + _value="$(_join_by '=' "${_split[@]:1}")" + _clean_value="$(_ltrim_one "${_value}")" - _key="$(_rtrim_one "${_split[0]}")" - _value="$(_join_by '=' "${_split[@]:1}")" - _clean_value="$(_ltrim_one "${_value}")" + if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then + echo "::add-mask::${_clean_value}" + fi - _debug_log "_key='${_key}'" - _debug_log "_value='${_value}'" - _debug_log "_clean_value='${_clean_value}'" + _yaml_keys+=("${_key}") + _env_names+=("$(_env_name "${_key}")") - _write_kv_to_env_file "${_key}" "${_clean_value}" + _write_kv_to_env_file "${_key}" "${_clean_value}" - done -} +done -_debug_log "Writing to \"$GITHUB_ENV\":" -_all_fields="$(yq -o p '.' $YAMLTOENV_YAML_FILE)" -_parse_yq_output "${_all_fields}" +echo "var-count=${#_env_names[@]}" >> $GITHUB_OUTPUT +echo "yaml-keys=$(_join_by "," "${_yaml_keys[@]}")" >> $GITHUB_OUTPUT +echo "env-names=$(_join_by "," "${_env_names[@]}")" >> $GITHUB_OUTPUT \ No newline at end of file From 76e943ec9738df1ff95fb8e8f63202c6ddf2d628 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Mon, 4 Nov 2024 18:23:33 -0600 Subject: [PATCH 02/15] do the thing --- scripts/yaml-to-env.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 307afd5..79734e1 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -38,11 +38,8 @@ _join_by() { _yaml_keys=() _env_names=() -_all_fields="$(yq -o p '.' "$YAMLTOENV_YAML_FILE")" -_parse_yq_output "${_all_fields}" - _lines=() -readarray -t _lines <<< "${1}" +readarray -t _lines <<< "$(yq -o p '.' "$YAMLTOENV_YAML_FILE")" for _line in "${_lines[@]}"; do if [ -z "${_line}" ]; then From 79085f5ccb81bf073966b9e5a1003cae57ae930b Mon Sep 17 00:00:00 2001 From: Channing Ko-Madden Date: Tue, 5 Nov 2024 09:17:52 -0500 Subject: [PATCH 03/15] Test small change --- scripts/yaml-to-env.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 79734e1..6324c21 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -8,8 +8,8 @@ function _upper() { printf '%s' "${1}" | tr '[:lower:]' '[:upper:]'; } function _env_name() { _upper "${1}" | sed -E 's/[^a-zA-Z0-9_]/_/g'; } _write_env_file_line() { - printf '%s' "${1}" | sed -e 's/\\n/\n/g' >> $GITHUB_ENV - printf '\n' >> $GITHUB_ENV + printf '%s' "${1}" | sed -e 's/\\n/\n/g' >> "$GITHUB_ENV" + printf '\n' >> "$GITHUB_ENV" } _write_kv_to_env_file() { @@ -58,7 +58,7 @@ for _line in "${_lines[@]}"; do _clean_value="$(_ltrim_one "${_value}")" if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then - echo "::add-mask::${_clean_value}" + echo "::add-mask::$_clean_value" fi _yaml_keys+=("${_key}") @@ -68,6 +68,6 @@ for _line in "${_lines[@]}"; do done -echo "var-count=${#_env_names[@]}" >> $GITHUB_OUTPUT -echo "yaml-keys=$(_join_by "," "${_yaml_keys[@]}")" >> $GITHUB_OUTPUT -echo "env-names=$(_join_by "," "${_env_names[@]}")" >> $GITHUB_OUTPUT \ No newline at end of file +echo "var-count=${#_env_names[@]}" >> "$GITHUB_OUTPUT" +echo "yaml-keys=$(_join_by "," "${_yaml_keys[@]}")" >> "$GITHUB_OUTPUT" +echo "env-names=$(_join_by "," "${_env_names[@]}")" >> "$GITHUB_OUTPUT" From f94b86771491982c3e5dfd80258789c693d3c0c7 Mon Sep 17 00:00:00 2001 From: Channing Ko-Madden Date: Tue, 5 Nov 2024 09:53:16 -0500 Subject: [PATCH 04/15] Test mask multi-line string --- scripts/yaml-to-env.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 6324c21..3ca28d1 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -58,7 +58,8 @@ for _line in "${_lines[@]}"; do _clean_value="$(_ltrim_one "${_value}")" if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then - echo "::add-mask::$_clean_value" + # sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0 + echo "::add-mask::$(echo "$_clean_value" | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g') fi _yaml_keys+=("${_key}") From 172e7c3ff92d6bb0dab05c93340a6de2a80d0698 Mon Sep 17 00:00:00 2001 From: Channing Ko-Madden Date: Tue, 5 Nov 2024 10:08:49 -0500 Subject: [PATCH 05/15] Fix missing " --- scripts/yaml-to-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 3ca28d1..a39eaa7 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -59,7 +59,7 @@ for _line in "${_lines[@]}"; do if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then # sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0 - echo "::add-mask::$(echo "$_clean_value" | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g') + echo "::add-mask::$(echo "$_clean_value" | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')" fi _yaml_keys+=("${_key}") From 33256c67298f0076aea51da5645f6ca6fbc55ed2 Mon Sep 17 00:00:00 2001 From: Channing Ko-Madden Date: Tue, 5 Nov 2024 11:17:01 -0500 Subject: [PATCH 06/15] Final test, I don't think value are matching --- scripts/yaml-to-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index a39eaa7..52f6578 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -59,7 +59,7 @@ for _line in "${_lines[@]}"; do if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then # sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0 - echo "::add-mask::$(echo "$_clean_value" | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')" + echo "::add-mask::$(echo "$_clean_value" | sed -e 's/\\n/\n/g' | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')" fi _yaml_keys+=("${_key}") From 1944951aa6f7d4b8e3942ae966d0ffa125052917 Mon Sep 17 00:00:00 2001 From: Channing Ko-Madden Date: Tue, 5 Nov 2024 11:53:15 -0500 Subject: [PATCH 07/15] Add mask-vars to test --- .github/workflows/tests.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 563104e..ea1ee48 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -27,6 +27,7 @@ jobs: uses: ./ with: yaml-file: test-values.yaml + mask-vars: true - name: Test env run: | From 11dd26ae2ce84494bd68808933be5ae11a17cc3a Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 20:50:04 -0600 Subject: [PATCH 08/15] fixes --- .github/workflows/example.yaml | 13 ++++++------- README.md | 6 +++--- action.yaml | 6 +++--- scripts/yaml-to-env.sh | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.github/workflows/example.yaml b/.github/workflows/example.yaml index c36d6c6..f765f13 100644 --- a/.github/workflows/example.yaml +++ b/.github/workflows/example.yaml @@ -12,12 +12,11 @@ on: type: string required: false description: "Version of yq to install, if not already" - default: "4.27.5" - debug: - type: boolean + mask-values: + type: string required: false - description: "Enable debug logging" - default: false + default: "false" + description: "Add value masking to all exported environment variable values (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" jobs: yaml-to-env: @@ -26,11 +25,11 @@ jobs: - uses: actions/checkout@v3 - name: Set ENV values from YAML - uses: dcarbone/yaml-to-env-action@v1.0.0 + uses: dcarbone/yaml-to-env-action@v1 with: - debug: '${{ inputs.debug }}' yaml-file: '${{ inputs.yaml-file }}' yq-version: '${{ inputs.yq-version }}' + mask-vars: '${{ inputs.mask-values }}' - name: Print environment variables run: | diff --git a/README.md b/README.md index f445f5c..f122cec 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ * [Action Inputs](#action-inputs) * [yaml-file](#yaml-file) * [yq-version](#yq-version) - * [mask-vars](#mask-vars) + * [mask-values](#mask-values) * [Action Outputs](#action-outputs) * [yq-installed](#yq-installed) * [var-count](#var-count) @@ -140,9 +140,9 @@ EOF description: "Version of yq to install, if not already in path. Tested with >= 4.25." ``` -#### mask-vars +#### mask-values ```yaml - mask-vars: + mask-values: required: false default: "false" description: "Add value masking to all exported envvars (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" diff --git a/action.yaml b/action.yaml index bae5759..c1b3172 100644 --- a/action.yaml +++ b/action.yaml @@ -13,10 +13,10 @@ inputs: yq-version: required: false description: "Version of yq to install, if not already in path. Tested with >= 4.25." - mask-vars: + mask-values: required: false default: "false" - description: "Add value masking to all exported envvars (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" + description: "Add value masking to all exported environment variable values (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" outputs: yq-installed: @@ -54,5 +54,5 @@ runs: shell: bash run: $GITHUB_ACTION_PATH/scripts/yaml-to-env.sh env: - YAMLTOENV_MASK_VARS: '${{ inputs.mask-vars }}' + YAMLTOENV_MASK_VALUES: '${{ inputs.mask-values }}' YAMLTOENV_YAML_FILE: '${{ inputs.yaml-file }}' diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 79734e1..f6cb9b5 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -57,7 +57,7 @@ for _line in "${_lines[@]}"; do _value="$(_join_by '=' "${_split[@]:1}")" _clean_value="$(_ltrim_one "${_value}")" - if [[ "${YAMLTOENV_MASK_VARS}" == 'true' ]]; then + if [[ "${YAMLTOENV_MASK_VALUES}" == 'true' ]]; then echo "::add-mask::${_clean_value}" fi From d1eeea199c194fe33de3001e893cd67d8ea1f880 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 20:50:29 -0600 Subject: [PATCH 09/15] fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f122cec..4eca21d 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ EOF mask-values: required: false default: "false" - description: "Add value masking to all exported envvars (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" + description: "Add value masking to all exported environment variable values (https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-masking-an-environment-variable)" ``` ## Action Outputs From b412b623cd21cd619311c2a4b0b624f75c42ef38 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 20:51:03 -0600 Subject: [PATCH 10/15] all pull requests --- .github/workflows/tests.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 563104e..8e843a4 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,8 +12,6 @@ on: - 'test-values.yaml' pull_request: - branches: - - main jobs: test: From 4fce2e22aa562d0463e3e56e6c8d5187f3dd39c7 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 20:53:00 -0600 Subject: [PATCH 11/15] fix --- .github/workflows/tests.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 8e843a4..b8a677e 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -25,6 +25,7 @@ jobs: uses: ./ with: yaml-file: test-values.yaml + mask-values: 'true' - name: Test env run: | From b8b5a41fa084480b9b06f978faa7e7bb211b8d9f Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 21:10:48 -0600 Subject: [PATCH 12/15] cleanup --- scripts/yaml-to-env.sh | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index 3bbb096..b09f67f 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash +# sourced from https://stackoverflow.com/a/17841619 function _join_by { local IFS="$1"; shift; echo "$*"; } + function _trim() { printf '%s' "${1}" | sed 's/^[[:blank:]]*//; s/[[:blank:]]*$//'; } function _ltrim_one { printf '%s' "${1}" | sed 's/^[[:blank:]]//'; } function _rtrim_one { printf '%s' "${1}" | sed 's/[[:blank:]]$//'; } @@ -28,13 +30,6 @@ _write_kv_to_env_file() { fi } -# sourced from https://stackoverflow.com/a/17841619 -_join_by() { - local IFS="$1" - shift - echo "$*" -} - _yaml_keys=() _env_names=() @@ -59,7 +54,8 @@ for _line in "${_lines[@]}"; do if [[ "${YAMLTOENV_MASK_VALUES}" == 'true' ]]; then # sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0 - echo "::add-mask::$(echo "$_clean_value" | sed -e 's/\\n/\n/g' | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')" + echo "::add-mask::$(echo "$_clean_value" \ + | sed -e 's/\\n/\n/g' -e ':a;N;$!ba;s/%/%25/g' -e ':a;N;$!ba;s/\r/%0D/g' -e ':a;N;$!ba;s/\n/%0A/g')" fi _yaml_keys+=("${_key}") From 705a58419c135cc9828208c045dcead8404b780c Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 21:11:59 -0600 Subject: [PATCH 13/15] hmm --- scripts/yaml-to-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/yaml-to-env.sh b/scripts/yaml-to-env.sh index b09f67f..9963d8b 100755 --- a/scripts/yaml-to-env.sh +++ b/scripts/yaml-to-env.sh @@ -55,7 +55,7 @@ for _line in "${_lines[@]}"; do if [[ "${YAMLTOENV_MASK_VALUES}" == 'true' ]]; then # sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0 echo "::add-mask::$(echo "$_clean_value" \ - | sed -e 's/\\n/\n/g' -e ':a;N;$!ba;s/%/%25/g' -e ':a;N;$!ba;s/\r/%0D/g' -e ':a;N;$!ba;s/\n/%0A/g')" + | sed -e 's/\\n/\n/g' | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')" fi _yaml_keys+=("${_key}") From 7e51df9c1a21777b0f7d98d23a0ad91ed6560daf Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 21:17:15 -0600 Subject: [PATCH 14/15] more clenaup --- README.md | 39 +------------------ .../workflows/example.yaml => example.yaml | 4 +- 2 files changed, 3 insertions(+), 40 deletions(-) rename .github/workflows/example.yaml => example.yaml (91%) diff --git a/README.md b/README.md index 4eca21d..8732380 100644 --- a/README.md +++ b/README.md @@ -23,44 +23,7 @@ ## Example Workflow -```yaml -name: YAML to Env Example Workflow - -on: - workflow_dispatch: - inputs: - yaml-file: - type: string - required: false - description: "Path to YAML file to parse" - default: "test-values.yaml" - yq-version: - type: string - required: false - description: "Version of yq to install, if not already" - mask-values: - type: string - required: false - default: 'false' - description: 'If "true", masks all extracted values.' - -jobs: - yaml-to-env: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Set ENV values from YAML - uses: dcarbone/yaml-to-env-action@v2 - with: - yaml-file: '${{ inputs.yaml-file }}' - yq-version: '${{ inputs.yq-version }}' - - - name: Print environment variables - run: | - printenv - -``` +You can see an example workflow here: [example.yaml](./example.yaml) ## Conversion Rules diff --git a/.github/workflows/example.yaml b/example.yaml similarity index 91% rename from .github/workflows/example.yaml rename to example.yaml index f765f13..5f2323d 100644 --- a/.github/workflows/example.yaml +++ b/example.yaml @@ -25,11 +25,11 @@ jobs: - uses: actions/checkout@v3 - name: Set ENV values from YAML - uses: dcarbone/yaml-to-env-action@v1 + uses: dcarbone/yaml-to-env-action@v2 with: yaml-file: '${{ inputs.yaml-file }}' yq-version: '${{ inputs.yq-version }}' - mask-vars: '${{ inputs.mask-values }}' + mask-values: '${{ inputs.mask-values }}' - name: Print environment variables run: | From 6827fd638cfee00c326a9f691971300c52531511 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 5 Nov 2024 21:17:30 -0600 Subject: [PATCH 15/15] latest checkout --- example.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.yaml b/example.yaml index 5f2323d..c876520 100644 --- a/example.yaml +++ b/example.yaml @@ -22,7 +22,7 @@ jobs: yaml-to-env: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set ENV values from YAML uses: dcarbone/yaml-to-env-action@v2