Skip to main content

Tiny actions/core-style helpers for Python GitHub Actions

Project description

gh-actions-tool-kit

A lightweight, typed Python-based toolkit inspired by @actions/core and actions/github, this library provides:

  • 🔍 A Context class to access GitHub Action runtime metadata
  • 📦 A WebhookPayload parser to load and normalize GitHub webhook events
  • 🧠 Models for structured data access (RepoIdentifier, IssueIdentifier, etc.)
  • 🐙 A helper for creating authenticated PyGitHub clients

🚀 Quick Start

Installation

pip install gh-actions-tool-kit

Usage

Actions Core Example

# ================================
# greeting.py
# ================================

from actions_tool_kit import get_input, set_output, notice, group

def main() -> None:
    name = get_input("name", required=True)
    with group("Greeting"):
        message = f"Hello, {name}!"
        notice(message, title="Python Action")
        set_output("greeting", message)

if __name__ == "__main__":
    main()

🧩 Features

Feature Description
get_input(name) Retrieves the value of an input defined in the GitHub Action with: section.
get_boolean_input(name) Retrieves a boolean input value (true or false) from the with: section, automatically parsing it.
set_output(name, value) Sets an output parameter for the step, which can be used by subsequent steps.
export_variable(name, value) Sets an environment variable that will be available to all subsequent steps in the job.
add_path(path) Prepends a directory to the system PATH variable for all subsequent steps in the job.
save_state(name, value) Saves state data that can be retrieved later using get_state() in a post-run step. Useful for cleanup or teardown.
get_state(name) Retrieves state saved by save_state() earlier in the job. Commonly used in post: scripts.
set_secret(secret) Masks a string from logs to prevent it from being exposed in the GitHub Actions output.
append_summary(markdown) Appends markdown content to the GitHub Actions job summary (visible in the UI under the job).
notice(message) Displays a notice message in the Actions logs.
warning(message) Displays a warning message in the Actions logs, usually in yellow.
error(message) Displays an error message in the Actions logs, usually in red.
debug(message) Sends a debug log message, visible only if step debugging is enabled.
group(title) / start_group(title) Starts a collapsible log group with a given title.
end_group() Ends the most recent collapsible log group.

Action Context Example

from actions_tool_kit import context

# Basic metadata
print(context.event_name)  # "pull_request"
print(context.sha)         # commit SHA
print(context.ref)         # branch ref
print(context.actor)       # user who triggered

# Repo info
print(context.repo.owner)  # "your-org"
print(context.repo.repo)   # "your-repo"

# Issue or PR
print(context.issue.number)  # 42

# Access webhook payload
print(context.payload.pull_request.get("title"))

🧩 Context Properties Features

Property Type Description
event_name str Event type (e.g., push, pull_request)
ref str Git ref that triggered the workflow
sha str Commit SHA
workflow str Workflow name
actor str Actor triggering the event
job str Job name
run_id int Unique run ID
run_number int Run number
run_attempt int Retry number for the run
api_url str GitHub API URL
server_url str GitHub server URL
graphql_url str GraphQL endpoint
repo RepoIdentifier owner and repo parsed
issue IssueIdentifier repo + issue/pull number
pr PullRequestIdentifier repo + PR number
head_branch str For PR events: source branch
base_branch str For PR events: target branch
sender Sender Structured sender info

📄 Webhook Payload Parser

  • The parse_payload() function:
    • Parses the JSON in $GITHUB_EVENT_PATH
    • Normalizes fields like repository, sender, issue, pull_request
    • Loads it into a structured WebhookPayload dataclass

[!NOTE] You don’t need to call this manually — Context does it for you.

🧱 Models

Class Description
RepoOwner login, name, extra
PayloadRepository name, owner, html_url, etc.
WebhookPayload Top-level GitHub event structure
RepoIdentifier {owner, repo}
IssueIdentifier {owner, repo, number}
PullRequestIdentifier {owner, repo, number}
Sender {type, login, extra}

Example Workflow

name: demo
on: [push]
jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Run Python Action
        env:
          name: <your name>
        run: python greeting.py

🙌 Credits

Inspired by @actions/core and actions/github

📄 License

MIT License © 2024 AB

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gh_actions_tool_kit-0.2.1.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gh_actions_tool_kit-0.2.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file gh_actions_tool_kit-0.2.1.tar.gz.

File metadata

  • Download URL: gh_actions_tool_kit-0.2.1.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gh_actions_tool_kit-0.2.1.tar.gz
Algorithm Hash digest
SHA256 94dbf12eedf8544fc2a2162f2b4f4e4469861e641b71e1a2ac324d852791ee5a
MD5 8820f60cb45f7e904be276f62e08b72b
BLAKE2b-256 6bdf1b2bc24138230e5aff2d9d727c01cdfeb2bad5a35b6694912703b6816899

See more details on using hashes here.

Provenance

The following attestation bundles were made for gh_actions_tool_kit-0.2.1.tar.gz:

Publisher: upload-to-pypi.yml on ab-git-actions/gh-actions-tool-kit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gh_actions_tool_kit-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gh_actions_tool_kit-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51e1d67331f54fa98a1134e34f1712b37fe782a7d37c217b256d53bcf4b40f36
MD5 6a34168ec2cd751715c84a567a9b55b3
BLAKE2b-256 94262320acc44ded0064796583b44807a205cc3636935db524e03e71a04230c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for gh_actions_tool_kit-0.2.1-py3-none-any.whl:

Publisher: upload-to-pypi.yml on ab-git-actions/gh-actions-tool-kit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page