Skip to content

Test Code Samples

Test Code Samples #15

---
name: Test Code Samples
permissions:
contents: read
on:
pull_request:
paths:
- "src/code-samples/**"
- ".github/workflows/test-code-samples.yml"
workflow_dispatch:
schedule:
# Run every Sunday at 00:00 UTC
- cron: "0 0 * * 0"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test-code-samples:
# GitHub does not expose repository secrets to workflows triggered from fork PRs.
# These tests require provider API keys for some samples, so skip on forks.
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false)
runs-on: ubuntu-latest
timeout-minutes: 15
services:
postgres:
image: pgvector/pgvector:pg17
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Get modified code sample paths
id: files
run: |
if [[ "${{ github.event_name }}" == "schedule" ]]; then
echo "run_all=true" >> "$GITHUB_OUTPUT"
echo "Weekly run: testing all code samples"
else
echo "run_all=false" >> "$GITHUB_OUTPUT"
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
git fetch origin "${{ github.event.pull_request.base.ref }}"
BASE=$(git merge-base HEAD origin/${{ github.event.pull_request.base.ref }})
else
BASE="${{ github.event.before }}"
fi
FILES=$(git diff --name-only "$BASE" HEAD -- src/code-samples/ \
| grep -E '\.(py|ts|java|kt)$' \
| tr '\n' ' ' || true)
{ echo "files<<FILESEND"; echo "$FILES"; echo "FILESEND"; } >> "$GITHUB_OUTPUT"
if [[ -n "$FILES" ]]; then
echo "Modified code samples: $FILES"
else
echo "No modified .py, .ts, .java, or .kt files in src/code-samples/"
fi
fi
- name: Set up Python and uv
uses: ./.github/actions/uv_setup
with:
python-version: "3.13"
- name: Install project dependencies
run: uv sync
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Set up Java (for JBang)
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"
- name: Install JBang
uses: jbangdev/setup-jbang@v0.1.1
- name: Wait for PostgreSQL
run: |
for i in $(seq 1 30); do
if python3 -c "
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect(('127.0.0.1', 5432))
s.close()
except OSError:
exit(1)
"; then
echo "PostgreSQL is ready"
exit 0
fi
echo "Waiting for PostgreSQL... ($i/30)"
sleep 2
done
echo "PostgreSQL did not become ready"
exit 1
- name: Test code samples
id: test
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
LANGSMITH_API_KEY: ${{ secrets.LANGSMITH_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
POSTGRES_URI: postgresql://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable
TAVILY_API_KEY: ${{ secrets.TAVILY_API_KEY }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
DAYTONA_API_KEY: ${{ secrets.DAYTONA_API_KEY }}
DAYTONA_API_URL: ${{ secrets.DAYTONA_API_URL }}
run: |
if [[ "${{ steps.files.outputs.run_all }}" == "true" ]]; then
echo "Running all code samples..."
make test-code-samples
else
FILES="${{ steps.files.outputs.files }}"
FILES=$(echo "$FILES" | tr -d '\n' | xargs)
if [[ -z "$FILES" ]]; then
echo "No code samples to test"
exit 0
fi
make test-code-samples FILES="$FILES"
fi
# Creates a Linear issue when the weekly scheduled test fails.
# Required setup: Add LINEAR_API_KEY (repo secret) and LINEAR_TEAM_KEY (repo variable)
create-linear-ticket:
needs: test-code-samples
if: failure() && github.event_name == 'schedule'
runs-on: ubuntu-latest
steps:
- name: Create Linear issue
uses: ctriolo/action-create-linear-issue@v1.3
with:
linear-api-key: ${{ secrets.LINEAR_API_KEY }}
linear-team-key: ${{ vars.LINEAR_TEAM_KEY }}
linear-issue-title: "Weekly code samples test failed"
linear-issue-description: |
The scheduled weekly test of all code samples in `src/code-samples/` failed.
**Workflow run:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
Please investigate and fix the failing samples.
linear-attachment-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
linear-attachment-title: "Failed workflow run"