fix: Allow user supplied registry to be set in the template (#135)

This commit is contained in:
Gerald Pinder 2024-03-25 20:27:34 -04:00 committed by GitHub
parent 2c98a7abc6
commit ab11362a0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 61 additions and 27 deletions

View file

@ -121,6 +121,8 @@ jobs:
BB_BUILDKIT_CACHE_GHA: true
run: |
cd integration-tests/test-repo
bluebuild template -vv | tee Containerfile
grep -q 'ARG IMAGE_REGISTRY=ghcr.io/blue-build' Containerfile || exit 1
if [ -n "$GH_TOKEN" ] && [ -n "$COSIGN_PRIVATE_KEY" ]; then
bluebuild build --push -vv
else

View file

@ -135,4 +135,6 @@ jobs:
BB_BUILDKIT_CACHE_GHA: true
run: |
cd integration-tests/test-repo
bluebuild template -vv | tee Containerfile
grep -q 'ARG IMAGE_REGISTRY=ghcr.io/blue-build' Containerfile || exit 1
bluebuild build --push -vv

View file

@ -1,9 +1,11 @@
use std::path::PathBuf;
use std::{env, path::PathBuf};
use anyhow::Result;
use blue_build_recipe::Recipe;
use blue_build_template::{ContainerFileTemplate, Template};
use blue_build_utils::constants::RECIPE_PATH;
use blue_build_utils::constants::{
CI_PROJECT_NAME, CI_PROJECT_NAMESPACE, CI_REGISTRY, GITHUB_REPOSITORY_OWNER, RECIPE_PATH,
};
use clap::Args;
use log::{debug, info, trace};
use typed_builder::TypedBuilder;
@ -23,6 +25,22 @@ pub struct TemplateCommand {
#[arg(short, long)]
#[builder(default, setter(into, strip_option))]
output: Option<PathBuf>,
/// The registry domain the image will be published to.
///
/// This is used for modules that need to know where
/// the image is being published (i.e. the signing module).
#[arg(long)]
#[builder(default, setter(into, strip_option))]
registry: Option<String>,
/// The registry namespace the image will be published to.
///
/// This is used for modules that need to know where
/// the image is being published (i.e. the signing module).
#[arg(long)]
#[builder(default, setter(into, strip_option))]
registry_namespace: Option<String>,
}
impl BlueBuildCommand for TemplateCommand {
@ -57,6 +75,7 @@ impl TemplateCommand {
.build_id(Driver::get_build_id())
.recipe(&recipe_de)
.recipe_path(recipe_path.as_path())
.registry(self.get_registry())
.build();
let output_str = template.render()?;
@ -73,6 +92,37 @@ impl TemplateCommand {
info!("Finished templating Containerfile");
Ok(())
}
fn get_registry(&self) -> String {
match (
self.registry.as_ref(),
self.registry_namespace.as_ref(),
Self::get_github_repo_owner(),
Self::get_gitlab_registry_path(),
) {
(Some(r), Some(rn), _, _) => format!("{r}/{rn}"),
(Some(r), None, _, _) => r.to_string(),
(None, None, Some(gh_repo_owner), None) => format!("ghcr.io/{gh_repo_owner}"),
(None, None, None, Some(gl_reg_path)) => gl_reg_path,
_ => "localhost".to_string(),
}
}
fn get_github_repo_owner() -> Option<String> {
Some(env::var(GITHUB_REPOSITORY_OWNER).ok()?.to_lowercase())
}
fn get_gitlab_registry_path() -> Option<String> {
Some(
format!(
"{}/{}/{}",
env::var(CI_REGISTRY).ok()?,
env::var(CI_PROJECT_NAMESPACE).ok()?,
env::var(CI_PROJECT_NAME).ok()?,
)
.to_lowercase(),
)
}
}
// ======================================================== //

View file

@ -2,8 +2,8 @@ use std::{borrow::Cow, env, fs, path::Path, process};
use blue_build_recipe::Recipe;
use blue_build_utils::constants::{
CI_PROJECT_NAME, CI_PROJECT_NAMESPACE, CI_REGISTRY, CI_SERVER_HOST, CI_SERVER_PROTOCOL,
COSIGN_PATH, GITHUB_REPOSITORY_OWNER, GITHUB_RESPOSITORY, GITHUB_SERVER_URL,
CI_PROJECT_NAME, CI_PROJECT_NAMESPACE, CI_SERVER_HOST, CI_SERVER_PROTOCOL, COSIGN_PATH,
GITHUB_RESPOSITORY, GITHUB_SERVER_URL,
};
use log::{debug, error, trace};
use typed_builder::TypedBuilder;
@ -24,6 +24,9 @@ pub struct ContainerFileTemplate<'a> {
#[builder(setter(into))]
os_version: Cow<'a, str>,
#[builder(setter(into))]
registry: Cow<'a, str>,
}
#[derive(Debug, Clone, Template, TypedBuilder)]
@ -102,22 +105,6 @@ fn print_containerfile(containerfile: &str) -> String {
file
}
fn get_github_repo_owner() -> Option<String> {
Some(env::var(GITHUB_REPOSITORY_OWNER).ok()?.to_lowercase())
}
fn get_gitlab_registry_path() -> Option<String> {
Some(
format!(
"{}/{}/{}",
env::var(CI_REGISTRY).ok()?,
env::var(CI_PROJECT_NAMESPACE).ok()?,
env::var(CI_PROJECT_NAME).ok()?,
)
.to_lowercase(),
)
}
fn get_repo_url() -> Option<String> {
Some(
match (

View file

@ -11,14 +11,7 @@ LABEL org.opencontainers.image.source="{{ repo }}"
LABEL io.artifacthub.package.readme-url=https://raw.githubusercontent.com/blue-build/cli/main/README.md
ARG RECIPE={{ recipe_path.display() }}
{%- if let Some(repo_owner) = self::get_github_repo_owner() %}
ARG IMAGE_REGISTRY=ghcr.io/{{ repo_owner }}
{%- else if let Some(registry) = self::get_gitlab_registry_path() %}
ARG IMAGE_REGISTRY={{ registry }}
{%- else %}
ARG IMAGE_REGISTRY=localhost
{%- endif %}
ARG CONFIG_DIRECTORY="/tmp/config"
ARG IMAGE_NAME="{{ recipe.name }}"