Skip to content

Commit

Permalink
feat: Display list of image refs at the end of a build
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Sep 7, 2024
1 parent 2f3edeb commit 4296085
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 59 deletions.
2 changes: 1 addition & 1 deletion process/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl BuildDriver for Driver {
impl_build_driver!(login())
}

fn build_tag_push(opts: &BuildTagPushOpts) -> Result<()> {
fn build_tag_push(opts: &BuildTagPushOpts) -> Result<Vec<String>> {
impl_build_driver!(build_tag_push(opts))
}
}
Expand Down
41 changes: 20 additions & 21 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl BuildDriver for DockerDriver {
Ok(())
}

fn build_tag_push(opts: &BuildTagPushOpts) -> Result<()> {
fn build_tag_push(opts: &BuildTagPushOpts) -> Result<Vec<String>> {
trace!("DockerDriver::build_tag_push({opts:#?})");

if opts.squash {
Expand Down Expand Up @@ -243,63 +243,62 @@ impl BuildDriver for DockerDriver {
],
);

let mut final_image = String::new();

match (opts.image.as_deref(), opts.archive_path.as_deref()) {
let final_images = match (opts.image.as_deref(), opts.archive_path.as_deref()) {
(Some(image), None) => {
if opts.tags.is_empty() {
final_image.push_str(image);
let images = if opts.tags.is_empty() {
cmd!(command, "-t", image);
string_vec![image]
} else {
final_image.push_str(
format!("{image}:{}", opts.tags.first().map_or("", String::as_str))
.as_str(),
);

opts.tags.iter().for_each(|tag| {
cmd!(command, "-t", format!("{image}:{tag}"));
});
}
opts.tags
.iter()
.map(|tag| format!("{image}:{tag}"))
.collect()
};
let first_image = images.first().unwrap();

if opts.push {
cmd!(
command,
"--output",
format!(
"type=image,name={image},push=true,compression={},oci-mediatypes=true",
"type=image,name={first_image},push=true,compression={},oci-mediatypes=true",
opts.compression
)
);
} else {
cmd!(command, "--load");
}
images
}
(None, Some(archive_path)) => {
final_image.push_str(archive_path);

cmd!(command, "--output", format!("type=oci,dest={archive_path}"));
string_vec![archive_path]
}
(Some(_), Some(_)) => bail!("Cannot use both image and archive path"),
(None, None) => bail!("Need either the image or archive path set"),
}
};
let display_image = final_images.first().unwrap(); // There will always be at least one image

cmd!(command, ".");

trace!("{command:?}");
if command
.status_image_ref_progress(&final_image, "Building Image")
.status_image_ref_progress(display_image, "Building Image")
.into_diagnostic()?
.success()
{
if opts.push {
info!("Successfully built and pushed image {}", final_image);
info!("Successfully built and pushed image {}", display_image);
} else {
info!("Successfully built image {}", final_image);
info!("Successfully built image {}", display_image);
}
} else {
bail!("Failed to build image {}", final_image);
bail!("Failed to build image {}", display_image);
}
Ok(())
Ok(final_images)
}
}

