Skip to content

Commit

Permalink
Remove docker container on build failure (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Mar 8, 2024
1 parent 37d67b3 commit d5cc2ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fix parsing of docker STDOUT - [#1526](https://github.com/paritytech/cargo-contract/pull/1526)
- Remove docker container on build failure - [#1531](https://github.com/paritytech/cargo-contract/pull/1531)

## [4.0.0-rc.3]

Expand Down
31 changes: 24 additions & 7 deletions crates/build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use bollard::{
CreateContainerOptions,
ListContainersOptions,
LogOutput,
RemoveContainerOptions,
},
errors::Error,
image::{
Expand Down Expand Up @@ -162,11 +163,27 @@ pub fn docker_build(args: ExecuteArgs) -> Result<BuildResult> {
)
.await?;

let mut build_result = run_build(&client, &container, &verbosity).await?;

update_build_result(&host_folder, &mut build_result)?;

update_metadata(&build_result, &verbosity, &image, &client).await?;
let build_result = async {
let mut build_result = run_build(&client, &container, &verbosity).await?;
update_build_result(&host_folder, &mut build_result)?;
update_metadata(&build_result, &verbosity, &image, &client).await?;
Ok::<BuildResult, anyhow::Error>(build_result)
}
.await;

let build_result = match build_result {
Ok(build_result) => build_result,
Err(e) => {
// Remove container to avoid leaving it in an incorrect state for
// subsequent calls
let options = Some(RemoveContainerOptions {
force: true,
..Default::default()
});
let _ = client.remove_container(&container, options).await;
return Err(e)
}
};

verbose_eprintln!(
verbosity,
Expand Down Expand Up @@ -305,7 +322,7 @@ async fn create_container(
let container_option = containers.first();

if container_option.is_some() {
return Ok(container_name);
return Ok(container_name)
}

let mount = Mount {
Expand Down Expand Up @@ -530,7 +547,7 @@ async fn show_pull_progress(
let status = info.status.unwrap_or_default();
if status.starts_with("Digest:") || status.starts_with("Status:") {
eprintln!("{}", status);
continue;
continue
}

if let Some(id) = info.id {
Expand Down

0 comments on commit d5cc2ef

Please sign in to comment.