| 1 |
# How to test locally: |
| 2 |
# |
| 3 |
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 4 |
# $ cargo install wrkflw |
| 5 |
# $ cargo install wrkflw |
| 6 |
# $ wrkflw run --verbose --event push --diff --show-action-messages .github/workflows/debug.yml |
| 7 |
|
| 8 |
name: "Report Workflow Information" |
| 9 |
description: "Reusable action meant to be used in workflow steps" |
| 10 |
branding: |
| 11 |
icon: "watch" |
| 12 |
color: "green" |
| 13 |
|
| 14 |
inputs: |
| 15 |
title: |
| 16 |
description: "A reference to put in the report title" |
| 17 |
required: true |
| 18 |
default: "the Job" |
| 19 |
parameters: |
| 20 |
description: "Input variables used in Job" |
| 21 |
required: false |
| 22 |
content: |
| 23 |
description: "Content to put in the report" |
| 24 |
required: false |
| 25 |
outputs: |
| 26 |
status: |
| 27 |
description: "Return report status" |
| 28 |
value: ${{ steps.report_workflow_generation.outputs.status }} |
| 29 |
runs: |
| 30 |
using: "composite" |
| 31 |
steps: |
| 32 |
- name: Report Workflow Information |
| 33 |
id: report_workflow_generation |
| 34 |
env: |
| 35 |
REPORT_PARAMS: "${{ inputs.parameters }}" |
| 36 |
REPORT_CONTENT: "${{ inputs.content }}" |
| 37 |
shell: python |
| 38 |
run: | |
| 39 |
import platform |
| 40 |
import os |
| 41 |
import json |
| 42 |
|
| 43 |
_GITHUB_STEP_SUMMARY = os.environ.get("GITHUB_STEP_SUMMARY", "sketch-github-step.md") |
| 44 |
_GITHUB_OUTPUT = os.environ.get("GITHUB_OUTPUT", "sketch-github-output.md") |
| 45 |
_group_is_open = False |
| 46 |
|
| 47 |
with open(_GITHUB_OUTPUT, "a") as _output, open(_GITHUB_STEP_SUMMARY, "a") as _summary: |
| 48 |
|
| 49 |
def out(key: str, text: str, *args: object, end: str = "\n", c: str = " \t\f\v") -> str: |
| 50 |
_text = text.format(*args).rstrip(c) if args else text.rstrip(c) if text else "" |
| 51 |
if key and key.strip(): |
| 52 |
_output.write("{}={}\n".format(key.strip(), _text + end)) |
| 53 |
return _text |
| 54 |
|
| 55 |
def group(text: str, *args: object) -> str: |
| 56 |
global _group_is_open |
| 57 |
_text = text.format(*args).strip() if args else text.strip() if text else "" |
| 58 |
if _text: |
| 59 |
if _group_is_open: |
| 60 |
endgroup() |
| 61 |
print("::group::{}".format(_text)) |
| 62 |
_group_is_open = True |
| 63 |
return _text |
| 64 |
|
| 65 |
def endgroup() -> None: |
| 66 |
global _group_is_open |
| 67 |
print("::endgroup::") |
| 68 |
_group_is_open = False |
| 69 |
|
| 70 |
def newline() -> None: |
| 71 |
_summary.write("\n") |
| 72 |
|
| 73 |
def line(text: str, *args: object, end: str = "\n", c: str = " \t\f\v") -> str: |
| 74 |
_text = text.format(*args).rstrip(c) if args else text.rstrip(c) if text else "" |
| 75 |
_summary.write(_text + end) |
| 76 |
return _text |
| 77 |
|
| 78 |
def header(text: str, *args: object, level: int = 2) -> str: |
| 79 |
_text = text.format(*args).strip() if args else text.strip() if text else "" |
| 80 |
if _text and level > 0: |
| 81 |
_pre, _hsh = "" if level < 2 else "\n", "#" * level |
| 82 |
line("{}{} {}\n", _pre, _hsh, _text) |
| 83 |
return _text |
| 84 |
|
| 85 |
def env(varname: str, *, title: str = "", level: int = 3) -> str: |
| 86 |
_value = os.environ.get(varname, None) if varname else None |
| 87 |
if _value: |
| 88 |
header(title, level=level) |
| 89 |
line(_value) |
| 90 |
newline() |
| 91 |
return _value |
| 92 |
|
| 93 |
def script(text: str, *args: object, lang: str = "", title: str = "", level: int = 3) -> str: |
| 94 |
_text = text.format(*args) if args else text if text else "" |
| 95 |
if _text: |
| 96 |
_lines = [ _l.rstrip() for _l in _text.splitlines()] |
| 97 |
_clean = "\n".join(_lines) |
| 98 |
header(title, level=level) |
| 99 |
line("```{}\n{}\n```\n", lang.strip().lower(), _clean) |
| 100 |
return _clean |
| 101 |
return _text |
| 102 |
|
| 103 |
def item(text: str, *args: object, indent: int = 0) -> None: |
| 104 |
_text = text.format(*args).rstrip() if args else text.rstrip() if text else "" |
| 105 |
if _text and indent >= 0: |
| 106 |
line("{}- {}", " " * abs(indent), _text) |
| 107 |
return _text |
| 108 |
|
| 109 |
def prop(title: str, text: str, *args: object, indent: int = 0) -> None: |
| 110 |
_text = text.format(*args) if args else text if text else "" |
| 111 |
if title: |
| 112 |
item("{}: {}\n", title, _text, indent=indent) |
| 113 |
return _text |
| 114 |
|
| 115 |
def listing(text: str, *, title: str = "", indent: int = 0, level: int = 3) -> None: |
| 116 |
if not text or not text.strip(): |
| 117 |
return |
| 118 |
try: |
| 119 |
_json = json.loads(text) |
| 120 |
except json.JSONDecodeError as _ex: |
| 121 |
line("Failed to parse JSON: {}", _ex) |
| 122 |
return |
| 123 |
if _json: |
| 124 |
header(title, level=level) |
| 125 |
for _key, _value in _json.items(): |
| 126 |
prop(_key.strip(), str(_value).strip() if _value else "", indent=indent) |
| 127 |
newline() |
| 128 |
|
| 129 |
def env_listing(varname: str, *, title: str = "", indent: int = 0, level: int = 3) -> str: |
| 130 |
_value = os.environ.get(varname, None) if varname else None |
| 131 |
if _value: |
| 132 |
listing(_value, title=title, indent=indent, level=level) |
| 133 |
return _value |
| 134 |
|
| 135 |
out("status", "started") |
| 136 |
group("Workflow Information") |
| 137 |
header("Job: ${{ inputs.title }}", level=1) |
| 138 |
|
| 139 |
env_listing("REPORT_PARAMS", title="Parameters") |
| 140 |
env("REPORT_CONTENT", title="Summary") |
| 141 |
|
| 142 |
header("Event Information") |
| 143 |
prop("github.repository", '''${{ github.repository }}''') |
| 144 |
prop("github.ref_name", '''${{ github.ref_name }}''') |
| 145 |
prop("github.sha", '''${{ github.sha }}''') |
| 146 |
prop("github.event_name", '''${{ github.event_name }}''') |
| 147 |
prop("github.actor", '''${{ github.actor }}''') |
| 148 |
|
| 149 |
group("Context Information") |
| 150 |
header("Context Information") |
| 151 |
listing("""${{ toJson(runner) }}""", title="Runner Context") |
| 152 |
listing("""${{ toJson(job) }}""", title="Job Context") |
| 153 |
endgroup() |
| 154 |
|
| 155 |
group("System Information") |
| 156 |
header("System Information") |
| 157 |
|
| 158 |
prop("System", platform.system()) |
| 159 |
prop("Release", platform.release()) |
| 160 |
prop("Version", platform.version()) |
| 161 |
prop("Uname", platform.uname()) |
| 162 |
prop("Node", platform.node()) |
| 163 |
prop("Machine", platform.machine()) |
| 164 |
prop("Processor", platform.processor()) |
| 165 |
|
| 166 |
group("Python Runtime") |
| 167 |
header("Python Runtime") |
| 168 |
|
| 169 |
prop("Version", platform.python_version()) |
| 170 |
prop("Version Tuple", " ".join(platform.python_version_tuple())) |
| 171 |
prop("Implementation", platform.python_implementation()) |
| 172 |
prop("Build", " ".join(platform.python_build())) |
| 173 |
prop("Compiler", platform.python_compiler()) |
| 174 |
prop("Branch", platform.python_branch()) |
| 175 |
prop("Revision", platform.python_revision()) |
| 176 |
|
| 177 |
out("status", "started") |
| 178 |
|
| 179 |
## end workflow action ## |
| 180 |
|