Expand Down
12 changes: 6 additions & 6 deletions process/drivers/github_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,32 @@ impl CiDriver for GithubDriver {
.iter()
.flat_map(|alt| {
string_vec![
&**alt,
format!("{alt}-{os_version}"),
format!("{timestamp}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
format!("{alt}-{os_version}"),
&**alt,
]
})
.collect(),
(false, None, Ok(event_name), Ok(event_num)) if event_name == PR_EVENT => {
vec![
format!("{short_sha}-{os_version}"),
format!("pr-{event_num}-{os_version}"),
format!("{short_sha}-{os_version}"),
]
}
(false, None, _, _) => {
vec![
format!("{short_sha}-{os_version}"),
format!("br-{ref_name}-{os_version}"),
format!("{short_sha}-{os_version}"),
]
}
(false, Some(alt_tags), Ok(event_name), Ok(event_num)) if event_name == PR_EVENT => {
alt_tags
.iter()
.flat_map(|alt| {
vec![
format!("{short_sha}-{alt}-{os_version}"),
format!("pr-{event_num}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
]
})
.collect()
Expand All @@ -99,8 +99,8 @@ impl CiDriver for GithubDriver {
.iter()
.flat_map(|alt| {
vec![
format!("{short_sha}-{alt}-{os_version}"),
format!("br-{ref_name}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
]
})
.collect(),
Expand Down
12 changes: 6 additions & 6 deletions process/drivers/gitlab_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ impl CiDriver for GitlabDriver {
.iter()
.flat_map(|alt| {
string_vec![
&**alt,
format!("{alt}-{os_version}"),
format!("{timestamp}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
format!("{alt}-{os_version}"),
&**alt,
]
})
.collect(),
(false, None, Ok(mr_iid), Ok(pipeline_source)) if pipeline_source == MR_EVENT => {
vec![
format!("{short_sha}-{os_version}"),
format!("mr-{mr_iid}-{os_version}"),
format!("{short_sha}-{os_version}"),
]
}
(false, None, _, _) => {
vec![
format!("{short_sha}-{os_version}"),
format!("br-{ref_name}-{os_version}"),
format!("{short_sha}-{os_version}"),
]
}
(false, Some(alt_tags), Ok(mr_iid), Ok(pipeline_source))
Expand All @@ -97,8 +97,8 @@ impl CiDriver for GitlabDriver {
.iter()
.flat_map(|alt| {
vec![
format!("{short_sha}-{alt}-{os_version}"),
format!("mr-{mr_iid}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
]
})
.collect()
Expand All @@ -107,8 +107,8 @@ impl CiDriver for GitlabDriver {
.iter()
.flat_map(|alt| {
vec![
format!("{short_sha}-{alt}-{os_version}"),
format!("br-{ref_name}-{alt}-{os_version}"),
format!("{short_sha}-{alt}-{os_version}"),
]
})
.collect(),
Expand Down
45 changes: 42 additions & 3 deletions process/drivers/local_driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blue_build_utils::string_vec;
use blue_build_utils::{cmd, string_vec};
use log::trace;
use miette::bail;

Expand All @@ -25,12 +25,39 @@ impl CiDriver for LocalDriver {
fn generate_tags(opts: &GenerateTagsOpts) -> miette::Result<Vec<String>> {
trace!("LocalDriver::generate_tags({opts:?})");
let os_version = Driver::get_os_version(opts.oci_ref)?;
let timestamp = blue_build_utils::get_tag_timestamp();
let short_sha = commit_sha();

Ok(opts.alt_tags.as_ref().map_or_else(
|| string_vec![format!("local-{os_version}")],
|| {
let mut tags = string_vec![
"latest",
&timestamp,
format!("{os_version}"),
format!("{timestamp}-{os_version}"),
];

if let Some(short_sha) = &short_sha {
tags.push(format!("{short_sha}-{os_version}"));
}

tags
},
|alt_tags| {
alt_tags
.iter()
.flat_map(|alt| string_vec![format!("local-{alt}-{os_version}")])
.flat_map(|alt| {
let mut tags = string_vec![
&**alt,
format!("{alt}-{os_version}"),
format!("{timestamp}-{alt}-{os_version}"),
];
if let Some(short_sha) = &short_sha {
tags.push(format!("{short_sha}-{alt}-{os_version}"));
}

tags
})
.collect()
},
))
Expand All @@ -46,3 +73,15 @@ impl CiDriver for LocalDriver {
Ok(String::from("localhost"))
}
}

fn commit_sha() -> Option<String> {
let output = cmd!("git", "rev-parse", "--short", "HEAD").output().ok()?;

if output.status.success() {
String::from_utf8(output.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
}
20 changes: 14 additions & 6 deletions process/drivers/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::{ExitStatus, Output},
};

use blue_build_utils::{constants::COSIGN_PUB_PATH, retry};
use blue_build_utils::{constants::COSIGN_PUB_PATH, retry, string_vec};
use log::{debug, info, trace};
use miette::{bail, miette, Context, IntoDiagnostic, Result};
use oci_distribution::Reference;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub trait BuildDriver {
///
/// # Errors
/// Will error if building, tagging, or pusing fails.
fn build_tag_push(opts: &BuildTagPushOpts) -> Result<()> {
fn build_tag_push(opts: &BuildTagPushOpts) -> Result<Vec<String>> {
trace!("BuildDriver::build_tag_push({opts:#?})");

let full_image = match (opts.archive_path.as_ref(), opts.image.as_ref()) {
Expand All @@ -96,22 +96,26 @@ pub trait BuildDriver {
info!("Building image {full_image}");
Self::build(&build_opts)?;

if !opts.tags.is_empty() && opts.archive_path.is_none() {
let image_list: Vec<String> = if !opts.tags.is_empty() && opts.archive_path.is_none() {
let image = opts
.image
.as_ref()
.ok_or_else(|| miette!("Image is required in order to tag"))?;
debug!("Tagging all images");

let mut image_list = Vec::with_capacity(opts.tags.len());

for tag in opts.tags.as_ref() {
debug!("Tagging {} with {tag}", &full_image);
let tagged_image = format!("{image}:{tag}");

let tag_opts = TagOpts::builder()
.src_image(&full_image)
.dest_image(format!("{image}:{tag}"))
.dest_image(&tagged_image)
.build();

Self::tag(&tag_opts)?;
image_list.push(tagged_image);

if opts.push {
let retry_count = if opts.retry_push { opts.retry_count } else { 0 };
Expand All @@ -132,9 +136,13 @@ pub trait BuildDriver {
})?;
}
}
}

Ok(())
image_list
} else {
string_vec![&full_image]
};

Ok(image_list)
}
}

Expand Down
5 changes: 3 additions & 2 deletions process/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ where
}
}

fn gen_random_ansi_color() -> u8 {
#[must_use]
pub fn gen_random_ansi_color() -> u8 {
// ANSI extended color range
// https://www.ditig.com/publications/256-colors-cheat-sheet
const LOW_END: u8 = 21; // Blue1 #0000ff rgb(0,0,255) hsl(240,100%,50%)
Expand All @@ -404,7 +405,7 @@ fn gen_random_ansi_color() -> u8 {
rand::thread_rng().gen_range(LOW_END..=HIGH_END)
}

fn color_str<T>(text: T, ansi_color: u8) -> String
pub fn color_str<T>(text: T, ansi_color: u8) -> String
where
T: AsRef<str>,
{
Expand Down
Loading

0 comments on commit 4296085

Please sign in to comment.