发布到 crates.io

以 Markdown 格式查看

将您的公共 Fern Rust SDK 发布到 crates.io 注册表。按照本页面的步骤操作后, 您将在 crates.io 上发布一个版本化的 crate。

本页面假设您已经:

  • 初始化了 fern 文件夹,为您的 Rust SDK 创建了 GitHub 仓库,并在 generators.yml 中配置了 Rust 生成器组。请参阅 生成 SDK (Rust)
在 crates.io 上发布的版本化包

配置 generators.yml

1

配置 output 位置

generators.yml 中将输出位置从 local-file-system(默认值)更改为 crates,以指示 Fern 应该直接将您的 crate 发布到 crates.io 注册表:

generators.yml
1groups:
2 rust-sdk:
3 generators:
4 - name: fernapi/fern-rust-sdk
5 version: 0.35.0
6 output:
7 location: crates
2

添加唯一的 crate 名称

您的 crate 名称在 crates.io 注册表中必须是唯一的,否则发布 SDK 将失败。如果您还没有更新 crate 名称,请立即更新:

generators.yml
1groups:
2 rust-sdk:
3 generators:
4 - name: fernapi/fern-rust-sdk
5 version: 0.35.0
6 output:
7 location: crates
8 package-name: your-crate-name
3

配置 clientClassName

clientClassName 选项控制生成的客户端结构体名称。这是用户实例化您的 SDK 客户端时将使用的名称。

generators.yml
1groups:
2 rust-sdk:
3 generators:
4 - name: fernapi/fern-rust-sdk
5 version: 0.35.0
6 output:
7 location: crates
8 package-name: your-crate-name
9 config:
10 clientClassName: YourClientName # 必须使用帕斯卡命名法
4

添加仓库位置

generators.yml 中添加您的 GitHub 仓库路径:

generators.yml
1groups:
2 rust-sdk:
3 generators:
4 - name: fernapi/fern-rust-sdk
5 version: 0.35.0
6 output:
7 location: crates
8 package-name: your-crate-name
9 config:
10 clientClassName: YourClientName
11 github:
12 repository: your-org/your-rust-sdk

设置 crates.io 发布身份验证

1

登录 crates.io

使用您的 GitHub 账户登录 crates.io

2

导航到账户设置

  1. 点击右上角的头像。
  2. 选择 Account Settings
  3. 进入 API Tokens 部分。
3

创建新的 API token

  1. 点击 New Token
  2. 为您的 token 命名(例如,“Fern SDK Publishing”)。
  3. Scopes 下,选择 publish-new 来发布新 crate,选择 publish-update 来更新现有 crate。
  4. 点击 Generate Token
在 crates.io 上创建新的 API token
请立即保存您的新 token。离开页面后将不会再次显示。
4

配置 crates.io 身份验证 token

generators.yml 中添加 token: ${CRATES_IO_API_KEY},告诉 Fern 在发布到 crates.io 时使用 CRATES_IO_API_KEY 环境变量进行身份验证。

generators.yml
1groups:
2 rust-sdk:
3 generators:
4 - name: fernapi/fern-rust-sdk
5 version: 0.35.0
6 output:
7 location: crates
8 package-name: your-crate-name
9 token: ${CRATES_IO_API_KEY}
10 config:
11 clientClassName: YourClientName
12 github:
13 repository: your-org/your-rust-sdk

发布您的 SDK

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

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

1

设置身份验证

在 GitHub 中打开您的源仓库。点击仓库中的 Settings 选项卡。然后,在 Security 部分下,打开 Secrets and variables > Actions

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

2

为您的 crates.io token 添加密钥

  1. 选择 New repository secret
  2. 将您的密钥命名为 CRATES_IO_API_KEY
  3. 添加您在上面生成的对应 token。
  4. 点击 Add secret
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
1name: Publish Rust SDK
2
3on:
4workflow_dispatch:
5 inputs:
6 version:
7 description: "The version of the Rust SDK that you would like to release"
8 required: true
9 type: string
10
11jobs:
12release:
13 runs-on: ubuntu-latest
14 steps:
15 - name: Checkout repo
16 uses: actions/checkout@v4
17
18 - name: Install Fern CLI
19 run: npm install -g fern-api
20
21 - name: Release Rust SDK
22 env:
23 FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
24 CRATES_IO_API_KEY: ${{ secrets.CRATES_IO_API_KEY }}
25 run: |
26 fern generate --group rust-sdk --version ${{ inputs.version }} --log-level debug
5

重新生成并发布您的 SDK

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

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.

工作流完成后,您可以通过登录 crates.io,导航到 Dashboard,并在 My Crates 下查看您的新发布。

1

设置 crates.io 环境变量

在本地计算机上设置 CRATES_IO_API_KEY 环境变量:

$export CRATES_IO_API_KEY=your-actual-rust-token
2

重新生成并发布您的 SDK

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

$fern generate --group rust-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.

工作流完成后,您可以通过登录 crates.io,导航到 Dashboard,并在 My Crates 下查看您的新发布。