iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤯

Struggling with Newline Characters in GitHub Actions

に公開2件

I was trying to use a local file containing line breaks in GitHub Actions.
However, when I processed the passed variables in my script by splitting them with \n, it didn't behave as expected.
So, I decided to verify it.

Verification

Let's try setting the following value in GitHub Actions Variables.

Setting a string with line breaks

Hello
World

This is just a plain text with line breaks. I'll output it as character codes using the following script.

echo $VALUE1 > temp.txt
xxd -p temp.txt

When executed on GitHub Actions:

48656c6c6f0d20576f726c640a

was the result. Let's try the same thing locally.

Then,

48656c6c6f0a576f726c640a

the value became this.
It's a bit hard to tell, but there's a difference in the middle part: 0d20 versus 0a.

0d is CR (\r), and 20 is a space. 0a is LF (\n), which is a line break. This is the one I wanted.

I'm not sure if it's because of how I set it up, but I found that the line break code changed and even a space was embedded.

Solution

There are probably several ways to handle this.
One way might be to replace \r with \n.
I handled it by Base64 encoding the string set in GitHub Actions Variables and then decoding it when using it.
If you're trying to set a string with line breaks in GitHub Actions Variables, be careful ✋

I'm not sure if you're interested, but I conducted the verification here.

https://github.com/fromkk/NewLineTest

Discussion

yorifujiyorifuji

原因としては

  • GitHubのフォームでの改行がCRLFとして扱われている(Webアプリの実装依存)
    • CRLFはWindows系での改行コード
    • LFはUNIX系(macOS含む)の改行コード
  • shellとechoの副作用でLFがスペースに置き換わった

あたりでしょうか。

echo "$VALUE1"とすることでCRLFとして出力させることはできると思いますが元のファイルの改行コードがLFだとすれば厳密には中身が別物になってしまいますね。

結局はbase64に変換するのが最善手ですね!

1
かっくんかっくん

ブラウザがPOSTする値もあるかも?と思ったので調べる必要あるかもしれないですね(今回はArcを利用しました)

1