diff --git a/README.md b/README.md index 5213463..e8388c7 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,14 @@ Run jq on your data and get result as output ### `cmd` **Required** This is the actual command that will be passed along +### `multiline` +Optional. Default `false`. + +| Value | Behavior | +| - | - | +| true | Multiple lines of output will be captured. Useful for capturing lists. | +| false | Only the first line of the output will be captured. The rest will be written to stdout. | + ## Outputs ### `value` @@ -37,3 +45,26 @@ jobs: - name: Show my version run: 'echo "version ${{ steps.version.outputs.value }}"' ``` + +## Using multiline output + +```yaml +jobs: + build: + runs-on: ubuntu-latest + steps: + + - name: Extract all keywords from package.json + uses: sergeysova/jq-action@v2 + id: keywords + with: + cmd: 'jq .keywords[] package.json -r' + multiline: true + + - name: Show keywords + run: | + keywords="${{ steps.keywords.outputs.value }}" + for keyword in $keywords; do + echo "$keyword" + done +``` diff --git a/action.yml b/action.yml index e6b82d0..37ac150 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,11 @@ inputs: cmd: description: 'jq command with arguments' required: true + multiline: + description: 'If true, support multiline output' + required: false + type: boolean + default: false outputs: value: description: 'What jq is outputs' diff --git a/entrypoint.sh b/entrypoint.sh index ae6719d..c544074 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,14 @@ #!/bin/bash set -e +OUTPUT=$(eval $INPUT_CMD) + +# Multiline string handling, per Github Community recommendation: +# https://github.community/t/set-output-truncates-multiline-strings/16852/3 +if ($INPUT_MULTILINE); then + OUTPUT="${OUTPUT//'%'/'%25'}" + OUTPUT="${OUTPUT//$'\n'/'%0A'}" + OUTPUT="${OUTPUT//$'\r'/'%0D'}" +fi + echo "::set-output name=value::$(eval $INPUT_CMD)"