deb: cleanup ephemeral context only on success
All checks were successful
CI / build (push) Successful in 11m39s

This commit is contained in:
2026-01-13 14:54:56 +01:00
parent 87ce0f648b
commit b4a60e2ae2
2 changed files with 48 additions and 21 deletions

View File

@@ -12,6 +12,7 @@ use xz2::read::XzDecoder;
pub struct EphemeralContextGuard {
previous_context: String,
chroot_path: PathBuf,
build_succeeded: bool,
}
impl EphemeralContextGuard {
@@ -41,6 +42,7 @@ impl EphemeralContextGuard {
Ok(Self {
previous_context: current_context_name,
chroot_path,
build_succeeded: false,
})
}
@@ -222,6 +224,11 @@ impl EphemeralContextGuard {
Ok(())
}
/// Mark the build as successful, which will trigger chroot cleanup on drop
pub fn mark_build_successful(&mut self) {
self.build_succeeded = true;
}
}
impl Drop for EphemeralContextGuard {
@@ -232,31 +239,46 @@ impl Drop for EphemeralContextGuard {
log::error!("Failed to restore context {}: {}", self.previous_context, e);
}
// Remove chroot directory
// We use the restored context to execute the cleanup command
let result = context::current()
.command("sudo")
.arg("rm")
.arg("-rf")
.arg(&self.chroot_path)
.status();
// Remove chroot directory only if build succeeded
if self.build_succeeded {
log::debug!(
"Build succeeded, removing chroot directory: {}",
self.chroot_path.display()
);
let result = context::current()
.command("sudo")
.arg("rm")
.arg("-rf")
.arg(&self.chroot_path)
.status();
match result {
Ok(status) => {
if !status.success() {
match result {
Ok(status) => {
if !status.success() {
log::error!(
"Failed to remove chroot directory {}",
self.chroot_path.display()
);
} else {
log::debug!(
"Successfully removed chroot directory: {}",
self.chroot_path.display()
);
}
}
Err(e) => {
log::error!(
"Failed to remove chroot directory {}",
self.chroot_path.display()
"Failed to execute cleanup command for {}: {}",
self.chroot_path.display(),
e
);
}
}
Err(e) => {
log::error!(
"Failed to execute cleanup command for {}: {}",
self.chroot_path.display(),
e
);
}
} else {
log::debug!(
"Build did not succeed or was not marked as successful, keeping chroot directory: {}",
self.chroot_path.display()
);
}
}
}

View File

@@ -48,7 +48,7 @@ pub fn build_binary_package(
};
// Create an ephemeral unshare context for all Local builds
let _guard = if mode == BuildMode::Local {
let mut guard = if mode == BuildMode::Local {
Some(ephemeral::EphemeralContextGuard::new(series)?)
} else {
None
@@ -82,6 +82,11 @@ pub fn build_binary_package(
}
}
// Mark build as successful to trigger chroot cleanup
if let Some(ref mut g) = guard {
g.mark_build_successful();
}
Ok(())
}