|
1 | | -# py-githubactions |
2 | | -Github actions core library implemented in python. |
| 1 | +# pygithubactions |
| 2 | +### pygithubactions/core |
| 3 | +> Core functions for setting results, logging, registering secrets and exporting variables across actions |
| 4 | +
|
| 5 | +### Usage |
| 6 | + |
| 7 | +### Import the package |
| 8 | + |
| 9 | +```python |
| 10 | +import pygithubaction.core |
| 11 | +``` |
| 12 | + |
| 13 | +### Inputs/Outputs |
| 14 | + |
| 15 | +Action inputs can be read with `get_input` which returns a `str` or `get_boolean_input` which parses a boolean based on the [yaml 1.2 specification](https://yaml.org/spec/1.2/spec.html#id2804923). If `required` set to be false, the input should have a default value in `action.yml`. |
| 16 | + |
| 17 | +Outputs can be set with `set_output` which makes them available to be mapped into inputs of other actions to ensure they are decoupled. |
| 18 | + |
| 19 | +```python |
| 20 | +from pygithubaction.core import get_input |
| 21 | +from pygithubaction.core import set_output |
| 22 | + |
| 23 | +my_input = get_input('inputName', required=True); |
| 24 | +boolean_input = get_boolean_input('booleanInputName', required=True); |
| 25 | +multiline_input = get_multiline_input('multilineInputName', required=True) |
| 26 | + |
| 27 | +set_output('outputkey', 'outputval') |
| 28 | +``` |
| 29 | + |
| 30 | +### Exporting variables |
| 31 | + |
| 32 | +Since each step runs in a separate process, you can use `export_variable` to add it to this step and future steps environment blocks. |
| 33 | + |
| 34 | +```python |
| 35 | +from pygithubaction.core import export_variable |
| 36 | + |
| 37 | +export_variable('envvar', 'val') |
| 38 | +``` |
| 39 | + |
| 40 | +### Setting a secret |
| 41 | + |
| 42 | +Setting a secret registers the secret with the runner to ensure it is masked in logs. |
| 43 | + |
| 44 | +```python |
| 45 | +from pygithubaction.core import set_secret |
| 46 | + |
| 47 | +set_secret('mypassword') |
| 48 | +``` |
| 49 | + |
| 50 | +### PATH Manipulation |
| 51 | + |
| 52 | +To make a tool's path available in the path for the remainder of the job (without altering the machine or containers state), use `add_path`. The runner will prepend the path given to the jobs PATH. |
| 53 | + |
| 54 | +```python |
| 55 | +from pygithubaction.core import add_path |
| 56 | + |
| 57 | +add_path('/path/to/mytool') |
| 58 | +``` |
| 59 | + |
| 60 | +### Exit codes |
| 61 | + |
| 62 | +You should use this library to set the failing exit code for your action. If status is not set and the script runs to completion, that will lead to a success. |
| 63 | + |
| 64 | +```python |
| 65 | +from pygithubaction.core import set_failed |
| 66 | + |
| 67 | +try: |
| 68 | + # Do stuff |
| 69 | + # ... |
| 70 | + |
| 71 | +except Exception as e: |
| 72 | + # set_failed logs the message and sets a failing exit code |
| 73 | + set_failed(f'Action failed with error {e}') |
| 74 | +``` |
| 75 | + |
| 76 | +### Logging |
| 77 | + |
| 78 | +Finally, this library provides some utilities for logging. Note that debug logging is hidden from the logs by default. This behavior can be toggled by enabling the [Step Debug Logs](../../docs/action-debugging.md#step-debug-logs). |
| 79 | + |
| 80 | +```python |
| 81 | +from pygithubaction.core import ( |
| 82 | + get_input, debug, info, notice, is_debug, error |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +my_input = get_input('input'); |
| 87 | + |
| 88 | +try: |
| 89 | + debug('Inside try block') |
| 90 | + |
| 91 | + if not my_input: |
| 92 | + warning('my_input was not set') |
| 93 | + |
| 94 | + if is_debug(): |
| 95 | + # curl -v https://github.com |
| 96 | + else: |
| 97 | + # curl https://github.com |
| 98 | + |
| 99 | + # Do stuff |
| 100 | + info('Output to the actions build log') |
| 101 | + notice('This is a message that will also emit an annotation') |
| 102 | + |
| 103 | +except Exception as e: |
| 104 | + error(f'Error: {e}, action may still succeed though') |
| 105 | +``` |
0 commit comments