Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing debug, adding more outputs, stuff #3

Merged
merged 18 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
64 changes: 46 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- TOC -->
* [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)
<!-- TOC -->

## Example Workflow

Expand All @@ -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:
dcarbone marked this conversation as resolved.
Show resolved Hide resolved
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 }}'

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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"
```
19 changes: 14 additions & 5 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}'
72 changes: 32 additions & 40 deletions scripts/yaml-to-env.sh
Original file line number Diff line number Diff line change
@@ -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:]]//'; }
Expand All @@ -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
}
Expand All @@ -30,52 +28,46 @@ _write_kv_to_env_file() {
fi
}

_parse_yq_output() {
local _lines
local _line
local _split
local _key
local _value
local _clean_value

_lines=()
readarray -t _lines <<< "${1}"
# sourced from https://stackoverflow.com/a/17841619
_join_by() {
local IFS="$1"
shift
echo "$*"
}

_debug_log "${#_lines[@]} possible value(s) and / or comments seen in file"
_yaml_keys=()
_env_names=()

for _line in "${_lines[@]}"; do
_debug_log "_line='${_line}'"
_lines=()
readarray -t _lines <<< "$(yq -o p '.' "$YAMLTOENV_YAML_FILE")"

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