Ibraheem Z. Abu Kaff
3 min readNov 14, 2022

--

GitHub Actions — Exporting Multi-Line / One-Line Value Environment Variable

Usually, when we create and set up new GitHub Action we utilize environment variables, to store information that we want to reference in the workflow.

Recently I was setting up a CI pipeline using GitHub Actions, and came across a case where I wanted to define new environment variables, some of which are supposed to have a multi-line value😵‍💫 — and it was throwing me an error.

Unable to process file command 'env' successfully. Invalid format

However, in this post, I will show you how to define Multi-Line / One Line Value Environment Variables, and how to add them to the `GitHub Env` Context to make them accessible among the steps of GitHub Action.

1 — Environment Variable With One-Line Value (simple and straightforward)

1.1— Definition:

# 1- Initialize a variable with a value
ONE_LINE_TEXT="This is a one-line text"

# 2- Define a new environment variable and assign a value to it, Then
# Add the new environment variable to the Context of the GitHub Environment
echo "ONE_LINE_TEXT_ENV_VAR=$ONE_LINE_TEXT" >> $GITHUB_ENV

1.2 — Usage/Referencing — This is very specific to GitHub Actions:

# To access the environment variable that we just defined,
# we need to do the following - This is Very specific to GitHub Actions:
echo ${{ env.ONE_LINE_TEXT_ENV_VAR }}

Full Example:

name: Test CI - One-Line Value
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Defining a new Environment Variable
run: |
ONE_LINE_TEXT="This is a one-line text"
echo "ONE_LINE_TEXT_ENV_VAR=$ONE_LINE_TEXT" >> $GITHUB_ENV

- name: Using the previously defined environment variable
run: |
echo ${{ env.ONE_LINE_TEXT_ENV_VAR }}

2 — Environment Variable With Multi-Line Value

2.1 — Definition:

# 1- Initialize a variable with a value
MULTI_LINES_TEXT="this is line one \n
this is line two \n
this is line three \n"

# 2- Define a new environment variable and assign a value to it, Then
# Add the new environment variable to the Context of the GitHub Environment
echo "MULTI_LINES_TEXT_ENV_VAR=$MULTI_LINES_TEXT" >> $GITHUB_ENV
# above line will throw us an error

Actually, the above definition won’t work while executing the Github Action

And it will throw an error saying:

Unable to process file command 'env' successfully. Invalid format

To resolve the issue, we have to do the following:

 echo "MULTI_LINES_TEXT_ENV_VAR<<EOF" >> $GITHUB_ENV

# here we can place the command that will generate multi-line text
echo $MULTI_LINES_TEXT >> $GITHUB_ENV

echo "EOF" >> $GITHUB_ENV

2.2 — Usage/Referencing — This is very specific to GitHub Actions:

# To access the environment variable that we just defined,
# we need to do the following - This is Very specific to GitHub Actions:
echo ${{ env.MULTI_LINES_TEXT_ENV_VAR }}

Full Example:

name: Test CI - Multi-Line Value
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Defining a new Environment Variable
run: |
MULTI_LINES_TEXT="this is line one \n
this is line two \n
this is line three \n"

echo "MULTI_LINES_TEXT_ENV_VAR<<EOF" >> $GITHUB_ENV
# here we can place the command that will generate multi-line text
echo $MULTI_LINES_TEXT >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV

- name: Using the previously defined environment variable
run: |
echo ${{ env.ONE_LINE_TEXT_ENV_VAR }}

Let’s Get Practical:

Now Let’s Take a practical example of running Pylint Command and Posting the results (which are supposed to be multi-line Text) to the PR comments:

name: Pylinting and Posting Results(multi-line text) to PR comments 
on: push
jobs:
pylinting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Installing Pylint Dependencies
run: |
pip install --upgrade pip
pip install pylint==2.15.3 pylint_django==2.5.3 pylint-plugin-utils==0.7
pip install pytest

- name: Pyliniting.
run: |
echo 'PYLINTING_RESULTS<<EOF' >> $GITHUB_ENV
pylint --exit-zero $(git ls-files '*.py') >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV

- name: Post Pylinting result as a PR Comment
uses: mshick/add-pr-comment@v1
with:
message: ${{ env.PYLINTING_RESULTS }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: 'github-actions[bot]'
allow-repeats: false

Hope you found that useful! Happy coding!

--

--

Ibraheem Z. Abu Kaff

Dubai-based Syrian Software Engineer, skilled in: #php #python #Node.js #django #laravel #mongodb #redis #docker #rest #k8s #microservices. ibraheem-abukaff.com