Skip to content

SDK contract testing in GitHub Actions

Automatically run Speakeasy tests on SDK pull requests or other events in a GitHub repository via GitHub Actions.

github.com
A screenshot of a successful test check
github.com
A screenshot of a successful test comment

This requires previously completing the Github Setup. After completing the setup, navigate to the SDK repository and run the following command:

Terminal window
# It's okay to run this command multiple times if you have already configured tests locally
speakeasy configure tests

This command enables both generateTests and generateNewTests settings in your gen.yaml configuration file and produces a Github Actions workflow like the following that enables running SDK tests as a Github check on Pull Requests.

name: Test SDKs
permissions:
checks: write
contents: write
pull-requests: write
statuses: write
on:
workflow_dispatch:
inputs:
target:
description: Specific target to test
type: string
pull_request:
paths:
- "**"
branches:
- main
jobs:
test:
uses: speakeasy-api/sdk-generation-action/.github/workflows/sdk-test.yaml@v15
with:
target: ${{ github.event.inputs.target }}
secrets:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
speakeasy_api_key: ${{ secrets.SPEAKEASY_API_KEY }}

Ensuring Tests Run on Automated PR Creation

Section titled “Ensuring Tests Run on Automated PR Creation”

To ensure that testing checks run by default when an SDK PR is created, implement one of the following options.

Installing the Speakeasy Github App and granting the App access to the SDK repository enables triggering testing runs after a PR is created. To install the app, visit this link or follow instructions in the CLI or dashboard.

Another option is to create a Github Personal Access Token (PAT) that will be used to create PRs in the SDK repository. This is a workaround recommended by Github.

  1. Create a fine-grained PAT with Pull requests Read/Write permissions. Ensure it has access to the SDK repository. Setting this to no expiration is recommended.
github.com
Setting up the PAT
github.com
Providing access to a specific repo
github.com
Pull request permissions
  1. In all SDK repositories, navigate to Settings > Secrets and variables > Actions and save the PAT as a Repository secret under the name PR_CREATION_PAT.
github.com
Providing access to a specific repo
  1. In all sdk_generation.yaml workflows, add the pr-creation-token as a provided secret at the bottom.
secrets:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
speakeasy_api_key: ${{ secrets.SPEAKEASY_API_KEY }}
pr_creation_pat: ${{ secrets.PR_CREATION_PAT }}

If your generation action is running in direct mode where SDK updates get immediately pushed to main, testing will run as part of the generation action. If tests fail, the generation action will fail and not push your SDK changes to main.