Writing Multi-line String Environment Variables to GITHUB_ENV in GitHub Actions
Introduction
While building workflows in GitHub Actions, I encountered a situation where I needed to set a secret containing JSON content as an environment variable.
echo 'HOGE_JSON=${{ secrets.FUGA_JSON }}' >> $GITHUB_ENV
This resulted in the following error:
Error: Invalid format ' ***'
Upon investigation, I found that when writing multi-line strings to the environment variable $GITHUB_ENV
, a specific syntax must be used, so I’m documenting it here.
Method
When writing multi-line strings to the environment variable $GITHUB_ENV
, it seems you can use the following syntax with delimiters.
{name}<<{delimiter}
{value}
{delimiter}
Using this, the process mentioned above can be rewritten as follows, using EOF
as the delimiter.
{
echo 'HOGE_JSON<<EOF'
echo "${{ secrets.FUGA_JSON }}"
echo 'EOF'
} >> $GITHUB_ENV
By the way, {}
is a shell notation for grouping commands. The above process can also be written as follows:
echo 'HOGE_JSON<<EOF' >> $GITHUB_ENV
echo "${{ secrets.FUGA_JSON }}" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
Conclusion
With this, you can write multi-line strings to $GITHUB_ENV
.
The documentation for GitHub Actions is really well done, which is greatly appreciated.