Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: try to publish a crate until it succeeds #843

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions tools/release-operator/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
use std::thread::sleep;
use std::time::{Duration, Instant};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -168,30 +169,52 @@ impl Crate {
fn submit(&self, token: &SecUtf8, dry_run: bool) -> anyhow::Result<()> {
log::info!("{self} publishing new version");

let mut command = Command::new("cargo");
command
.arg("publish")
.args(["--token", token.unsecure()])
.current_dir(&self.path);
let ours = self.get_local_version()?;
let delay = Duration::from_secs(10);
let start_time = Instant::now();
let timeout = Duration::from_secs(600);
let mut attempt = 1;
let maximum_attempts = 10;

if dry_run {
command.arg("--dry-run");
}
loop {
if Instant::now() - start_time > timeout {
bail!("{self} could not be published to the registry within {timeout:?}");
}

let exit_status = command.status()?;
if attempt > maximum_attempts {
bail!("{self} could not be published to the registry; tried {attempt} times");
}

if !exit_status.success() {
match exit_status.code() {
Some(code) => {
bail!("`cargo publish` failed with exit code {code}")
}
None => {
bail!("`cargo publish` was terminated by signal")
let mut command = Command::new("cargo");
command
.arg("publish")
.args(["--token", token.unsecure()])
.current_dir(&self.path);

if dry_run {
command.arg("--dry-run");
}

let exit_status = command.status()?;

if !exit_status.success() {
match exit_status.code() {
Some(code) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: no need to check which code came back as there is only a single failure code https://doc.rust-lang.org/cargo/commands/cargo-publish.html#exit-status

log::warn!("`cargo publish` failed with exit code {code}; trying again in {delay:?}");
sleep(delay);
attempt += 1;
continue;
}
None => {
bail!("`cargo publish` was terminated by signal")
}
}
}

log::info!("{self} published as {ours}");
break;
}

let ours = self.get_local_version()?;
let delay = Duration::from_secs(10);
let start_time = Instant::now();
let timeout = Duration::from_secs(600);
Expand Down