deb: cleanup ephemeral context only on success
All checks were successful
CI / build (push) Successful in 11m39s
All checks were successful
CI / build (push) Successful in 11m39s
This commit is contained in:
@@ -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,8 +239,12 @@ 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 {
|
||||||
|
log::debug!(
|
||||||
|
"Build succeeded, removing chroot directory: {}",
|
||||||
|
self.chroot_path.display()
|
||||||
|
);
|
||||||
let result = context::current()
|
let result = context::current()
|
||||||
.command("sudo")
|
.command("sudo")
|
||||||
.arg("rm")
|
.arg("rm")
|
||||||
@@ -248,6 +259,11 @@ impl Drop for EphemeralContextGuard {
|
|||||||
"Failed to remove chroot directory {}",
|
"Failed to remove chroot directory {}",
|
||||||
self.chroot_path.display()
|
self.chroot_path.display()
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
log::debug!(
|
||||||
|
"Successfully removed chroot directory: {}",
|
||||||
|
self.chroot_path.display()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -258,5 +274,11 @@ impl Drop for EphemeralContextGuard {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log::debug!(
|
||||||
|
"Build did not succeed or was not marked as successful, keeping chroot directory: {}",
|
||||||
|
self.chroot_path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user