fix: use container skopeo (#110)
the `os_version` is defaulting to the `image_tag` inside containers and causing our template to use latest tag --------- Co-authored-by: Gerald Pinder <gmpinder@gmail.com>
This commit is contained in:
parent
c084592934
commit
580c3d6ce7
22 changed files with 567 additions and 383 deletions
|
|
@ -14,7 +14,6 @@ chrono = "0.4"
|
|||
indexmap = { version = "2", features = ["serde"] }
|
||||
|
||||
anyhow.workspace = true
|
||||
format_serde_error.workspace = true
|
||||
log.workspace = true
|
||||
serde.workspace = true
|
||||
serde_yaml.workspace = true
|
||||
|
|
@ -23,4 +22,3 @@ typed-builder.workspace = true
|
|||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct ImageInspection {
|
||||
#[serde(alias = "Labels")]
|
||||
labels: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
impl ImageInspection {
|
||||
pub fn get_version(&self) -> Option<String> {
|
||||
Some(
|
||||
self.labels
|
||||
.get("org.opencontainers.image.version")?
|
||||
.as_str()
|
||||
.map(std::string::ToString::to_string)?
|
||||
.split('.')
|
||||
.take(1)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
pub mod akmods_info;
|
||||
pub mod image_inspection;
|
||||
pub mod module;
|
||||
pub mod module_ext;
|
||||
pub mod recipe;
|
||||
|
||||
pub use akmods_info::*;
|
||||
pub use image_inspection::*;
|
||||
pub use module::*;
|
||||
pub use module_ext::*;
|
||||
pub use recipe::*;
|
||||
|
|
|
|||
|
|
@ -1,16 +1,15 @@
|
|||
use std::{borrow::Cow, env, fs, path::Path, process::Command};
|
||||
use std::{borrow::Cow, env, fs, path::Path};
|
||||
|
||||
use anyhow::Result;
|
||||
use blue_build_utils::constants::*;
|
||||
use chrono::Local;
|
||||
use format_serde_error::SerdeError;
|
||||
use indexmap::IndexMap;
|
||||
use log::{debug, info, trace, warn};
|
||||
use log::{debug, trace, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::Value;
|
||||
use typed_builder::TypedBuilder;
|
||||
|
||||
use crate::{ImageInspection, Module, ModuleExt};
|
||||
use crate::{Module, ModuleExt};
|
||||
|
||||
#[derive(Default, Serialize, Clone, Deserialize, Debug, TypedBuilder)]
|
||||
pub struct Recipe<'a> {
|
||||
|
|
@ -42,12 +41,11 @@ pub struct Recipe<'a> {
|
|||
|
||||
impl<'a> Recipe<'a> {
|
||||
#[must_use]
|
||||
pub fn generate_tags(&self) -> Vec<String> {
|
||||
pub fn generate_tags(&self, os_version: &str) -> Vec<String> {
|
||||
trace!("Recipe::generate_tags()");
|
||||
trace!("Generating image tags for {}", &self.name);
|
||||
|
||||
let mut tags: Vec<String> = Vec::new();
|
||||
let image_version = self.get_os_version();
|
||||
let timestamp = Local::now().format("%Y%m%d").to_string();
|
||||
|
||||
if let (Ok(commit_branch), Ok(default_branch), Ok(commit_sha), Ok(pipeline_source)) = (
|
||||
|
|
@ -63,22 +61,22 @@ impl<'a> Recipe<'a> {
|
|||
trace!("CI_MERGE_REQUEST_IID={mr_iid}");
|
||||
if pipeline_source == "merge_request_event" {
|
||||
debug!("Running in a MR");
|
||||
tags.push(format!("mr-{mr_iid}-{image_version}"));
|
||||
tags.push(format!("mr-{mr_iid}-{os_version}"));
|
||||
}
|
||||
}
|
||||
|
||||
if default_branch == commit_branch {
|
||||
debug!("Running on the default branch");
|
||||
tags.push(image_version.to_string());
|
||||
tags.push(format!("{timestamp}-{image_version}"));
|
||||
tags.push(os_version.to_string());
|
||||
tags.push(format!("{timestamp}-{os_version}"));
|
||||
tags.push("latest".into());
|
||||
tags.push(timestamp);
|
||||
} else {
|
||||
debug!("Running on branch {commit_branch}");
|
||||
tags.push(format!("br-{commit_branch}-{image_version}"));
|
||||
tags.push(format!("br-{commit_branch}-{os_version}"));
|
||||
}
|
||||
|
||||
tags.push(format!("{commit_sha}-{image_version}"));
|
||||
tags.push(format!("{commit_sha}-{os_version}"));
|
||||
} else if let (
|
||||
Ok(github_event_name),
|
||||
Ok(github_event_number),
|
||||
|
|
@ -98,19 +96,19 @@ impl<'a> Recipe<'a> {
|
|||
|
||||
if github_event_name == "pull_request" {
|
||||
debug!("Running in a PR");
|
||||
tags.push(format!("pr-{github_event_number}-{image_version}"));
|
||||
tags.push(format!("pr-{github_event_number}-{os_version}"));
|
||||
} else if github_ref_name == "live" || github_ref_name == "main" {
|
||||
tags.push(image_version.to_string());
|
||||
tags.push(format!("{timestamp}-{image_version}"));
|
||||
tags.push(os_version.to_string());
|
||||
tags.push(format!("{timestamp}-{os_version}"));
|
||||
tags.push("latest".into());
|
||||
tags.push(timestamp);
|
||||
} else {
|
||||
tags.push(format!("br-{github_ref_name}-{image_version}"));
|
||||
tags.push(format!("br-{github_ref_name}-{os_version}"));
|
||||
}
|
||||
tags.push(format!("{short_sha}-{image_version}"));
|
||||
tags.push(format!("{short_sha}-{os_version}"));
|
||||
} else {
|
||||
warn!("Running locally");
|
||||
tags.push(format!("local-{image_version}"));
|
||||
tags.push(format!("local-{os_version}"));
|
||||
}
|
||||
debug!("Finished generating tags!");
|
||||
debug!("Tags: {tags:#?}");
|
||||
|
|
@ -143,55 +141,4 @@ impl<'a> Recipe<'a> {
|
|||
|
||||
Ok(recipe)
|
||||
}
|
||||
|
||||
pub fn get_os_version(&self) -> String {
|
||||
trace!("Recipe::get_os_version()");
|
||||
|
||||
if blue_build_utils::check_command_exists("skopeo").is_err() {
|
||||
warn!("The 'skopeo' command doesn't exist, falling back to version defined in recipe");
|
||||
return self.image_version.to_string();
|
||||
}
|
||||
|
||||
let base_image = self.base_image.as_ref();
|
||||
let image_version = self.image_version.as_ref();
|
||||
|
||||
info!("Retrieving information from {base_image}:{image_version}, this will take a bit");
|
||||
|
||||
let output = match Command::new("skopeo")
|
||||
.arg("inspect")
|
||||
.arg(format!("docker://{base_image}:{image_version}"))
|
||||
.output()
|
||||
{
|
||||
Err(_) => {
|
||||
warn!(
|
||||
"Issue running the 'skopeo' command, falling back to version defined in recipe"
|
||||
);
|
||||
return self.image_version.to_string();
|
||||
}
|
||||
Ok(output) => output,
|
||||
};
|
||||
|
||||
if !output.status.success() {
|
||||
warn!("Failed to get image information for {base_image}:{image_version}, falling back to version defined in recipe");
|
||||
return self.image_version.to_string();
|
||||
}
|
||||
|
||||
let inspection: ImageInspection = match serde_json::from_str(
|
||||
String::from_utf8_lossy(&output.stdout).as_ref(),
|
||||
) {
|
||||
Err(err) => {
|
||||
let err_msg =
|
||||
SerdeError::new(String::from_utf8_lossy(&output.stdout).to_string(), err)
|
||||
.to_string();
|
||||
warn!("Issue deserializing 'skopeo' output, falling back to version defined in recipe. {err_msg}",);
|
||||
return self.image_version.to_string();
|
||||
}
|
||||
Ok(inspection) => inspection,
|
||||
};
|
||||
|
||||
inspection.get_version().unwrap_or_else(|| {
|
||||
warn!("Version label does not exist on image, using version in recipe");
|
||||
image_version.to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue