pybuildkite
pybuildkite is a Python wrapper library for interacting with the Buildkite CI/CD platform's REST API. It provides a programmatic interface to manage organizations, pipelines, builds, agents, and other Buildkite resources. The library is currently at version 1.3.0 and mirrors the functionality of the Buildkite API, which is primarily a REST API (currently v2). Its release cadence is driven by updates to the underlying Buildkite API and community contributions.
Warnings
- breaking The underlying Buildkite REST API transitioned from v1 to v2, introducing breaking changes such as renaming 'project' properties to 'pipeline' and removing certain endpoints. While `pybuildkite` v1.x largely aligns with Buildkite API v2, users migrating from very old `pybuildkite` versions or those whose code directly relied on v1 API semantics should consult `pybuildkite`'s release notes and the official Buildkite API migration guide (https://buildkite.com/docs/api/rest#migrating-from-v1-to-v2).
- gotcha When dealing with API endpoints that return large datasets, it's crucial to implement pagination. By default, `pybuildkite` might return a limited number of items per request (e.g., 100). Failing to handle pagination can lead to incomplete data retrieval.
- gotcha Buildkite API access tokens are sensitive credentials. Buildkite has updated its security policies, including new tokens being single-organization by default and deprecating retrieval of agent tokens via GraphQL for newly created tokens. Using compromised or overly permissive tokens is a significant security risk.
- gotcha The primary class for interacting with the Buildkite API is named `Buildkite` (note the lowercase 'k'). Incorrectly using `BuildKite` (uppercase 'K') due to case sensitivity will result in an `ImportError` or `NameError`.
Install
-
pip install pybuildkite
Imports
- Buildkite
from pybuildkite.buildkite import Buildkite
- BuildState
from pybuildkite.buildkite import BuildState
Quickstart
import os
from pybuildkite.buildkite import Buildkite, BuildState
# Get your Buildkite API access token from environment variables
api_access_token = os.environ.get('BUILDKITE_API_ACCESS_TOKEN', 'YOUR_API_ACCESS_TOKEN_HERE')
org_slug = os.environ.get('BUILDKITE_ORGANIZATION_SLUG', 'your-org-slug')
pipeline_slug = os.environ.get('BUILDKITE_PIPELINE_SLUG', 'your-pipeline-slug')
if api_access_token == 'YOUR_API_ACCESS_TOKEN_HERE':
print("Warning: Please set the BUILDKITE_API_ACCESS_TOKEN environment variable.")
print("You can generate one at: https://buildkite.com/user/api-access-tokens")
exit(1)
buildkite = Buildkite()
buildkite.set_access_token(api_access_token)
try:
# Get all info about a particular organization
org = buildkite.organizations().get_org(org_slug)
print(f"Organization Name: {org['name']}")
# List all running and scheduled builds for a particular pipeline
builds = buildkite.builds().list_all_for_pipeline(
org_slug, pipeline_slug, states=[BuildState.RUNNING, BuildState.SCHEDULED]
)
print(f"Found {len(builds)} running/scheduled builds for pipeline '{pipeline_slug}':")
for build in builds:
print(f" Build #{build['number']}: {build['state']} - {build['message']}")
# Example: Create a new build (uncomment and modify to use)
# new_build = buildkite.builds().create_build(
# org_slug, pipeline_slug, 'HEAD', 'main',
# clean_checkout=True, message="Triggered from pybuildkite quickstart!"
# )
# print(f"Created new build #{new_build['number']} in pipeline '{pipeline_slug}'.")
except Exception as e:
print(f"An error occurred: {e}")