> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiIxZTQ3MDk1MS0yOWY0LTQxYjItYmYzZC00NDMxY2VlMGRkMGQiLCJleHAiOjE3Nzg0OTMxNTksImlhdCI6MTc3ODQ5Mjg1OX0.nSjUvdrKzXu_ijmv0bK5m3-atrPSKIrgYvY7MzYfzxc
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 同步你的 AsyncAPI 规范

保持你的 AsyncAPI 规范与代码库同步对于维护准确的 SDK 和文档至关重要。Fern 提供了几种自动化选项来简化这个过程。

## GitHub Actions

使用 Fern 的 GitHub Action 在你的 AsyncAPI 规范发生变更时自动更新 SDK 和文档：

```yaml title=".github/workflows/fern.yml"
name: Fern

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  fern-check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        
      - name: Check AsyncAPI spec
        uses: fern-api/action@v0
        with:
          command: check
        env:
          FERN_TOKEN: ${{ secrets.FERN_TOKEN }}

  fern-generate:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        
      - name: Generate SDKs and docs
        uses: fern-api/action@v0
        with:
          command: generate
        env:
          FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
```

## Webhook 集成

设置 webhook 以在 AsyncAPI 规范更新时触发 SDK 生成：

```yaml title="generators.yml" {4-8}
api:
  specs:
    - spec: asyncapi.yml
  github:
    repository: your-org/your-repo
  webhooks:
    - url: https://your-api.com/webhooks/fern
      events: [generate]
  generators:
    - name: fernapi/fern-typescript-sdk
      version: 0.8.8
      output:
        location: npm
        package-name: "@your-org/sdk"
```

## 从源自动同步

配置 Fern 自动从各种源拉取你的 AsyncAPI 规范：

### 从 URL

```yaml title="generators.yml" {3-4}
api:
  specs:
    - spec: https://api.yourcompany.com/asyncapi.yml
      auto-sync: true
  generators:
    - name: fernapi/fern-typescript-sdk
      version: 0.8.8
```

### 从 git 仓库

```yaml title="generators.yml" {3-7}
api:
  specs:
    - spec:
        git:
          repository: https://github.com/your-org/api-specs
          path: asyncapi/api.yml
          branch: main
  generators:
    - name: fernapi/fern-typescript-sdk
      version: 0.8.8
```

## CI/CD 集成

### CircleCI

```yaml title=".circleci/config.yml" {15-23}
version: 2.1

orbs:
  fern: fernapi/fern@1.0

workflows:
  version: 2
  build-and-test:
    jobs:
      - build
      - test:
          requires:
            - build
      - fern/generate:
          requires:
            - test
          filters:
            branches:
              only: main
          context:
            - fern-context
```

### GitLab CI

```yaml title=".gitlab-ci.yml" {13-20}
stages:
  - build
  - test
  - generate

variables:
  FERN_TOKEN: $FERN_TOKEN

build:
  stage: build
  script:
    - echo "Building application..."

generate-sdks:
  stage: generate
  image: fernapi/fern:latest
  script:
    - fern generate
  only:
    - main
```

## 定时更新

设置定时更新以确保你的 SDK 保持最新：

```yaml title=".github/workflows/scheduled-update.yml"
name: Scheduled AsyncAPI Update

on:
  schedule:
    - cron: '0 2 * * 1'  # Every Monday at 2 AM UTC
  workflow_dispatch:

jobs:
  update-specs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        
      - name: Update AsyncAPI specs
        run: |
          curl -o fern/asyncapi/asyncapi.yml https://api.yourcompany.com/asyncapi.yml
          
      - name: Generate with latest spec
        uses: fern-api/action@v0
        with:
          command: generate
        env:
          FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
```

## 监控变更

跟踪你的 AsyncAPI 规范的变更：

```yaml title="generators.yml" {4-9}
api:
  specs:
    - spec: asyncapi.yml
  change-detection:
    enabled: true
    breaking-changes: error
    notifications:
      slack: ${{ secrets.SLACK_WEBHOOK }}
      email: team@yourcompany.com
  generators:
    - name: fernapi/fern-typescript-sdk
      version: 0.8.8
```

这确保了对你的 AsyncAPI 规范的任何破坏性变更都会被检测到，并且在变更传播到你的 SDK 和文档之前通知相应的团队成员。