Skip to content

Commit

Permalink
Write action stdout as an output
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jul 29, 2024
1 parent 27ee16b commit a2051cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 16 additions & 6 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Check failure on line 29 in action/main.py

View workflow job for this annotation

GitHub Actions / Pylint

action/main.py#L29

Redefining built-in 'exit' (redefined-builtin, W0622)
"""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)

Check failure on line 38 in action/main.py

View workflow job for this annotation

GitHub Actions / Pylint

action/main.py#L38

Consider using 'sys.exit' instead (consider-using-sys-exit, R1722)


def pip_install(*packages):
Expand Down Expand Up @@ -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"<<DARKER_ACTION_EOF\n{proc.stdout}\nDARKER_ACTION_EOF")
exit(proc.returncode)

Check failure on line 104 in action/main.py

View workflow job for this annotation

GitHub Actions / Pylint

action/main.py#L104

Consider using 'sys.exit' instead (consider-using-sys-exit, R1722)

0 comments on commit a2051cf

Please sign in to comment.