发布rust公共仓库到crates

  • 3 分钟阅读
  • 标签: 
  • rust

前言

  • https://github.com/emo-crab/nvd-rs项目进入稳定维护阶段了,其中在这个工作区里nvd-cvesnvd-cpenvd-cvssnvd-cwenvd-api这几个库都在项目中使用频繁,每次在构建的时候都要作为依赖拷贝到docker里面,要是修改了一下文档导致没有匹配到docker的缓存,那就要重新构建,非常浪费时间。所以打算将这几个常用的库发布到https://crates.io/

注册crates

  • 打开https://crates.io/后点击注册使用github登录,这是会发送验证邮件到你的邮箱,激活后就可以使用了。

Untitled

登录验证Cargo

Untitled

  • 然后使用cargo login命令,如果你设置了cargo的国内镜像,这时你就会发现
➜  nvd-rs git:(main) cargo login 
error: crates-io is replaced with non-remote-registry source registry `rsproxy`;
include `--registry crates-io` to use crates.ioy locally
  • 这是我的cargo配置,使用了rsproxy镜像,只要把它都注释掉就可以了。
➜  ~ cat .cargo/config 
[source.crates-io]
replace-with = 'rsproxy'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
[build]
rustc-wrapper = "/usr/bin/sccache"
➜  nvd-rs git:(main) cargo login
    Updating crates.io index
please paste the token found on https://crates.io/me below
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
       Login token for `crates.io` saved

发布整个工作区

  • 因为我的项目都放在一个工作区里,cargo publish命令需要加上-p参数一个一个发布,非常的麻烦,所以要装一个cargo子命令cargo-workspaces
➜  nvd-rs git:(main) cargo install cargo-workspaces
  • 在当前根目录的Cargo.toml文件,配置工作区成员,排除掉不公开的库。
[workspace.package]
#name = "nvd-rs" #改这个
version = "0.1.0"
edition = "2021"
authors = ["Kali-Team [email protected]>"]
include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
license-file = "LICENSE"
license = "GPL-3.0-only"
[workspace]
members = ["nvd-api", "nvd-cpe", "nvd-cves", "nvd-cvss", "nvd-cwe"]
exclude = ["helper", "nvd-cpe/schema", "nvd-server", "nvd-yew"]
resolver = "2"
➜  nvd-rs git:(main) cargo workspaces publish --all --force '*' --from-git --yes
info already published nvd-cpe v0.1.0
info already published nvd-cvss v0.1.0

GitHub Action

  • 使用调度触发,将CARGO_REGISTRY_TOKEN设置为cargo登录时复制的token就可以了。
name: Publish

on:
  workflow_dispatch:

jobs:

  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: stellar/actions/rust-cache@main
      - run: rustup update
      - uses: stellar/binaries@v12
        with:
          name: cargo-workspaces
          version: 0.2.35
      - run: cargo workspaces publish --all --force '*' --from-git --yes
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

注意事项

  • 如果是工作区,当前的cargo一定要为:[workspace.package],不然会连工作区一起上传到crates。在不想公开的库设置publish = false为假
[package]
name = "tldextract-cli"
version.workspace = true
edition.workspace = true
authors.workspace = true
include.workspace = true
readme.workspace = true
license.workspace = true
description.workspace = true
homepage.workspace = true
repository.workspace = true
publish = false

效果

Untitled

参考