create-release icon indicating copy to clipboard operation
create-release copied to clipboard

Process Escape Characters in Release `body`

Open nathanielatom opened this issue 6 years ago • 5 comments

My use case: I'm using an environment variable to pass a parsed sub-section of my changelog into the release body.

The environment variable is defined in a previous step using https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env. From what I can tell, this logging command does not work with multi-line environment variables unless the newlines are escaped, so I replaced the newline character with the ascii characters \n. This environment variable can be passed successfully to further steps, and echo -e $CHANGELOG processes the escaped characters and shows newlines as expected.

But when passing body: ${{ env.CHANGELOG }} to actions/create-release, the created release shows ascii \n characters instead of newlines. Would it be possible to process escape characters in the body the same way echo -e does? If this isn't desired as default behaviour, could an input be added to specify this?

Thanks :)

nathanielatom avatar Nov 25 '19 05:11 nathanielatom

I found out that you can set outputs with newlines, if you replace newlines with the literal characters %0A

e.g., echo "::set-output name=my_output::hello%0Aworld%0A!"

Presumably also works with ::set-env although I haven't tried

walfie avatar Nov 27 '19 17:11 walfie

Here is the workaround I'm using now:

text="${text//'%'/'%25'}"
text="${text//$'\n'/'%0A'}"
text="${text//$'\r'/'%0D'}"
echo "::set-output name=value::$text"

And using master

- name: Create ${{ steps.tag.outputs.value }} release
  id: create_release
  uses: actions/create-release@master
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 
  with:
     tag_name: ${{ github.ref }}
     release_name: ${{ steps.tag.outputs.value }}
     body: |
       ${{ steps.release_notes.outputs.value }}
     draft: true
     prerelease: false

trung avatar Dec 06 '19 18:12 trung

So my script generates a table of metrics for my bachelor thesis:

Relevant code

def print_result(analysis_result: Dict[str, Any]) -> None:
    data = sorted([(k, v) for k, v in analysis_result.items()])
    to_print = "### Metrics\n" + tabulate.tabulate(data, tablefmt="github")
    # use newline char for url encoding instead of standard one
    # to get multiline output shown in release body
    to_print = to_print.replace('\n', '%0A')
    print(to_print)

Output (standard output)

'### Metrics%0A|------------|-----|%0A| citations | 3 |%0A| figures | 0 |%0A| pages | 30 |%0A| tables | 0 |%0A| word_count | 510 |'

In the CI job console, the table is shown correctly:

image

However, in the release, the table becomes malformed like these:

image

Can someone help me out finding the problem here?

nazarimilad avatar Dec 27 '19 09:12 nazarimilad

Nevermind, the problem is that multiple continous whitespace characters are replaced by one in HTML (and thus Markdown rendering). So I ended up adding fenced code blocks characters.

nazarimilad avatar Dec 27 '19 10:12 nazarimilad

Thanks @walfie and @trung! I found that I had to tweak the search and replace a little, so I ended up with:

text="${text//$'%'/%25}"
text="${text//$'\n'/%0A}"
text="${text//$'\r'/%0D}"
echo "::set-env name=value::$text"

Also I can confirm it works with ::set-env as expected.

nathanielatom avatar Jan 17 '20 02:01 nathanielatom