---
title: GitHub Actions details
description: Functionality for helping to create GitHub Actions workflows in Python
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Workflow setup

In your GitHub Actions workflow, include the following in your `run` step:

```bash
env:
  CONTEXT_GITHUB: ${{ toJson(github) }}
```

This stores the full [github context](https://docs.github.com/actions/reference/context-and-expression-syntax-for-github-actions#github-context), which includes information such as the name of the current workflow being run, the GitHub access token, and so forth.

As well as the `github` context, you can do that same thing for any of the other GitHub Actions contexts, which are:

`github` `env` `job` `steps` `runner` `secrets` `strategy` `matrix` `needs`

For instance, for the [needs](https://docs.github.com/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds) context, information about previous jobs specified in your `needs` clause, add this underneath your `CONTEXT_GITHUB` line:

```bash
  CONTEXT_NEEDS: ${{ toJson(needs) }}
```

Note that here's no harm having entries that are not used -- GitHub Actions will set them to an empty dictionary by default.

GitHub also adds a number of [`GITHUB_*` environment variables](https://docs.github.com/actions/reference/environment-variables#default-environment-variables) to all runners. These are available through the `env_github` `AttrDict`, with the `GITHUB_` prefix removed, and remainder converted to lowercase. For instance:

```python
env_github.repository
```

```output
'octocat/Hello-World'
```

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L37)

### user\_repo

```python
def user_repo():
```

*List of `user,repo` from \`env\_github.repository*

```python
user_repo()
```

```output
['octocat', 'Hello-World']
```

The possible events are available in the `Event` `enum`.

```python
', '.join(Event)
```

```output
'page_build, content_reference, repository_import, create, workflow_run, delete, organization, sponsorship, project_column, push, context, milestone, project_card, project, package, pull_request, repository_dispatch, team_add, workflow_dispatch, member, meta, code_scanning_alert, public, needs, check_run, security_advisory, pull_request_review_comment, org_block, commit_comment, watch, marketplace_purchase, star, installation_repositories, check_suite, github_app_authorization, team, status, repository_vulnerability_alert, pull_request_review, label, installation, release, issues, repository, gollum, membership, deployment, deploy_key, issue_comment, ping, deployment_status, fork, schedule'
```

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L58)

### create\_workflow\_files

```python
def create_workflow_files(
    fname:str, workflow:str, build_script:str, prebuild:bool=False
):
```

*Create workflow and script files in suitable places in `github` folder*

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L70)

### fill\_workflow\_templates

```python
def fill_workflow_templates(
    name:str, event, run, context, script, opersys:str='ubuntu', prebuild:bool=False
):
```

*Function to create a simple Ubuntu workflow that calls a Python [`ghapi`](cli.md#ghapi) script*

`event` is the event to trigger on. `run` is the shell lines to run before running the script, such as a `pip install` step. `context` are the env var context lines to include in the `env:` section of the workflow, normally created with [`env_contexts`](actions.md#env_contexts). `opersys` can be a string containing a comma-separated list of operating systems, e.g. `macos, ubuntu, windows`, which will be used to create a parallel matrix build.

The `prebuild` bool tells [`ghapi`](cli.md#ghapi) to include a prebuild job, which contains the following workflow:

```bash
runs-on: ubuntu-latest
outputs:
  out: ${{ toJson(steps) }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
  with: {python-version: '3.12'}
- name: Create release
  id: step1
  env:
    CONTEXT_GITHUB: ${{ toJson(github) }}
  run: |
    pip install -q ghapi
    python .github/scripts/prebuild.py
```

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L81)

### env\_contexts

```python
def env_contexts(
    contexts
):
```

*Create a suitable `env:` line for a workflow to make a context available in the environment*

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L90)

### create\_workflow

```python
def create_workflow(
    name:str, event:__main__.Event, contexts:list=None, opersys:str='ubuntu', prebuild:bool=False
):
```

*Function to create a simple Ubuntu workflow that calls a Python [`ghapi`](cli.md#ghapi) script*

```python
create_workflow('test', Event.release)
```

To create a basic [`ghapi`](cli.md#ghapi) workflow, call [`create_workflow`](actions.md#create_workflow), passing in the event that you wish to respond to, and a name for your workflow.

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L98)

### gh\_create\_workflow

```python
def gh_create_workflow(
    name:str, # Name of the workflow file
    event:str, # Event to listen for
    contexts:str='', # Space-delimited extra contexts to include in `env` in addition to github
):
```

*Supports `gh-create-workflow`, a CLI wrapper for [`create_workflow`](actions.md#create_workflow).*

## Accessing contexts from Python

The information from these variables are provided by `context_github`, `context_needs`, and so forth for each named context. These variables are `AttrDict` objects.

```python
L(context_github)
```

```output
(#26) ['token','job','ref','sha','repository','repository_owner','repositoryUrl','run_id','run_number','retention_days'...]
```

```python
context_github.ref
```

```output
'refs/heads/master'
```

If you use our recommended workflow template, you will have this included in your prebuild step (if you have any):

```bash
outputs:
  out: ${{ toJson(steps) }}
```

You can access this content as a dictionary like so:

```python
loads(nested_idx(context_needs, "prebuild", "outputs", "out"))
```

```output
{'step1': {'outputs': {'tag': 'v0.79.0'},
  'outcome': 'success',
  'conclusion': 'success'}}
```

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L110)

### example\_payload

```python
def example_payload(
    event
):
```

*Get an example of a JSON payload for `event`*

## Workflow helper functions

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L115)

### github\_token

```python
def github_token():
```

*Get GitHub token from `GITHUB_TOKEN` env var if available, or from `github` context*

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L120)

### actions\_output

```python
def actions_output(
    name, value
):
```

*Print the special GitHub Actions `::set-output` line for `name::value`*

Details in the [GitHub Documentation for `set-output`](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter).

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L125)

### actions\_debug

```python
def actions_debug(
    message
):
```

*Print the special `::debug` line for `message`*

Details in the [GitHub Documentation for `debug`](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message). Note that you must create a secret named `ACTIONS_STEP_DEBUG` with the value true to see the debug messages set by this command in the log.

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L130)

### actions\_warn

```python
def actions_warn(
    message, details:str=''
):
```

*Print the special `::warning` line for `message`*

Details in the [GitHub Documentation for `warning`](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message). For the optional `details`, you can provide comma-delimited file, line, and column information, e.g.: `file=app.js,line=1,col=5`.

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L135)

### actions\_error

```python
def actions_error(
    message, details:str=''
):
```

*Print the special `::error` line for `message`*

Details in the [GitHub Documentation for `error`](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#setting-an-error-message). For the optional `details`, you can provide comma-delimited file, line, and column information, e.g.: `file=app.js,line=1,col=5`.

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L141)

### actions\_group

```python
def actions_group(
    title
):
```

*Context manager to print the special `::group`/`::endgroup` lines for `title`*

```python
with actions_group('Adding numbers...'): print(1+1)
```

```output
::group::Adding numbers...
2
::endgroup::
```

Details in the GitHub Documentation for [grouping log lines](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#grouping-log-lines).

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L148)

### actions\_mask

```python
def actions_mask(
    value
):
```

*Print the special `::add-mask` line for `value`*

Details in the [GitHub Documentation for `add-mask`](https://docs.github.com/actions/reference/workflow-commands-for-github-actions#masking-a-value-in-log).

[source](https://github.com/fastai/ghapi/blob/main/ghapi/actions.py#L153)

### set\_git\_user

```python
async def set_git_user(
    api=None
):
```

*Set git user name/email to authenticated user (if `api`) or GitHub Actions bot (otherwise)*

When pushing to git from a workflow, you'll need to set your username and email address. You can set them to the [`GhApi`](core.md#ghapi) authenticated user's details by passing `api`. Otherwise, `github-actions[bot]` and `github-actions[bot]@users.noreply.github.com` will be used, which will make a push appear to be from "GitHub Actions".
