|
1 | 1 | # GitHub Action Utils
|
2 | 2 |
|
| 3 | +[](https://github.com/saadmk11/github-action-utils/releases/latest) |
| 4 | + |
| 5 | + |
| 6 | +[](https://github.com/saadmk11/github-action-utils/blob/main/LICENSE) |
| 7 | +[](https://github.com/saadmk11/github-action-utils/stargazers) |
| 8 | + |
3 | 9 | This package is a collection of python functions that can be used to run [GitHub Action Workflow Commands](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions) from a python script inside a workflow.
|
4 | 10 |
|
5 |
| -## Example: |
| 11 | +## Features |
| 12 | + |
| 13 | +This package provides a simple interface to run GitHub Action Workflow Commands from a python script inside a workflow. |
| 14 | +You can run almost all the command from [GitHub Action Workflow Commands](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions) using this package. |
| 15 | + |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +**Python:** 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install `github-action-utils` using pip: |
| 24 | + |
| 25 | +```console |
| 26 | +pip install github-action-utils |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +This section describes how to use the `github-action-utils` package. The functions in the package should be used inside a workflow. |
| 32 | + |
| 33 | +### **`echo`** |
| 34 | + |
| 35 | +Prints specified message to the action workflow console. |
| 36 | + |
| 37 | +**example:** |
| 38 | + |
| 39 | +```python |
| 40 | +>> from github_action_utils import echo |
| 41 | + |
| 42 | +>> echo("Hello World") |
| 43 | + |
| 44 | +# Output: |
| 45 | +# Hello World |
| 46 | +``` |
| 47 | + |
| 48 | +### **`debug`** |
| 49 | + |
| 50 | +Prints colorful debug message to the action workflow console. |
| 51 | +GitHub Actions Docs: [debug](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-debug-message) |
| 52 | + |
| 53 | +**example:** |
| 54 | + |
| 55 | +```python |
| 56 | +>> from github_action_utils import debug |
| 57 | + |
| 58 | +>> debug("Hello World") |
| 59 | + |
| 60 | +# Output: |
| 61 | +# ::debug ::Hello World |
| 62 | +``` |
| 63 | + |
| 64 | +### **`notice`** |
| 65 | + |
| 66 | +Prints colorful notice message to the action workflow console. |
| 67 | +GitHub Actions Docs: [notice](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message) |
| 68 | + |
| 69 | +**example:** |
| 70 | + |
| 71 | +```python |
| 72 | +>> from github_action_utils import notice |
| 73 | + |
| 74 | +>> notice( |
| 75 | + "test message", |
| 76 | + title="test title", |
| 77 | + file="abc.py", |
| 78 | + col=1, |
| 79 | + end_column=2, |
| 80 | + line=4, |
| 81 | + end_line=5, |
| 82 | +) |
| 83 | + |
| 84 | +# Output: |
| 85 | +# ::notice title=test title,file=abc.py,col=1,endColumn=2,line=4,endLine=5::test message= |
| 86 | +``` |
| 87 | + |
| 88 | +### **`warning`** |
| 89 | + |
| 90 | +Prints colorful warning message to the action workflow console. |
| 91 | +GitHub Actions Docs: [warning](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-warning-message) |
| 92 | + |
| 93 | +**example:** |
| 94 | + |
| 95 | +```python |
| 96 | +>> from github_action_utils import warning |
| 97 | + |
| 98 | +>> warning( |
| 99 | + "test message", |
| 100 | + title="test title", |
| 101 | + file="abc.py", |
| 102 | + col=1, |
| 103 | + end_column=2, |
| 104 | + line=4, |
| 105 | + end_line=5, |
| 106 | +) |
| 107 | + |
| 108 | +# Output: |
| 109 | +# ::warning title=test title,file=abc.py,col=1,endColumn=2,line=4,endLine=5::test message |
| 110 | +``` |
| 111 | + |
| 112 | +### **`error`** |
| 113 | + |
| 114 | +Prints colorful error message to the action workflow console. |
| 115 | +GitHub Actions Docs: [error](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-error-message) |
| 116 | + |
| 117 | +**example:** |
| 118 | + |
| 119 | +```python |
| 120 | +>> from github_action_utils import error |
| 121 | + |
| 122 | +>> error( |
| 123 | + "test message", |
| 124 | + title="test title", |
| 125 | + file="abc.py", |
| 126 | + col=1, |
| 127 | + end_column=2, |
| 128 | + line=4, |
| 129 | + end_line=5, |
| 130 | +) |
| 131 | + |
| 132 | +# Output: |
| 133 | +# ::error title=test title,file=abc.py,col=1,endColumn=2,line=4,endLine=5::test message |
| 134 | +``` |
| 135 | + |
| 136 | +### **`set_output`** |
| 137 | + |
| 138 | +Sets an action's output parameter for the running workflow. |
| 139 | +GitHub Actions Docs: [set_output](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter) |
| 140 | + |
| 141 | +**example:** |
| 142 | + |
| 143 | +```python |
| 144 | +>> from github_action_utils import set_output |
| 145 | + |
| 146 | +>> set_output("test_name", "test_value",) |
| 147 | + |
| 148 | +# Output: |
| 149 | +# ::set-output name=test_name::test_value |
| 150 | +``` |
| 151 | + |
| 152 | +### **`save_state`** |
| 153 | + |
| 154 | +Creates environment variable for sharing state with workflow's pre: or post: actions. |
| 155 | +GitHub Actions Docs: [save_state](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#sending-values-to-the-pre-and-post-actions) |
| 156 | + |
| 157 | +**example:** |
| 158 | + |
| 159 | +```python |
| 160 | +>> from github_action_utils import save_state |
| 161 | + |
| 162 | +>> save_state("test_name", "test_value",) |
| 163 | + |
| 164 | +# Output: |
| 165 | +# ::save-state name=test_name::test_value |
| 166 | +``` |
| 167 | + |
| 168 | +### **`get_state`** |
| 169 | + |
| 170 | +Gets state environment variable from running workflow. |
| 171 | + |
| 172 | +**example:** |
| 173 | + |
| 174 | +```python |
| 175 | +>> from github_action_utils import get_state |
| 176 | + |
| 177 | +>> get_state("test_name") |
| 178 | + |
| 179 | +# Output: |
| 180 | +# test_value |
| 181 | +``` |
| 182 | + |
| 183 | +### **`get_state`** |
| 184 | + |
| 185 | +Gets user input from running workflow. |
| 186 | + |
| 187 | +**example:** |
| 188 | + |
| 189 | +```python |
| 190 | +>> from github_action_utils import get_user_input |
| 191 | + |
| 192 | +>> get_user_input("my_input") |
| 193 | + |
| 194 | +# Output: |
| 195 | +# my value |
| 196 | +``` |
| 197 | + |
| 198 | +### **`begin_stop_commands` and `end_stop_commands`** |
| 199 | + |
| 200 | +Stops processing any workflow commands. This special command allows you to log anything without accidentally running a workflow command. |
| 201 | +GitHub Actions Docs: [stop_commands](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#stopping-and-starting-workflow-commands) |
| 202 | + |
| 203 | +**example:** |
| 204 | + |
| 205 | +```python |
| 206 | +>> from github_action_utils import echo, begin_stop_commands, end_stop_commands, stop_commands |
| 207 | + |
| 208 | +>> begin_stop_commands(token="my_token") |
| 209 | +>> echo("Hello World") |
| 210 | +>> end_stop_commands("my_token") |
| 211 | + |
| 212 | +# Output: |
| 213 | +# ::stop-commands ::my_token |
| 214 | +# Hello World |
| 215 | +# ::my_token:: |
| 216 | + |
| 217 | +# ==================== |
| 218 | +# Using Stop Commands Context Manager |
| 219 | +# ==================== |
| 220 | + |
| 221 | +>> with stop_commands(token="my_token"): |
| 222 | +... echo("Hello World") |
| 223 | + |
| 224 | +# Output: |
| 225 | +# ::stop-commands ::my_token |
| 226 | +# Hello World |
| 227 | +# ::my_token:: |
| 228 | +``` |
| 229 | + |
| 230 | +### **`add_mask`** |
| 231 | + |
| 232 | +Masking a value prevents a string or variable from being printed in the workflow console. |
| 233 | +GitHub Actions Docs: [add_mask](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#masking-a-value-in-log) |
| 234 | + |
| 235 | +**example:** |
| 236 | + |
| 237 | +```python |
| 238 | +>> from github_action_utils import add_mask |
| 239 | + |
| 240 | +>> add_mask("test value") |
| 241 | + |
| 242 | +# Output: |
| 243 | +# ::add-mask ::test value |
| 244 | +``` |
| 245 | + |
| 246 | +### **`set_env`** |
| 247 | + |
| 248 | +Creates an environment variable by writing this to the `GITHUB_ENV` environment file which is available to any subsequent steps in a workflow job. |
| 249 | +GitHub Actions Docs: [set_env](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable) |
| 250 | + |
| 251 | +**example:** |
| 252 | + |
| 253 | +```python |
| 254 | +>> from github_action_utils import set_env |
| 255 | + |
| 256 | +>> set_env("my_env", "test value") |
| 257 | +``` |
| 258 | + |
| 259 | +### **`get_workflow_environment_variables`** |
| 260 | + |
| 261 | +Gets all environment variables from the `GITHUB_ENV` environment file which is available to the workflow. |
| 262 | +GitHub Actions Docs: [set_env](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable) |
| 263 | + |
| 264 | +**example:** |
| 265 | + |
| 266 | +```python |
| 267 | +>> from github_action_utils import get_workflow_environment_variables |
| 268 | + |
| 269 | +>> get_workflow_environment_variables() |
| 270 | + |
| 271 | +# Output: |
| 272 | +# {"my_env": "test value"} |
| 273 | +``` |
| 274 | + |
| 275 | +### **`get_env`** |
| 276 | + |
| 277 | +Gets all environment variables from `os.environ` or the `GITHUB_ENV` environment file which is available to the workflow. |
| 278 | +This can also be used to get [environment variables set by GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables). |
| 279 | +GitHub Actions Docs: [set_env](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable) |
| 280 | + |
| 281 | +**example:** |
| 282 | + |
| 283 | +```python |
| 284 | +>> from github_action_utils import get_env |
| 285 | + |
| 286 | +>> get_env("my_env") |
| 287 | +>> get_env("GITHUB_API_URL") |
| 288 | + |
| 289 | +# Output: |
| 290 | +# test value |
| 291 | +# https://api.github.com |
| 292 | +``` |
| 293 | + |
| 294 | +### **`append_job_summary`** |
| 295 | + |
| 296 | +Sets some custom Markdown for each job so that it will be displayed on the summary page of a workflow run. |
| 297 | +GitHub Actions Docs: [append_job_summary](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary) |
| 298 | + |
| 299 | +**example:** |
| 300 | + |
| 301 | +```python |
| 302 | +>> from github_action_utils import append_job_summary |
| 303 | + |
| 304 | +>> append_job_summary("# test summary") |
| 305 | +``` |
| 306 | + |
| 307 | + |
| 308 | +### **`overwrite_job_summary`** |
| 309 | + |
| 310 | +Clears all content for the current step, and adds new job summary. |
| 311 | +GitHub Actions Docs: [overwrite_job_summary](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#overwriting-job-summaries) |
| 312 | + |
| 313 | +**example:** |
| 314 | + |
| 315 | +```python |
| 316 | +>> from github_action_utils import overwrite_job_summary |
| 317 | + |
| 318 | +>> overwrite_job_summary("# test summary") |
| 319 | +``` |
| 320 | + |
| 321 | +### **`remove_job_summary`** |
| 322 | + |
| 323 | +completely removes job summary for the current step. |
| 324 | +GitHub Actions Docs: [remove_job_summary](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#removing-job-summaries) |
| 325 | + |
| 326 | +**example:** |
| 327 | + |
| 328 | +```python |
| 329 | +>> from github_action_utils import remove_job_summary |
| 330 | + |
| 331 | +>> remove_job_summary() |
| 332 | +``` |
| 333 | + |
| 334 | +### **`add_system_path`** |
| 335 | + |
| 336 | +Prepends a directory to the system PATH variable (`GITHUB_PATH`) and automatically makes it available to all subsequent actions in the current job. |
| 337 | +GitHub Actions Docs: [add_system_path](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path) |
| 338 | + |
| 339 | +**example:** |
| 340 | + |
| 341 | +```python |
| 342 | +>> from github_action_utils import add_system_path |
| 343 | + |
| 344 | +>> add_system_path("var/path/to/file") |
| 345 | +``` |
| 346 | + |
| 347 | +### **`event_payload`** |
| 348 | + |
| 349 | +Get GitHub Event payload that triggered the workflow. |
| 350 | + |
| 351 | +More details: [GitHub Actions Event Payload](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads) |
| 352 | + |
| 353 | +**example:** |
| 354 | + |
| 355 | +```python |
| 356 | +>> from github_action_utils import event_payload |
| 357 | + |
| 358 | +>> event_payload() |
| 359 | + |
| 360 | +# Output: |
| 361 | +# {"action": "opened", "number": 1, "pull_request": {"url": "https://api.github.com/repos/octocat/Hello-World/pulls/1"}, "repository": {"url": "https://api.github.com/repos/octocat/Hello-World"}, "sender": {"login": "octocat"}...} |
| 362 | +``` |
| 363 | + |
| 364 | +## Example |
6 | 365 |
|
7 |
| -### Example Workflow: |
| 366 | +### Example Workflow |
8 | 367 |
|
9 | 368 | ```yaml
|
10 | 369 | name: run-python-script
|
|
43 | 402 | gha_utils.append_job_summary("# Hello World")
|
44 | 403 | ```
|
45 | 404 |
|
46 |
| -### Example Code: |
| 405 | +### Example Code |
47 | 406 |
|
48 | 407 | ```python
|
49 | 408 | import github_action_utils as gha_utils
|
|
0 commit comments