pybuildkite

1.3.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

Initializes the Buildkite client with an API access token (preferably from an environment variable) and demonstrates how to fetch organization details and list builds for a specific pipeline. It also includes a commented-out example for creating a new build.

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}")

view raw JSON →