feat: reuse release builds and add release notes
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-10-03 15:03:37 +02:00
parent f8e87c30b8
commit 7b30e92b2b
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
7 changed files with 343 additions and 39 deletions

2
Cargo.lock generated
View File

@ -991,7 +991,7 @@ dependencies = [
[[package]]
name = "rust_realease"
version = "0.1.3"
version = "0.1.4"
dependencies = [
"clap",
"console",

View File

@ -1,6 +1,6 @@
[package]
name = "rust_realease"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
publish = false
repository = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease"

View File

@ -8,18 +8,20 @@
## Install
Install Rust and Cargo, Git, Cross, and Docker or Podman. Install cargo-binstall when producing the default `tgz` artifacts and running end-to-end install verification. A working container backend is needed for release builds.
The preferred installation method is cargo-binstall, which downloads the matching prebuilt release artifact:
```bash
cargo install --path . --locked
cargo binstall --git https://proxy.dandaedre.com/gitea/idle-org/rust_realease \
rust_realease
rust_realease --help
```
The released `john-slop` binary can be installed directly from its Gitea repository with cargo-binstall:
To build and publish releases, also install Git, Cross, and Docker or Podman. Install cargo-binstall itself when producing the default `tgz` artifacts and running end-to-end installation verification. A working container backend is needed for release builds.
For development, or when a prebuilt artifact is unavailable for the host, install from this checkout:
```bash
cargo binstall --git https://proxy.dandaedre.com/gitea/idle-org/john-slop \
john-slop
cargo install --path . --locked
```
To run from this checkout without installing, use `cargo run -- SOURCE [OPTIONS]`.
@ -90,6 +92,8 @@ This runs host `cargo check` and `cargo test`, builds the selected targets with
Override the matrix with repeatable `--target TARGET` options. Use `--stop-after verify` for host checks alone.
A completed Cross build from a clean checkout is reused automatically when the Git commit and build settings still match. Packaging is repeated so the upload always gets a newly validated artifact set. Pass `--recompile` to ignore the completed-build cache and build every target again. Dirty checkouts are never reused.
Use `--artifact-format binary` when you only want raw executables, or `--artifact-format both` to publish raw executables alongside the default cargo-binstall archives:
```bash
@ -110,6 +114,15 @@ export GITEA_TOKEN
rust_realease ./my-tool
```
Add release notes inline or read them from a UTF-8 file:
```bash
rust_realease ./my-tool -m "Highlights and upgrade notes"
rust_realease ./my-tool --message-file CHANGELOG.md
```
The two message options are mutually exclusive. Supplying notes also updates the body when the tagged Gitea release already exists; omitting them preserves an existing body.
To publish `rust_realease` itself, install the current publisher and use the
repository wrapper. It reads the token from a file without placing it in the
process arguments and publishes both archive and standalone-binary assets:

View File

@ -89,9 +89,9 @@ For repositories whose build scripts need a selected Rust toolchain inside Cross
`--compile-only` runs through the package stage and then stops: it performs host checks, cross-compiles, and writes local artifacts without creating or uploading a release. It is the explicit artifact-build mode and conflicts with `--dry-run`, `--prepare-repo`, and `--stop-after`.
`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. Release builds use fresh target directories to avoid stale build-script output. `release` creates or reuses the Gitea release; `upload` uploads archives and checksums. The final stage downloads assets anonymously for public repositories or with Gitea authentication for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for public targets. `--skip-binstall-test` skips that install check, while retaining download verification.
`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. A successful Cross build from a clean checkout records its Git commit, package, targets, profile, lock mode, container engine, active Rust toolchain, and binary list. A later run with the same inputs reuses those regular binary files from Cargo's target directory; dirty checkouts, changed inputs, missing files, and invalid cache records build in a fresh target directory instead. `--recompile` explicitly bypasses a matching completed build. `release` creates or reuses the Gitea release; `upload` uploads archives and checksums. The final stage downloads assets anonymously for public repositories or with Gitea authentication for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for public targets. `--skip-binstall-test` skips that install check, while retaining download verification.
`--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when `GITEA_TOKEN` is set. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly.
`--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when `GITEA_TOKEN` is set. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. `-m/--message TEXT` sets release notes, while `--message-file PATH` reads them from a UTF-8 file; they are mutually exclusive. Notes are included when creating a release and replace the body of an existing tagged release when explicitly supplied. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly.
## Artifact contract

View File

@ -1,4 +1,5 @@
use crate::{config::Config, error::Error, policy, project::SelectedPackage, runner::Runner, ui};
use serde::{Deserialize, Serialize};
use std::{
env, fs,
path::{Path, PathBuf},
@ -11,6 +12,26 @@ pub struct TargetOutputs {
pub binaries: Vec<(String, PathBuf)>,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct BuildFingerprint {
format_version: u8,
commit: String,
package: String,
version: String,
binaries: Vec<String>,
targets: Vec<String>,
profile: String,
locked: bool,
engine: String,
toolchain: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
struct BuildCache {
fingerprint: BuildFingerprint,
target_dir: PathBuf,
}
pub fn prerequisites(
root: &Path,
config: &Config,
@ -163,24 +184,51 @@ pub fn build(
project: &SelectedPackage,
config: &Config,
engine: &str,
clean_commit: Option<&str>,
recompile: bool,
runner: &Runner,
) -> Result<Vec<TargetOutputs>, Error> {
let target_base = project.target_directory.join("binstall-publisher");
fs::create_dir_all(&target_base)
.map_err(|e| Error(format!("cannot create {}: {e}", target_base.display())))?;
let toolchain = executable("rustup").and_then(|_| {
runner
.text("rustup", &["show", "active-toolchain"], root)
.ok()
.and_then(|line| line.split_whitespace().next().map(str::to_owned))
});
let fingerprint = clean_commit.map(|commit| BuildFingerprint {
format_version: 1,
commit: commit.to_owned(),
package: project.name.clone(),
version: project.version.clone(),
binaries: project.binaries.clone(),
targets: config.targets.clone(),
profile: config.profile.clone(),
locked: config.locked,
engine: engine.to_owned(),
toolchain: toolchain.clone(),
});
let cache_path = target_base.join("release-build.json");
if !recompile {
if let Some(expected) = fingerprint.as_ref() {
if let Some(outputs) =
cached_outputs(&target_base, &cache_path, expected, project, config)?
{
ui::success(format_args!(
"Reusing completed release build for commit {} (use --recompile to rebuild)",
short_commit(&expected.commit)
));
return Ok(outputs);
}
}
}
let fresh = tempfile::Builder::new()
.prefix("build-")
.tempdir_in(&target_base)
.map_err(|e| Error(format!("cannot create fresh build directory: {e}")))?;
let target_dir = fresh.keep();
println!("Fresh build directory: {}", target_dir.display());
let toolchain = executable("rustup")
.and_then(|_| {
runner
.text("rustup", &["show", "active-toolchain"], root)
.ok()
})
.and_then(|line| line.split_whitespace().next().map(str::to_owned));
let mut environment = vec![("CROSS_CONTAINER_ENGINE", engine)];
if let Some(toolchain) = toolchain.as_deref() {
environment.push(("RUSTUP_TOOLCHAIN", toolchain));
@ -235,23 +283,113 @@ pub fn build(
"Cross builds complete in {:.1}s",
builds_started.elapsed().as_secs_f64()
));
if let Some(fingerprint) = fingerprint {
let cache = BuildCache {
fingerprint,
target_dir,
};
let encoded = serde_json::to_vec_pretty(&cache)
.map_err(|e| Error(format!("cannot encode release build cache: {e}")))?;
let mut temporary = tempfile::NamedTempFile::new_in(&target_base)
.map_err(|e| Error(format!("cannot create release build cache: {e}")))?;
use std::io::Write as _;
temporary
.write_all(&encoded)
.map_err(|e| Error(format!("cannot write release build cache: {e}")))?;
temporary
.persist(&cache_path)
.map_err(|e| Error(format!("cannot save release build cache: {e}")))?;
}
Ok(outputs)
}
fn cached_outputs(
target_base: &Path,
cache_path: &Path,
expected: &BuildFingerprint,
project: &SelectedPackage,
config: &Config,
) -> Result<Option<Vec<TargetOutputs>>, Error> {
match fs::symlink_metadata(cache_path) {
Ok(metadata) if metadata.file_type().is_file() => (),
Ok(_) => return Ok(None),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => {
return Err(Error(format!(
"cannot inspect release build cache {}: {error}",
cache_path.display()
)))
}
}
let encoded =
fs::read(cache_path).map_err(|e| Error(format!("cannot read release build cache: {e}")))?;
let cache: BuildCache = match serde_json::from_slice(&encoded) {
Ok(cache) => cache,
Err(_) => return Ok(None),
};
if &cache.fingerprint != expected {
return Ok(None);
}
let base = fs::canonicalize(target_base)
.map_err(|e| Error(format!("cannot resolve build cache directory: {e}")))?;
let target_dir = match fs::canonicalize(&cache.target_dir) {
Ok(path) if path.starts_with(&base) => path,
_ => return Ok(None),
};
let profile_dir = if config.profile == "dev" {
"debug"
} else {
&config.profile
};
let mut outputs = Vec::new();
for target in &config.targets {
let mut binaries = Vec::new();
for name in &project.binaries {
let binary = target_dir
.join(target)
.join(target)
.join(profile_dir)
.join(policy::binary_filename(name, target));
let resolved = match fs::canonicalize(&binary) {
Ok(path) if path.starts_with(&base) => path,
_ => return Ok(None),
};
match fs::symlink_metadata(&binary) {
Ok(metadata) if metadata.file_type().is_file() => {
binaries.push((name.clone(), resolved));
}
_ => return Ok(None),
}
}
outputs.push(TargetOutputs {
target: target.clone(),
binaries,
});
}
Ok(Some(outputs))
}
fn short_commit(commit: &str) -> &str {
commit.get(..12).unwrap_or(commit)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cross_command_scopes_package_and_uses_fresh_target_path() {
let project = SelectedPackage {
fn test_project(target_directory: PathBuf) -> SelectedPackage {
SelectedPackage {
name: "cli".into(),
version: "0.1.0".into(),
binaries: vec!["cli".into(), "helper".into()],
manifest: PathBuf::from("Cargo.toml"),
repository: None,
target_directory: PathBuf::from("target"),
};
let config = Config {
target_directory,
}
}
fn test_config() -> Config {
Config {
targets: vec!["x86_64-pc-windows-gnu".into()],
profile: "release".into(),
locked: true,
@ -259,7 +397,13 @@ mod tests {
token_env: "GITEA_TOKEN".into(),
package_format: crate::config::PackageFormat::Tgz,
repository_fallbacks: Vec::new(),
};
}
}
#[test]
fn cross_command_scopes_package_and_uses_fresh_target_path() {
let project = test_project(PathBuf::from("target"));
let config = test_config();
let args = cross_arguments(
&project,
&config,
@ -285,6 +429,61 @@ mod tests {
]
);
}
#[test]
fn completed_clean_build_is_reused_only_for_the_same_fingerprint() {
let temporary = tempfile::tempdir().unwrap();
let target_base = temporary.path().join("binstall-publisher");
let target_dir = target_base.join("build-complete");
let project = test_project(temporary.path().to_owned());
let config = test_config();
for binary in &project.binaries {
let path = target_dir
.join(&config.targets[0])
.join(&config.targets[0])
.join("release")
.join(policy::binary_filename(binary, &config.targets[0]));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, binary).unwrap();
}
let fingerprint = BuildFingerprint {
format_version: 1,
commit: "0123456789abcdef".into(),
package: project.name.clone(),
version: project.version.clone(),
binaries: project.binaries.clone(),
targets: config.targets.clone(),
profile: config.profile.clone(),
locked: config.locked,
engine: "podman".into(),
toolchain: Some("stable-x86_64-unknown-linux-gnu".into()),
};
let cache_path = target_base.join("release-build.json");
fs::write(
&cache_path,
serde_json::to_vec(&BuildCache {
fingerprint: fingerprint.clone(),
target_dir,
})
.unwrap(),
)
.unwrap();
let outputs = cached_outputs(&target_base, &cache_path, &fingerprint, &project, &config)
.unwrap()
.unwrap();
assert_eq!(outputs.len(), 1);
assert_eq!(outputs[0].binaries.len(), 2);
let different = BuildFingerprint {
commit: "different".into(),
..fingerprint
};
assert!(
cached_outputs(&target_base, &cache_path, &different, &project, &config)
.unwrap()
.is_none()
);
}
#[test]
fn cross_hooks_are_rejected() {
let doc: toml::Value =

View File

@ -46,6 +46,15 @@ pub struct Cli {
pub prerelease: bool,
#[arg(long)]
pub replace_assets: bool,
/// Ignore a matching completed build and compile every release target again.
#[arg(long)]
pub recompile: bool,
/// Release notes text.
#[arg(long, short = 'm', conflicts_with = "message_file")]
pub message: Option<String>,
/// Read release notes from a UTF-8 file.
#[arg(long, value_name = "PATH", conflicts_with = "message")]
pub message_file: Option<PathBuf>,
#[arg(long)]
pub keep_workdir: bool,
#[arg(long)]
@ -94,6 +103,15 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
"--prepare-repo is a separate mode; omit --stop-after.".into(),
));
}
let release_message = match args.message_file.as_deref() {
Some(path) => Some(std::fs::read_to_string(path).map_err(|e| {
Error(format!(
"cannot read release message file {}: {e}",
path.display()
))
})?),
None => args.message.clone(),
};
let acquisition_runner = Runner::new("GITEA_TOKEN").with_verbose(args.verbose);
let checkout = project::acquire_project(
&args.source,
@ -317,7 +335,23 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
return Ok(());
}
let engine = engine.ok_or_else(|| Error("No usable Cross container backend.".into()))?;
let outputs = build::build(&checkout.root, &selected, &config, &engine, &runner)?;
let commit = project::head_commit(&checkout.root, &runner)?;
let checkout_clean = runner
.text(
"git",
&["status", "--porcelain", "--untracked-files=normal"],
&checkout.root,
)?
.is_empty();
let outputs = build::build(
&checkout.root,
&selected,
&config,
&engine,
checkout_clean.then_some(commit.as_str()),
args.recompile,
&runner,
)?;
if args.stop_after == Some(Stage::Build) {
return Ok(());
}
@ -345,10 +379,15 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
if args.stop_after == Some(Stage::Package) {
return Ok(());
}
let commit = project::head_commit(&checkout.root, &runner)?;
let (api, private) =
publication.ok_or_else(|| Error("Publication client was not initialized.".into()))?;
let release = api.ensure_release(&tag, &commit, args.draft, args.prerelease)?;
let release = api.ensure_release(
&tag,
&commit,
args.draft,
args.prerelease,
release_message.as_deref(),
)?;
ui::success(format_args!("Release ready: {tag}"));
if args.stop_after == Some(Stage::Release) {
return Ok(());
@ -477,4 +516,27 @@ mod tests {
assert!(Cli::try_parse_from(arguments).is_err());
}
}
#[test]
fn release_message_inputs_and_recompile_flag_parse() {
let args = Cli::try_parse_from([
"rust_realease",
".",
"--recompile",
"-m",
"Changes in this release",
])
.unwrap();
assert!(args.recompile);
assert_eq!(args.message.as_deref(), Some("Changes in this release"));
assert!(Cli::try_parse_from([
"rust_realease",
".",
"--message",
"inline",
"--message-file",
"notes.md",
])
.is_err());
}
}

View File

@ -120,6 +120,7 @@ impl Gitea {
commit: &str,
draft: bool,
prerelease: bool,
message: Option<&str>,
) -> Result<RemoteRelease, Error> {
let encoded = encode(tag);
let remote = self
@ -134,19 +135,33 @@ impl Gitea {
"Remote tag commit differs from the checkout; refusing publication.".into(),
));
}
let release = if let Some(existing) = self.request(
let (release, existed) = if let Some(existing) = self.request(
Method::GET,
&format!("/releases/tags/{encoded}"),
None,
true,
)? {
existing
(existing, true)
} else {
self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease})), false)?
.ok_or_else(|| Error("Gitea release creation returned no response.".into()))?
(
self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease, "body": message.unwrap_or_default()})), false)?
.ok_or_else(|| Error("Gitea release creation returned no response.".into()))?,
false,
)
};
serde_json::from_value(release)
.map_err(|_| Error("Gitea release response is missing id or draft state.".into()))
let release: RemoteRelease = serde_json::from_value(release)
.map_err(|_| Error("Gitea release response is missing id or draft state.".into()))?;
if existed {
if let Some(message) = message {
self.request(
Method::PATCH,
&format!("/releases/{}", release.id),
Some(json!({"body": message})),
false,
)?;
}
}
Ok(release)
}
pub fn repository_private(&self) -> Result<bool, Error> {
@ -300,7 +315,10 @@ mod tests {
assert!(n > 0, "client closed before body");
bytes.extend_from_slice(&buffer[..n]);
}
requests.push(first);
requests.push(format!(
"{first}\n{}",
String::from_utf8_lossy(&bytes[header_end..header_end + length])
));
let reason = if reply.status == 200 {
"OK"
} else if reply.status == 201 {
@ -343,9 +361,13 @@ mod tests {
},
]);
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
let release = api.ensure_release("v0.1.0", "abc", false, true).unwrap();
let release = api
.ensure_release("v0.1.0", "abc", false, true, Some("Release notes"))
.unwrap();
assert_eq!(release.id, 7);
assert_eq!(handle.join().unwrap().len(), 3);
let requests = handle.join().unwrap();
assert_eq!(requests.len(), 3);
assert!(requests[2].contains(r#""body":"Release notes""#));
let (host, handle) = server(vec![
Reply {
@ -360,15 +382,23 @@ mod tests {
status: 200,
body: r#"{"id":9,"draft":true}"#,
},
Reply {
method: "PATCH",
path: "/gitea/api/v1/repos/team/demo/releases/9",
status: 200,
body: r#"{"id":9,"draft":true}"#,
},
]);
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
assert_eq!(
api.ensure_release("v0.1.0", "abc", false, false)
api.ensure_release("v0.1.0", "abc", false, false, Some("Updated notes"))
.unwrap()
.id,
9
);
assert_eq!(handle.join().unwrap().len(), 2);
let requests = handle.join().unwrap();
assert_eq!(requests.len(), 3);
assert!(requests[2].contains(r#""body":"Updated notes""#));
let _ = base;
}
@ -426,7 +456,7 @@ mod tests {
}]);
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
let error = api
.ensure_release("v0.1.0", "abc", false, false)
.ensure_release("v0.1.0", "abc", false, false, None)
.unwrap_err()
.to_string();
assert!(error.contains("HTTP 401"));
@ -448,7 +478,7 @@ mod tests {
}]);
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
let error = api
.ensure_release("v0.1.0", "abc", false, false)
.ensure_release("v0.1.0", "abc", false, false, None)
.unwrap_err()
.to_string();
assert!(error.contains(expected), "{error}");