cargo release
Cargo release
cargo-release 是一个用于发布 Rust 包的工具,它允许您使用 Git 标签和版本号来管理您的软件包版本。
usage
要使用 cargo release 更新版本号,创建 Git 标签,并将更改推送到远程仓库,但不发布到 crates.io,您可以使用以下命令和选项:
cargo release <LEVEL> --execute --no-publish --no-verify
或者,如果您想设置特定的版本号而不是按级别递增,可以使用:
cargo release <VERSION> --execute --no-publish --no-verify
解释:
-
cargo release: 这是cargo-release子命令的基本命令,用于启动发布流程。 -
<LEVEL>或<VERSION>:<LEVEL>: 指定版本递增的级别。可以是
-
major -
minor -
patch -
release -
rc -
beta -
alpha -
例如,使用
patch将使版本号从x.y.z递增到x.y.(z+1)。<VERSION>: 直接指定您想要设置的版本号。例如,使用1.2.3将把版本设置为1.2.3。
-
--execute: 这个选项告诉cargo release实际执行所有步骤。默认情况下,cargo release运行在 dry-run 模式,只进行模拟,不进行实际更改。务必加上--execute才能真正更新版本号、创建标签和推送。 -
--no-publish: 这个选项指示cargo release不要 运行cargo publish命令。这将阻止您的包发布到 crates.io。 -
--no-verify: 建议添加此选项,以跳过构建验证步骤。由于您不打算发布到 crates.io,因此可以跳过验证以加快流程。如果您希望在发布 到您自己的仓库 之前进行验证,可以移除此选项。
示例:
1. 递增补丁版本并执行发布流程,但不发布到 crates.io:
cargo release patch --execute --no-publish --no-verify
2. 设置特定版本号并执行发布流程,但不发布到 crates.io:
cargo release 1.2.4 --execute --no-publish --no-verify
其他有用的选项 (可选):
-
--sign: 如果您想对 Git 提交和标签进行签名,可以添加此选项。这需要您配置了 GPG 密钥。cargo release patch --execute --no-publish --no-verify --sign -
--no-push: 如果您只想更新版本号和创建本地 Git 标签,但 不 推送到远程仓库,可以添加此选项。如果您需要稍后手动推送,可以使用此选项。在您的问题中,您提到要推送到仓库,所以通常不需要此选项。cargo release patch --execute --no-publish --no-verify --no-push -
--allow-branch <GLOB[,...]>: 默认情况下,cargo release仅允许在master或main分支上运行。如果您想在其他分支上运行cargo release,可以使用此选项指定允许的分支名称模式。例如,允许在develop分支上发布:cargo release patch --execute --no-publish --no-verify --allow-branch develop
完整流程示例 (假设您要递增补丁版本并推送到远程仓库,但不发布到 crates.io):
-
确保您的 Git 仓库是干净的,并且您位于要发布的分支上。
-
运行以下命令:
cargo release patch --execute --no-publish --no-verify -
cargo release将会执行以下操作:- 更新您项目
Cargo.toml文件中的版本号。 - 提交版本号更新的更改。
- 创建一个带有新版本号的 Git 标签。
- 将提交和标签推送到您配置的远程仓库。
- 不会 执行
cargo publish,因此您的包不会发布到 crates.io。
- 更新您项目
请务必仔细检查 cargo release 的输出,特别是在第一次使用 --execute 选项时,以确保它执行了您期望的操作。
您可以先尝试不加 --execute 运行命令,以 dry-run 模式预览将要发生的变化。
cargo-release replace
当我们想要在 cargo-release 时, 不仅仅更改 Cargo.toml 和 Cargo.lock 中的 crate 版本号时。
比如: 我在 README 中也写我们的 crate 的安装方法。
### Installation
### Add this to your `Cargo.toml`:
[dependencies]
path_macro2 = "0.1.0"
那么我们自然希望 README 中的版本号 0.1.0 也会随 cargo release 的操作而自动 version bump。
正好,cargo release 也考虑到了这样的情况, 为我们提供了 pre-release replacements 这样的前置 hook。
Cargo subcommand for you to smooth your release process.
Usage: cargo release [OPTIONS] [LEVEL|VERSION]
cargo release <STEP>
Steps:
# ...
replace Perform pre-release replacements
因此在我们 crate 的 Cargo.toml 中加上如下的规则即可:
[package.metadata.release]
pre-release-replacements = [
{file="README.md", search="path_macro2 = \"[0-9]+\\.[0-9]+\\.[0-9]+[^\"]*\"", replace="path_macro2 = \"{{version}}\""},
]
运行 cargo release patch -x, Cargo.toml,Cargo.lock,README.md中的 version 都自动 bump 了。
diff --git a/Cargo.lock b/Cargo.lock
index 28ec980..a9806f1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,4 +4,4 @@ version = 4
[[package]]
name = "path_macro2"
-version = "0.1.0"
+version = "0.1.1"
diff --git a/Cargo.toml b/Cargo.toml
index 6fa70b0..69c2365 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "path_macro2"
-version = "0.1.0"
+version = "0.1.1"
edition = "2024"
authors = ["yunique unic<yuniqueunic@gmail.com>"]
license = "MIT OR Apache-2.0"
diff --git a/README.md b/README.md
index 0ec70e1..6fb7814 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ Add this to your `Cargo.toml`:
```toml
[dependencies]
-path_macro2 = "0.1.0"
+path_macro2 = "0.1.1"
cargo-release 的安装
您可以使用以下命令安装 cargo-release:
cargo install cargo-release
cargo-release 的参数
Cargo subcommand for you to smooth your release process.
Usage: cargo release [OPTIONS] [LEVEL|VERSION]
cargo release <STEP>
Steps:
changes Print commits since last tag
version Bump crate versions
replace Perform pre-release replacements
hook Run pre-release hooks
commit Commit the specified packages
publish Publish the specified packages
owner Ensure owners are set on specified packages
tag Tag the released commits
push Push tags/commits to remote
config Dump workspace configuration
help Print this message or the help of the given subcommand(s)
Arguments:
[LEVEL|VERSION]
Either bump by LEVEL or set the VERSION for all selected packages
Possible values:
- major: Increase the major version (x.0.0)
- minor: Increase the minor version (x.y.0)
- patch: Increase the patch version (x.y.z)
- release: Remove the pre-version (x.y.z)
- rc: Increase the rc pre-version (x.y.z-rc.M)
- beta: Increase the beta pre-version (x.y.z-beta.M)
- alpha: Increase the alpha pre-version (x.y.z-alpha.M)
Options:
--manifest-path <PATH>
Path to Cargo.toml
-p, --package <SPEC>
Package to process (see `cargo help pkgid`)
--workspace
Process all packages in the workspace
--exclude <SPEC>
Exclude packages from being processed
--unpublished
Process all packages whose current version is unpublished
-m, --metadata <METADATA>
Semver metadata
-x, --execute
Actually perform a release. Dry-run mode is the default
--no-confirm
Skip release confirmation and version preview
--prev-tag-name <NAME>
The name of tag for the previous release
-c, --config <PATH>
Custom config file
--isolated
Ignore implicit configuration files
-Z <FEATURE>
Unstable options
--sign
Sign both git commit and tag
--dependent-version <ACTION>
Specify how workspace dependencies on this crate should be handed
Possible values:
- upgrade: Always upgrade dependents
- fix: Upgrade when the old version requirement no longer applies
--allow-branch <GLOB[,...]>
Comma-separated globs of branch names a release can happen from
--certs-source <CERTS_SOURCE>
Indicate what certificate store to use for web requests
Possible values:
- webpki: Use certs from Mozilla's root certificate store
- native: Use certs from the system root certificate store
-q, --quiet...
Pass many times for less log output
-v, --verbose...
Pass many times for more log output
By default, it'll report info. Passing `-v` one time adds debug logs, `-vv` adds trace logs.
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
Commit:
--sign-commit
Sign git commit
Publish:
--no-publish
Do not run cargo publish on release
--registry <NAME>
Cargo registry to upload to
--no-verify
Don't verify the contents by building them
--features <FEATURES>
Provide a set of features that need to be enabled
--all-features
Enable all features via `all-features`. Overrides `features`
--target <TRIPLE>
Build for the target triple
Tag:
--no-tag
Do not create git tag
--sign-tag
Sign git tag
--tag-prefix <PREFIX>
Prefix of git tag, note that this will override default prefix based on sub-directory
--tag-name <NAME>
The name of the git tag
Push:
--no-push
Do not run git push in the last step
--push-remote <NAME>
Git remote to push