File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 4
4
#
5
5
6
6
import os
7
+ import uuid
7
8
from contextlib import contextmanager
8
9
from io import TextIOWrapper
9
10
from pathlib import Path
@@ -287,6 +288,37 @@ def output(name: str, value: SupportsStr):
287
288
with Path (output_filename ).open ("a" , encoding = "utf8" ) as f :
288
289
f .write (f"{ name } ={ value } \n " )
289
290
291
+ @staticmethod
292
+ def multiline_output (name : str , value : SupportsStr ):
293
+ """
294
+ Set an multiline action output
295
+
296
+ An action output can be consumed by another job
297
+
298
+ Example:
299
+ .. code-block:: python
300
+
301
+ from pontos.github.actions import ActionIO
302
+
303
+ ActionIO.output("foo", "bar")
304
+
305
+ Args:
306
+ name: Name of the output variable
307
+ value: Value of the output variable
308
+ """
309
+ output_filename = os .environ .get ("GITHUB_OUTPUT" )
310
+ if not output_filename :
311
+ raise GitHubActionsError (
312
+ "GITHUB_OUTPUT environment variable not set. Can't write "
313
+ "action output."
314
+ )
315
+
316
+ with Path (output_filename ).open ("a" , encoding = "utf8" ) as f :
317
+ delimiter = uuid .uuid1 ()
318
+ f .write (f"{ name } <<{ delimiter } " )
319
+ f .write (f"{ value } " )
320
+ f .write (str (delimiter ))
321
+
290
322
@staticmethod
291
323
def input (name : str , default : Optional [str ] = None ) -> Optional [str ]:
292
324
"""
Original file line number Diff line number Diff line change @@ -107,6 +107,27 @@ def test_output(self):
107
107
108
108
self .assertEqual (output , "foo=bar\n lorem=ipsum\n " )
109
109
110
+ @patch ("uuid.uuid1" )
111
+ def test_multiline_output (self , uuid_mock ):
112
+ deadbeef = "deadbeef"
113
+ name = "foo"
114
+ ml_string = """bar
115
+ baz
116
+ boing"""
117
+ expected_output = f"{ name } <<{ deadbeef } { ml_string } { deadbeef } "
118
+ uuid_mock .return_value = deadbeef
119
+ with temp_directory () as temp_dir :
120
+ file_path = temp_dir / "github.output"
121
+
122
+ with patch .dict (
123
+ "os.environ" , {"GITHUB_OUTPUT" : str (file_path )}, clear = True
124
+ ):
125
+ ActionIO .multiline_output ("foo" , ml_string )
126
+
127
+ output = file_path .read_text (encoding = "utf8" )
128
+
129
+ self .assertEqual (output , expected_output )
130
+
110
131
@patch .dict ("os.environ" , {}, clear = True )
111
132
def test_output_no_env (self ):
112
133
with self .assertRaises (GitHubActionsError ):
You can’t perform that action at this time.
0 commit comments