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

View File

@@ -48,7 +48,7 @@ pub fn build_binary_package(
}; };
// Create an ephemeral unshare context for all Local builds // 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)?) Some(ephemeral::EphemeralContextGuard::new(series)?)
} else { } else {
None 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(()) Ok(())
} }