Skip to content

Commit

Permalink
Merge pull request #3 from dcarbone/dcarbone/updates
Browse files Browse the repository at this point in the history
removing debug, adding more outputs, stuff
  • Loading branch information
dcarbone authored Nov 6, 2024
2 parents a38149b + 6827fd6 commit 9163614
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 111 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ on:
- 'test-values.yaml'

pull_request:
branches:
- main

jobs:
test:
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'
mask-values: "true"

- name: Test env
run: |
Expand Down
91 changes: 41 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,28 @@

[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-values](#mask-values)
* [Action Outputs](#action-outputs)
* [yq-installed](#yq-installed)
* [var-count](#var-count)
* [yaml-keys](#yaml-keys)
* [env-names](#env-names)
<!-- TOC -->

## 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"
default: "4.27.5"
debug:
type: boolean
required: false
description: "Enable debug logging"
default: false

jobs:
yaml-to-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set ENV values from YAML
uses: dcarbone/yaml-to-env-action@v1.0.0
with:
debug: '${{ inputs.debug }}'
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

Expand All @@ -62,7 +35,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 +103,12 @@ EOF
description: "Version of yq to install, if not already in path. Tested with >= 4.25."
```
#### debug
#### mask-values
```yaml
debug:
mask-values:
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 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
Expand All @@ -145,3 +118,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-values:
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 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:
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_VALUES: '${{ inputs.mask-values }}'
YAMLTOENV_YAML_FILE: '${{ inputs.yaml-file }}'
15 changes: 7 additions & 8 deletions .github/workflows/example.yaml → example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@ 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:
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 }}'
mask-values: '${{ inputs.mask-values }}'

- name: Print environment variables
run: |
Expand Down
75 changes: 32 additions & 43 deletions scripts/yaml-to-env.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env bash

function _debug_log () { if [[ $YAMLTOENV_DEBUG == 'true' ]]; then printf '[DEBUG] - %s' "${@}"; echo ""; fi; }
# 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:]]$//'; }
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
printf '%s' "${1}" | sed -e 's/\\n/\n/g' >> "$GITHUB_ENV"
printf '\n' >> "$GITHUB_ENV"
}

_write_kv_to_env_file() {
Expand All @@ -30,52 +30,41 @@ _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}"
_yaml_keys=()
_env_names=()

_debug_log "${#_lines[@]} possible value(s) and / or comments seen in file"
_lines=()
readarray -t _lines <<< "$(yq -o p '.' "$YAMLTOENV_YAML_FILE")"

for _line in "${_lines[@]}"; do
_debug_log "_line='${_line}'"

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_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')"
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"

0 comments on commit 9163614

Please sign in to comment.