跳到导航

发布到 NuGet

以 Markdown 格式查看

将您的公开 Fern C#/.NET SDK 发布到 NuGet 注册表。按照本页面的步骤操作后, 您将在 NuGet 上发布一个版本化的包。

在 NuGet 上发布的版本化包

本页面假设您已有:

  • 一个已初始化的 fern 文件夹、一个用于 C#/.NET SDK 的 GitHub 仓库,以及在 generators.yml 中的一个 C#/.NET 生成器组。参见 生成 SDK (C#/.NET)。

配置 SDK 包设置

您需要更新 generators.yml 文件来配置包名称、输出位置和客户端名称,以便 NuGet 发布。您的 generators.yml 应该放在您的源代码仓库中(或本地机器上),而不是包含 C#/.NET SDK 代码的仓库中。

1

配置 output 位置

在您的 C#/.NET SDK 的 group 中,将输出位置从 local-file-system(默认值)更改为 nuget,以指示 Fern 应该直接将您的包发布到 npm 注册表:

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
2

添加唯一的包名称

您的包名称在 NuGet 仓库中必须是唯一的,否则将您的 SDK 发布到 NuGet 将失败。

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
package-name: your-package-name
3

配置 client-class-name

client-class-name 选项控制生成的客户端的名称。这是客户使用的导入您的 SDK 的名称(import { your-client-name } from 'your-package-name';)。

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
package-name: your-package-name
config:
client_class_name: YourClientName # 必须是 PascalCase

生成 NuGet API 密钥

1

登录 NuGet

登录 NuGet 或创建一个新账户。

2

添加新密钥

  1. 点击您的个人资料图片。

  2. 选择 API Keys,然后 Create。

  3. 为您的密钥命名。

  4. 选择 Push > Push new packages and package versions 作为 Select Scopes 类型。

  5. 在 Select Packages > Glob Patten 下输入 *。

    替换现有的 NuGet 包

    如果您要覆盖现有包,可以选择相关包而不是输入 *。

  6. 点击 Create。

创建新的 API 密钥
保存您的新密钥 - 离开页面后将不再显示。

配置 NuGet 发布

1

添加仓库位置

添加包含您的 C#/.NET SDK 的 GitHub 仓库路径:

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
package-name: your-package-name
config:
client_class_name: YourClientName
github:
repository: your-org/company-csharp
2

配置 NuGet 身份验证密钥

将 api-key: ${NUGET_API_KEY} 添加到 generators.yml,告诉 Fern 在发布到 NuGet 注册表时使用 NUGET_API_KEY 环境变量进行身份验证。

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
package-name: your-package-name
api-key: ${NUGET_API_KEY}
config:
client_class_name: YourClientName
github:
repository: your-org/company-csharp
3

选择您的发布模式

Optionally set the mode to control how Fern handles SDK publishing:

  • mode: release (default): Fern generates code, commits to the default branch (or the branch you specify), and tags a release automatically
  • mode: pull-request (recommended): Fern generates code and creates a PR for you to review before release
  • mode: push: Fern generates code and pushes to a branch you specify for you to review before release

You can also configure other settings, like the reviewers or license. Refer to the output.github reference for more information.

generators.yml
groups:
csharp-sdk:
generators:
- name: fern-csharp-sdk
version: 2.92.8
output:
location: nuget
package-name: your-package-name
api-key: ${NUGET_API_KEY}
config:
client_class_name: YourClientName
github:
repository: your-org/company-csharp
mode: push
branch: your-branch-name # mode: push 时必需

发布您的 SDK

决定如何将您的 SDK 发布到 NuGet。您可以使用 GitHub 工作流进行自动化发布,或通过 CLI 直接发布。

通过 GitHub Actions 设置发布工作流,这样您就可以直接从源代码仓库触发新的 SDK 发布。

1

设置身份验证

在 GitHub 中打开您的源代码仓库。点击 Settings 标签。然后,在 Security 部分下,打开 Secrets and variables > Actions。

您也可以使用 URL https://github.com/<your-repo>/settings/secrets/actions。

2

为您的 NuGet API 密钥添加密钥

  1. 选择 New repository secret。
  2. 将您的密钥命名为 NUGET_API_KEY。
  3. 添加您在上面生成的相应 API 密钥。
  4. 点击 Add secret。
NUGET_API_KEY 密钥
3

为您的 Fern Token 添加密钥

  1. 选择 New repository secret。
  2. 将您的密钥命名为 FERN_TOKEN。
  3. 添加您的 Fern token。如果您还没有,可以通过运行 fern token 来生成一个。默认情况下,fern_token 是为 fern.config.json 中列出的组织生成的。
  4. 点击 Add secret。
4

设置新的工作流

设置一个可以从 GitHub UI 手动触发的 CI 工作流。在您的仓库中,导航到 Actions。选择 New workflow,然后 Set up workflow yourself。添加类似于这样的工作流:

.github/workflows/publish.yml
name: Publish C#/.NET SDK
on:
workflow_dispatch:
inputs:
version:
description: "您想要发布的 C#/.NET SDK 的版本"
required: true
type: string
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install Fern CLI
run: npm install -g fern-api
- name: Release C#/.NET SDK
env:
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: |
fern generate --group csharp-sdk --version ${{ inputs.version }} --log-level debug

您也可以配置您的工作流为 on: [push] 执行。参见 Vapi 的 npm 发布 GitHub Action 作为示例。

5

重新生成并发布您的 SDK

导航到 Actions 标签,选择您刚创建的工作流,指定一个版本号,然后点击 Run workflow。这将重新生成您的 SDK。

运行 TS 发布工作流

The rest of the release process depends on your chosen mode:

  • Release mode (default): If you didn’t specify a mode or set mode: release, no further action is required. Fern automatically tags the new release with your specified version number and initiates the publishing workflow in your SDK repository.

  • Pull request or push mode: If you set mode: pull-request or mode: push, Fern creates a pull request or pushes to a branch respectively. Review and merge the PR (pull-request) or branch (push), then tag a new release to initiate the publishing workflow in your SDK repository.

工作流完成后,您可以通过登录 NuGet 并导航到 Manage Packages 来查看您的新发布。

1

设置 npm 环境变量

在本地机器上设置 NUGET_API_KEY 环境变量:

export NUGET_API_KEY=your-actual-nuget-api-key
2

重新生成并发布您的 SDK

重新生成您的 SDK,指定版本:

fern generate --group csharp-sdk --version <version>

The rest of the release process depends on your chosen mode:

  • Release mode (default): If you didn’t specify a mode or set mode: release, no further action is required. Fern automatically tags the new release with your specified version number and initiates the publishing workflow in your SDK repository.

  • Pull request or push mode: If you set mode: pull-request or mode: push, Fern creates a pull request or pushes to a branch respectively. Review and merge the PR (pull-request) or branch (push), then tag a new release to initiate the publishing workflow in your SDK repository.

工作流完成后,您可以通过登录 NuGet 并导航到 Manage Packages 来查看您的新发布。