From 619f07f85c9015e0fa1838f2f94fd41c611642d6 Mon Sep 17 00:00:00 2001 From: Julius Gamanyi <85777+juliusgb@users.noreply.github.com> Date: Thu, 29 Jun 2023 08:23:16 +0200 Subject: [PATCH] docs(ghactions, syntax fixes): multiline string to output (#33) --- github-actions/multistring-in-outputs-var.md | 85 +++++++++++++++++++ .../print-github-webhook-payload.md | 10 +-- jq/access-array-index-in-loop.md | 2 +- 3 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 github-actions/multistring-in-outputs-var.md diff --git a/github-actions/multistring-in-outputs-var.md b/github-actions/multistring-in-outputs-var.md new file mode 100644 index 0000000..dfba0e9 --- /dev/null +++ b/github-actions/multistring-in-outputs-var.md @@ -0,0 +1,85 @@ +# GitHub Actions - save multiline string to output + +Trying to write a multi-line string in GitHub Actions output variable `$GITHUB_OUTPUT` returns the error, `Unable to process file command 'output' successfully.` + +[GitHub Actions documentation](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings) shows us how to do that. +But, I didn't understand it till I came across this [stackoverflow answer](https://stackoverflow.com/a/74232400) that I reproduce below. + +Let the variable `jsonData` hold example json (from another [jq TIL](https://til.juliusgamanyi.com/posts/jq-access-array-index-in-loop)) using the `here-doc` + + +```yaml +name: Multiline in outputs + +on: + push: + branches: + '**' + +jobs: + test: + name: debug + runs-on: [ubuntu] + + steps: + - name: prepare multiline string + id: prep-multiline + run: | + jsonData=$(cat <> $GITHUB_OUTPUT + + - name: use multiline string + run: | + echo "JsonInfo is {% raw %} ${{ steps.prep-multiline.outputs.JsonInfo }} {% endraw %} +``` + +Explanation of lines 24-25 + +1. Set output with the defined 'name' (in our case `JsonInfo`), and a 'delimiter' that would mark the end of the data: typically it would be a plain EOF but it's strongly recommended that the delimiter is random. +1. Keep reading each line and concatenating it into one input. +1. Once reaching the line consisting of the defined delimiter, stop processing. +This means that another output could start being added. + +That `jsonData` can contain the results of a command that returns json. + + +```yaml +name: Multiline in outputs + +on: + push: + branches: + '**' + +jobs: + test: + name: debug + runs-on: [ubuntu] + + steps: + - name: prepare multiline string + id: prep-multiline + run: | + AWS_REGION="us-east-1" + EC2_NAME="my-ec2" + jsonData="$(\ + aws ec2 describe-instances \ + --region "$AWS_REGION" \ + --filters "Name=tag:Name,Values=$EC2_NAME" \ + --query 'Reservations[*].Instances[*].{InstanceId: InstanceId, State: State.Name}' \ + --output json + )" + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) + echo "JsonInfo<> $GITHUB_OUTPUT + + - name: use multiline string + run: | + echo "JsonInfo is {% raw %} ${{ steps.prep-multiline.outputs.JsonInfo }} {% endraw %} +``` diff --git a/github-actions/print-github-webhook-payload.md b/github-actions/print-github-webhook-payload.md index 62cb961..0a72c11 100644 --- a/github-actions/print-github-webhook-payload.md +++ b/github-actions/print-github-webhook-payload.md @@ -25,11 +25,7 @@ jobs: env: GITHUB_CONTEXT: ${{ toJson(github) }} run: | - echo "$GITHUB_CONTEXT" - - - name: print delete webhook payload - run: | - echo ${{ GITHUB.event }} + echo "GITHUB_CONTEXT is $GITHUB_CONTEXT" + echo "VARS_CONTEXT is $VARS_CONTEXT" + echo "delete event is ${{ GITHUB_CONTEXT.event }}" ``` - - diff --git a/jq/access-array-index-in-loop.md b/jq/access-array-index-in-loop.md index 00ab783..36a025e 100644 --- a/jq/access-array-index-in-loop.md +++ b/jq/access-array-index-in-loop.md @@ -54,7 +54,7 @@ jsonData=$(cat <