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

Support CICD #477

Merged
merged 2 commits into from
Apr 16, 2024
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
64 changes: 42 additions & 22 deletions src/commands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub struct Args {
/// Don't attach to the log stream
detach: bool,

#[clap(short, long)]
/// Only stream build logs and exit after it's done
cicd: bool,

#[clap(short, long)]
/// Service to deploy to (defaults to linked service)
service: Option<String>,
Expand All @@ -61,6 +65,11 @@ pub struct UpErrorResponse {
pub message: String,
}

enum UpExitReason {
Deployed,
Failed,
}

pub async fn get_service_to_deploy(
configs: &Configs,
client: &Client,
Expand Down Expand Up @@ -294,34 +303,45 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
return Ok(());
}

// Stream both build and deploy logs
let build_deployment_id = deployment_id.clone();
let deploy_deployment_id = deployment_id.clone();

let tasks = vec![
tokio::task::spawn(async move {
if let Err(e) =
stream_build_logs(build_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream build logs: {}", e);
}
}),
tokio::task::spawn(async move {
// Create vector of log streaming tasks
// Always stream build logs
let mut tasks = vec![tokio::task::spawn(async move {
if let Err(e) =
stream_build_logs(build_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream build logs: {}", e);
}
})];

// Stream deploy logs only if cicd flag is not set
if !args.cicd {
tasks.push(tokio::task::spawn(async move {
if let Err(e) =
stream_deploy_logs(deploy_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream deploy logs: {}", e);
}
}),
];
}));
}

// If the build fails, we want to terminate the process
tokio::task::spawn(async move {
match wait_for_failure(deployment_id.clone()).await {
Ok(_) => {
println!("{}", "Build failed".red().bold());
std::process::exit(1);
}
match wait_for_exit_reason(deployment_id.clone()).await {
Ok(reason) => match reason {
UpExitReason::Deployed => {
if args.cicd {
println!("{}", "Deploy complete".green().bold());
std::process::exit(0);
}
}
_ => {
println!("{}", "Build failed".red().bold());
std::process::exit(1);
}
},
Err(e) => {
eprintln!("Failed to fetch deployment status: {}", e);
}
Expand All @@ -333,19 +353,19 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
Ok(())
}

async fn wait_for_failure(deployment_id: String) -> Result<(), anyhow::Error> {
async fn wait_for_exit_reason(deployment_id: String) -> Result<UpExitReason, anyhow::Error> {
let configs = Configs::new()?;
let client = GQLClient::new_authorized(&configs)?;

loop {
tokio::time::sleep(Duration::from_secs(5)).await;

if let Ok(deployment) = get_deployment(&client, &configs, deployment_id.clone()).await {
if deployment.status == DeploymentStatus::FAILED {
break;
match deployment.status {
DeploymentStatus::SUCCESS => return Ok(UpExitReason::Deployed),
DeploymentStatus::FAILED => return Ok(UpExitReason::Failed),
_ => {}
}
}
}

Ok(())
}
Loading