From a2051cff8b3d436ee84e1251b656b921abb4cd7f Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:14:57 +0300 Subject: [PATCH] Write action stdout as an output --- action.yml | 3 +++ action/main.py | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 4f733bf07..8be6f4631 100644 --- a/action.yml +++ b/action.yml @@ -39,6 +39,9 @@ outputs: exitcode: description: "Exit code of Darker" value: ${{ steps.darker.outputs.exitcode }} + stdout: + description: "Standard output of Darker" + value: ${{ steps.darker.outputs.stdout }} branding: color: "black" icon: "check-circle" diff --git a/action/main.py b/action/main.py index 75d4d18d4..ff2fa6f88 100644 --- a/action/main.py +++ b/action/main.py @@ -19,10 +19,23 @@ REVISION = os.getenv("INPUT_REVISION") or os.getenv("INPUT_COMMIT_RANGE") or "HEAD^" WORKING_DIRECTORY = os.getenv("INPUT_WORKING_DIRECTORY", ".") + +def set_github_output(key: str, val: str) -> None: + """Write a key-value pair to the output file.""" + with Path(os.environ["GITHUB_OUTPUT"]).open("a", encoding="UTF-8") as f: + print(f"{key}={val}", file=f) + + +def exit(exitcode: int) -> None: + """Write the exit code to the output file and exit with it.""" + set_github_output("exitcode", str(exitcode)) + sys.exit(exitcode) + + # Check if the working directory exists if not os.path.isdir(WORKING_DIRECTORY): print(f"::error::Working directory does not exist: {WORKING_DIRECTORY}", flush=True) - sys.exit(21) + exit(21) def pip_install(*packages): @@ -87,8 +100,5 @@ def pip_install(*packages): ) print(proc.stdout, end="") -print(f"Writing exitcode={proc.returncode} to {os.environ['GITHUB_OUTPUT']}") - -with Path(os.environ["GITHUB_OUTPUT"]).open("a", encoding="UTF-8") as f: - print(f"exitcode={proc.returncode}", file=f) -sys.exit(proc.returncode) +set_github_output("stdout", f"<