API¶
This page has been added here as a quick reference for the API of the
m.github.actions
module.
Action
¶
Bases: BaseModel
A Github action.
The main object to help us define the actions.yaml
file. Each repository can
declare several actions. For this reason we can declare the actions
object as
a single Action
or a list of them.
We could technically do everything in one single step but when we use external
actions then we are forced to split the step. Note that we have no way of
having the if
fields in each step. This is because we are encouraged to handle
that logic in each of the steps we declare. We have full control of the output to
stdout
and stderr
here.
Attributes:
Name | Type | Description |
---|---|---|
file_path |
str
|
The full path to the |
name |
str
|
The name of the action. |
description |
str
|
Short description for the action. |
inputs |
type[KebabModel] | None
|
A model describing the inputs for the action. If the action does not
need inputs then provide |
steps |
list[RunStep | UsesStep | BashStep]
|
The steps for the action |
Source code in m/github/actions/actions.py
gather_outputs()
¶
Obtain a tuple with the action outputs and all steps outputs.
The steps outputs is a dictionary that maps keys of the form [step_id].[step_output_arg] to the output of another step, action input or some other value that should be used as input.
This function validates that all the keys are valid.
Returns:
Type | Description |
---|---|
Res[ActionOutputs]
|
A tuple with the outputs if successful, otherwise an issue. |
Source code in m/github/actions/actions.py
BashStep
¶
Bases: BaseModel
, Generic[InputModel, OutputModel]
Step to add bash scripting into the action.
This step does not connect to any of the others one and its been added to be able to run poetry commands or any other setup that we may need in the action.
Attributes:
Name | Type | Description |
---|---|---|
id |
str
|
The id of the step. |
run_if |
str | None
|
The condition to run the step. |
run |
str
|
The bash script to execute. |
args |
InputModel | None
|
The arguments to pass to the run function. |
Source code in m/github/actions/actions.py
get_inputs_outputs()
¶
Get the inputs and outputs for the step.
Returns:
Type | Description |
---|---|
InputOutputs
|
A tuple of the inputs and outputs. |
to_str(_python_path, available_values)
¶
Generate a string to use in the Github Action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_python_path |
str
|
The path to the python module. |
required |
available_values |
dict[str, str]
|
The values that are available to the step. |
required |
Returns:
Type | Description |
---|---|
str
|
A string to add to the Github action. |
Source code in m/github/actions/actions.py
KebabModel
¶
Bases: BaseModel
Allows models to be defined with kebab case properties.
Inputs and outputs need to be written using a KebabModel
as a base class.
This is so that their definitions may be written using kebab casing in the
final action.yaml
.
from m.github.actions import KebabModel, InArg
class MyInput(KebabModel):
my_input: str = InArg(help='description')
Source code in m/pydantic.py
RunStep
¶
Bases: BaseModel
, Generic[InputModel, OutputModel]
Model used to define a "run" step in a Github action.
The id
is important because this is how we will be able to refer to the
outputs generated by the step. Say our action called other external actions
such as the cache action. Then if we wanted to pass one of the outputs to
the cache action we would have to get a handle on the step.
The args
should leverage the help of their own input models. All they
require is that we provide the handle to other outputs from other steps or
some other values we wish to pass.
Experiment with different values to see what action.yaml
generates.
Attributes:
Name | Type | Description |
---|---|---|
id |
str
|
The id of the step. |
run_if |
str | None
|
The condition to run the step. |
run |
Callable[[InputModel], Res[OutputModel]]
|
The function passed to |
args |
InputModel | None
|
The arguments to pass to the run function. |
Source code in m/github/actions/actions.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
get_inputs_outputs()
¶
Get the inputs and outputs for the step.
Returns:
Type | Description |
---|---|
InputOutputs
|
A tuple of the inputs and outputs. |
to_str(python_path, available_values)
¶
Generate a string to use in the Github Action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
python_path |
str
|
The path to the python module. |
required |
available_values |
dict[str, str]
|
The values that are available to the step. |
required |
Returns:
Type | Description |
---|---|
str
|
A string to add to the Github action. |
Source code in m/github/actions/actions.py
UsesStep
¶
Bases: BaseModel
, Generic[InputModel, OutputModel]
A "uses" step in a Github action.
Model similar to RunStep
but since we do
not have access to the code that gets executed all we can do is provide the
uses
field and our models to describe what the action expects.
For instance, say we wanted to use the actions/cache@v4
action. To avoid
having issues in the future we should create the input and output models
manually by looking at the documentation
https://github.com/actions/cache#inputs.
class CacheInputs(KebabModel):
key: str = InArg(help='An explicit key for a cache entry')
path: str = InArg(help="""
A list of files, directories, and wildcard patterns to cache and
restore.
""")
Similarly for the outputs we can define a model. This in the long run should help us maintain the composite action better. It is recommended to check the inputs and outputs as we update action versions to ensure compatibility.
Attributes:
Name | Type | Description |
---|---|---|
id |
str
|
The step id. |
run_if |
str | None
|
The condition to run the step. |
uses |
str
|
A string referencing an action. |
inputs |
type[InputModel]
|
A reference to a KebabModel type. |
outputs |
type[OutputModel]
|
A reference to a KebabModel type. |
args |
InputModel | None
|
The arguments to pass to the action |
Source code in m/github/actions/actions.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
|
get_inputs_outputs()
¶
Get the inputs and outputs for the step.
Returns:
Type | Description |
---|---|
InputOutputs
|
A tuple of the inputs and outputs. |
to_str(_python_path, available_values)
¶
Generate a string to use in the Github Action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_python_path |
str
|
The path to the python module. |
required |
available_values |
dict[str, str]
|
The values that are available to the step. |
required |
Returns:
Type | Description |
---|---|
str
|
A string to add to the Github action. |
Source code in m/github/actions/actions.py
InArg(*, help, default=None)
¶
Force proper annotation of the input of a GitHub Action.
Should be used to declare the input arguments of an action. It returns
Any
to bypass mypy
's type checking. Similar to
pydantic.fields.Field but it is tailored to help us write the inputs for
an action and its steps.
Note
By default all input arguments are required. If you want to make an input not required then provide a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
help |
str
|
Human-readable description. |
required |
default |
str | None
|
The default value for the argument. |
None
|
Returns:
Type | Description |
---|---|
Any
|
A new |
Source code in m/github/actions/api.py
OutArg(*, help, export=False)
¶
Force proper annotation of the output of a GitHub Action.
Note
All steps have access to the steps output, if we want to make the output
available to the action we need to export
it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
help |
str
|
Human-readable description. |
required |
export |
bool
|
Whether the argument is to be exported to the action. |
False
|
Returns:
Type | Description |
---|---|
Any
|
A new |
Source code in m/github/actions/api.py
run_action(main)
¶
Entry point for a GitHub Action.
This is the main function that should be used to run an action. It takes in
a function that takes in a m.pydantic.KebabModel and returns a
Res[KebabModel]
.
The only place where this function is needed is in the the if block
mypy
will make sure that the you are providing the correct type of function
to run_action
. Keep in mind, the function is generic and we should be writing
models for the inputs and outputs for all of our functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
main |
Callable[[InputModel], Res[OutputModel]]
|
The main function of the GitHub Action. |
required |