前言
创建部分就先略过,毕竟都能找到这儿了,这部分肯定熟。作者主要分享下如何通过 Github Actions 完成自动编译发布及其中可能的一些坑。
一. 前期准备
1. 不要重名!
是的,第一个要避免的就是先上 crates.io 搜搜看自己想出来的 crate name 别人有没有用了,如果有那很遗憾,改名吧!
2. 一个 crate repo
比如这里我们准备的这个 wincent-rs,至于它是干什么的,这不重要!
3. 一个 crates.io 账号
- 访问 crates.io
- 使用 GitHub 账号登录
- 找到
Account Settings -> API Tokens
- 点击
Create API Token
- 记下这个 token,后面会用到
二. 更新 Crate
1. 更新项目信息
编辑 Cargo.toml,以下为示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [package] name = "my-awesome-lib" version = "0.1.0" edition = "2021" authors = ["Your Name <your.email@example.com>"] description = "A brief description of your library" license = "MIT" repository = "https://github.com/username/my-awesome-lib" documentation = "https://docs.rs/my-awesome-lib" readme = "README.md" keywords = ["awesome", "library", "example"] categories = ["development-tools"]
[dependencies]
[package.metadata.docs.rs] default-target = "x86_64-pc-windows-gnu" targets = ["x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc"]
|
2. 更新项目文档
编辑 README.md,以下为示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| # My Awesome Library
A brief description of what your library does.
## Installation
Add this to your `Cargo.toml`:
````toml [dependencies] my-awesome-lib = "0.1.0" ````
## Usage
````rust use my_awesome_lib::awesome_function;
fn main() { let result = awesome_function(42); println!("Result: {}", result); } ````
## License
This project is licensed under the MIT License - see the LICENSE file for details.
|
生成并预览文档
1
| cargo doc --no-deps --open
|
三. GitHub Actions CI/CD 配置
1. 添加 Secrets
在 GitHub 仓库中配置必要的 secrets:
- 访问仓库的 Settings -> Secrets and variables -> Actions -> Repository secrets
- 添加新的 secret:
- 名称:
CARGO_REGISTRY_TOKEN
- 值:你的 crates.io API token,这里就是之前生成的那个 token
2. 创建编译发布 Action
在项目根目录创建 .github/workflows/publish.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| name: Build and Publish
on: push: tags: - 'v*'
jobs: test: name: Test runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Install Rust uses: dtolnay/rust-toolchain@stable with: toolchain: stable - name: Build run: cargo build --verbose - name: Run tests run: cargo test --verbose - name: Run clippy run: cargo clippy -- -D warnings - name: Check formatting run: cargo fmt -- --check
publish: name: Publish needs: test runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Install Rust uses: dtolnay/rust-toolchain@stable with: toolchain: stable - name: Publish to crates.io shell: bash env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} run: cargo publish --token "$CARGO_REGISTRY_TOKEN"
|
3. 创建源码发布 Action
在项目根目录创建 .github/workflows/archive.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| name: Release Source Code
on: workflow_run: workflows: ["Build and Publish"] types: - completed branches: [ "main" ]
jobs: build: runs-on: windows-latest if: ${{ github.event.workflow_run.conclusion == 'success' && startsWith(github.event.workflow_run.head_branch, 'v') }}
steps: - name: Checkout code uses: actions/checkout@v4 with: ref: ${{ github.event.workflow_run.head_branch }}
- name: Upload Release Assets uses: softprops/action-gh-release@v1 with: tag_name: ${{ github.event.workflow_run.head_branch }} draft: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
这样当我们在 main 分支上创建新的 tag 时,会自动触发工作流,并发布到 crates.io,同时将源码打包发布至 Github Release。
记得手动更新 cargo.toml 中版本号!!作者暂时没想到什么特别好的方法自动升级并不报错通过 workflow。
1 2 3 4 5
| git commit -m "Bump version to x.y.z"
git tag v0.1.0 git push origin v0.1.0
|
四. 总结
这是作者目前初步探索出来的一套方案,尚待优化,希望对你有所帮助