Skip to content
Draft
Show file tree
Hide file tree
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
75 changes: 64 additions & 11 deletions process/drivers/github_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,36 @@ impl CiDriver for GithubDriver {
}

fn generate_tags(opts: GenerateTagsOpts) -> Result<Vec<Tag>> {
use blue_build_utils::tagging::{TagMetadata, apply_tagging_policies, resolve_tag_template};

let metadata = TagMetadata {
tag: None,
os_version: opts.os_version,
timestamp: opts.timestamp,
short_sha: opts.short_sha,
};

// 1. Manual Tags (Verbatim + Template Resolution)
if let Some(tags) = opts.tags {
return tags
.iter()
.map(|t| resolve_tag_template(t, &metadata).parse())
.collect::<Result<Vec<Tag>>>();
}

// 2. Tagging Policies (if provided)
if let (Some(alt_tags), Some(policies)) = (opts.alt_tags, opts.tagging) {
return apply_tagging_policies(alt_tags, policies, &metadata);
}

// 3. Fallback (Legacy uBlue Logic)
const PR_EVENT: &str = "pull_request";
let timestamp = blue_build_utils::get_tag_timestamp();
let os_version = Driver::get_os_version()
.oci_ref(opts.oci_ref)
.call()
.inspect(|v| trace!("os_version={v}"))?;
let ref_name = get_env_var(GITHUB_REF_NAME)
.inspect(|v| trace!("{GITHUB_REF_NAME}={v}"))?
.replace('/', "_");
let short_sha = {
let mut short_sha = get_env_var(GITHUB_SHA).inspect(|v| trace!("{GITHUB_SHA}={v}"))?;
short_sha.truncate(7);
short_sha
};
let short_sha = opts.short_sha.unwrap_or_default();
let os_version = opts.os_version;
let timestamp = opts.timestamp;

let tags = match (
Self::on_default_branch(),
Expand All @@ -63,7 +79,7 @@ impl CiDriver for GithubDriver {
(true, None, _, _) => {
string_vec![
"latest",
&timestamp,
timestamp,
format!("{os_version}"),
format!("{timestamp}-{os_version}"),
format!("{short_sha}-{os_version}"),
Expand Down Expand Up @@ -310,12 +326,49 @@ mod test {
GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.maybe_alt_tags(alt_tags.as_deref())
.os_version("41")
.timestamp(&*TIMESTAMP)
.short_sha(COMMIT_SHA)
.platform(Platform::LinuxAmd64)
.build(),
)
.unwrap();
tags.sort();

assert_eq!(tags, expected);
}

#[test]
fn generate_tags_policy() {
use blue_build_utils::tagging::TaggingPolicy;

setup_default_branch();
let oci_ref: Reference = "ghcr.io/ublue-os/silverblue-main".parse().unwrap();
let alt_tags = vec!["stable".parse::<Tag>().unwrap()];
let policies = vec![TaggingPolicy {
match_tag: "stable".to_string(),
tags: vec!["{tag}-{os_version}".to_string(), "{timestamp}".to_string()],
}];

let mut tags = GithubDriver::generate_tags(
GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.alt_tags(&alt_tags)
.tagging(&policies)
.os_version("41")
.timestamp("20240101")
.platform(Platform::LinuxAmd64)
.build(),
)
.unwrap();
tags.sort();

let mut expected = vec![
"stable-41".parse::<Tag>().unwrap(),
"20240101".parse::<Tag>().unwrap(),
];
expected.sort();

assert_eq!(tags, expected);
}
}
35 changes: 30 additions & 5 deletions process/drivers/gitlab_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,33 @@ impl CiDriver for GitlabDriver {
}

fn generate_tags(opts: GenerateTagsOpts) -> Result<Vec<Tag>> {
use blue_build_utils::tagging::{TagMetadata, apply_tagging_policies, resolve_tag_template};

let metadata = TagMetadata {
tag: None,
os_version: opts.os_version,
timestamp: opts.timestamp,
short_sha: opts.short_sha,
};

// 1. Manual Tags (Verbatim + Template Resolution)
if let Some(tags) = opts.tags {
return tags
.iter()
.map(|t| resolve_tag_template(t, &metadata).parse())
.collect::<Result<Vec<Tag>>>();
}

// 2. Tagging Policies (if provided)
if let (Some(alt_tags), Some(policies)) = (opts.alt_tags, opts.tagging) {
return apply_tagging_policies(alt_tags, policies, &metadata);
}

// 3. Fallback (Legacy uBlue Logic)
const MR_EVENT: &str = "merge_request_event";
let os_version = Driver::get_os_version().oci_ref(opts.oci_ref).call()?;
let timestamp = blue_build_utils::get_tag_timestamp();
let short_sha =
get_env_var(CI_COMMIT_SHORT_SHA).inspect(|v| trace!("{CI_COMMIT_SHORT_SHA}={v}"))?;
let os_version = opts.os_version;
let timestamp = opts.timestamp;
let short_sha = opts.short_sha.unwrap_or_default();
let ref_name = get_env_var(CI_COMMIT_REF_NAME)
.inspect(|v| trace!("{CI_COMMIT_REF_NAME}={v}"))?
.replace('/', "_");
Expand All @@ -66,7 +88,7 @@ impl CiDriver for GitlabDriver {
(true, None, _, _) => {
string_vec![
"latest",
&timestamp,
timestamp,
format!("{os_version}"),
format!("{timestamp}-{os_version}"),
format!("{short_sha}-{os_version}"),
Expand Down Expand Up @@ -315,6 +337,9 @@ mod test {
GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.maybe_alt_tags(alt_tags.as_deref())
.os_version("41")
.timestamp(&*TIMESTAMP)
.short_sha(COMMIT_SHA)
.platform(Platform::LinuxAmd64)
.build(),
)
Expand Down
30 changes: 26 additions & 4 deletions process/drivers/local_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,40 @@ impl CiDriver for LocalDriver {
}

fn generate_tags(opts: GenerateTagsOpts) -> Result<Vec<Tag>> {
use blue_build_utils::tagging::{TagMetadata, apply_tagging_policies, resolve_tag_template};

let short_sha = opts.short_sha.map(|s| s.to_string()).or_else(commit_sha);
let metadata = TagMetadata {
tag: None,
os_version: opts.os_version,
timestamp: opts.timestamp,
short_sha: short_sha.as_deref(),
};

// 1. Manual Tags (Verbatim + Template Resolution)
if let Some(tags) = opts.tags {
return tags
.iter()
.map(|t| resolve_tag_template(t, &metadata).parse())
.collect::<Result<Vec<Tag>>>();
}

// 2. Tagging Policies (if provided)
if let (Some(alt_tags), Some(policies)) = (opts.alt_tags, opts.tagging) {
return apply_tagging_policies(alt_tags, policies, &metadata);
}

trace!("LocalDriver::generate_tags({opts:?})");
let os_version = Driver::get_os_version().oci_ref(opts.oci_ref).call()?;
let timestamp = blue_build_utils::get_tag_timestamp();
let short_sha = commit_sha();
let os_version = opts.os_version;
let timestamp = opts.timestamp;

opts.alt_tags
.as_ref()
.map_or_else(
|| {
let mut tags = string_vec![
"latest",
&timestamp,
timestamp,
format!("{os_version}"),
format!("{timestamp}-{os_version}"),
];
Expand Down
12 changes: 11 additions & 1 deletion process/drivers/opts/ci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blue_build_utils::{container::Tag, platform::Platform};
use blue_build_utils::{container::Tag, platform::Platform, tagging::TaggingPolicy};
use bon::Builder;
use oci_client::Reference;

Expand All @@ -10,6 +10,16 @@ pub struct GenerateTagsOpts<'scope> {
#[builder(into)]
pub alt_tags: Option<&'scope [Tag]>,

#[builder(into)]
pub tags: Option<&'scope [String]>,

#[builder(into)]
pub tagging: Option<&'scope [TaggingPolicy]>,

pub os_version: &'scope str,
pub timestamp: &'scope str,
pub short_sha: Option<&'scope str>,

pub platform: Option<Platform>,
}

Expand Down
12 changes: 12 additions & 0 deletions recipe/src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

use blue_build_utils::{
constants::COSIGN_IMAGE_VERSION, container::Tag, platform::Platform, secret::Secret,
tagging::TaggingPolicy,
};
use bon::Builder;
use cached::proc_macro::cached;
Expand Down Expand Up @@ -59,6 +60,17 @@ pub struct Recipe {
#[builder(into)]
pub alt_tags: Option<Vec<Tag>>,

/// Exact tags to add to the image.
///
/// This will override any automatic tagging logic in the drivers.
#[serde(alias = "tags", skip_serializing_if = "Option::is_none")]
#[builder(into)]
pub tags: Option<Vec<String>>,

/// Custom tagging policies for expanding alt-tags.
#[serde(skip_serializing_if = "Option::is_none")]
pub tagging: Option<Vec<TaggingPolicy>>,

/// The version of nushell to use for modules.
#[serde(skip_serializing_if = "Option::is_none", rename = "nushell-version")]
pub nushell_version: Option<MaybeVersion>,
Expand Down
22 changes: 22 additions & 0 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,32 @@ impl BuildCommand {
);

let recipe = &Recipe::parse(recipe_path)?;
let timestamp = &blue_build_utils::get_tag_timestamp();
let os_version = &Driver::get_os_version()
.oci_ref(&recipe.base_image_ref()?)
.call()
.inspect(|v| trace!("os_version={v}"))?;
let short_sha = {
let mut short_sha = blue_build_utils::get_env_var("GITHUB_SHA")
.or_else(|_| blue_build_utils::get_env_var("CI_COMMIT_SHA"))
.unwrap_or_default();
short_sha.truncate(7);
if short_sha.is_empty() {
None
} else {
Some(short_sha)
}
};

let tags = &Driver::generate_tags(
GenerateTagsOpts::builder()
.oci_ref(&recipe.base_image_ref()?)
.maybe_alt_tags(recipe.alt_tags.as_deref())
.maybe_tags(recipe.tags.as_deref())
.maybe_tagging(recipe.tagging.as_deref())
.os_version(os_version)
.timestamp(timestamp)
.maybe_short_sha(short_sha.as_deref())
.maybe_platform(self.platform.first().copied())
.build(),
)?;
Expand Down
1 change: 1 addition & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod platform;
pub mod secret;
pub mod semver;
pub mod syntax_highlighting;
pub mod tagging;
#[cfg(feature = "test")]
pub mod test_utils;
pub mod traits;
Expand Down
Loading
Loading