From f6242d36190af61b72a4bf6defe90a7d96485d30 Mon Sep 17 00:00:00 2001 From: Ross Anderson Date: Wed, 22 Dec 2021 18:50:37 -0800 Subject: [PATCH] Add multiline support --- README.md | 33 +++++++++++++++++++++++++++++++++ action.yml | 5 +++++ entrypoint.sh | 10 ++++++++++ 3 files changed, 48 insertions(+) diff --git a/README.md b/README.md index 5213463..23cd952 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,15 @@ 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`. + +If `multiline: true`, multiple lines of output will be captured. Useful +for capturing lists. + +If `multiline: false`, only the first line of the output will be +captured. The rest will be written to stdout. + ## Outputs ### `value` @@ -37,3 +46,27 @@ 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)"