Using Python to Set GitHub Action Outputs

The output format of GitHub Action is to write {name}={value} to the environment variable GITHUB_OUTPUT[1][2] This article shares how to set up the GitHub Action output using Python. The main content comes from this discussion[3].

Singleline

1
2
3
4
5
import os
name = 'my_name'
value = 'my_value'
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f'{name}={value}', file=fh)

Multiline

1
2
3
4
5
6
7
8
import uuid

def set_multiline_output(name, value):
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
delimiter = uuid.uuid1()
print(f'{name}<<{delimiter}', file=fh)
print(value, file=fh)
print(delimiter, file=fh)

Just call it with

1
set_multiline_output("value", my_multiline_string)

References


Using Python to Set GitHub Action Outputs
https://blog.zhanganzhi.com/en/2024/09/a2d3aacb3fc0/
Author
Andy Zhang
Posted on
September 6, 2024
Licensed under