Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo a multi line variable in Github Actions?

I have stored a multiline environnement variable thanks to github docs.

- name: copy
  run: |
     echo 'CONTENT_ENV<<EOF' >> $GITHUB_ENV
     cat README.md >> $GITHUB_ENV
     echo 'EOF' >> $GITHUB_ENV

I would like to get it, but this code give one line :

 - name: paste
   run: echo $CONTENT_ENV > index.md

The worflow shows the variable properly stored in env (with spaces) :

workflow

like image 504
Foxhunt Avatar asked Jun 20 '26 02:06

Foxhunt


2 Answers

Your variable is properly stored, but you need to add quotes if you want to preserve the newlines :

- name: paste
  run: echo "$CONTENT_ENV" > index.md
- name: test
  run: cat index.md

Short explanation is that default field separator is whitespace (space, tab and newline), so if you don't use quotes, echo will consider all newlines separated strings as different arguments. See this post for more in-depth explanation

like image 191
Bertrand Martel Avatar answered Jun 22 '26 08:06

Bertrand Martel


You can use an option like this

- name: Output file
  id: read-file
  run: |
    FILE_CONTENT="$(<${FILE_NAME})"
    printf "npmrc_content<<EOF\n%s\nEOF\n" "${FILE_CONTENT}" >> "$GITHUB_OUTPUT"
like image 37
Jorge Arias Avatar answered Jun 22 '26 06:06

Jorge Arias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!