diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index 7e87ae6..f95b5e8 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -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 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4988de2..cf0c601 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/src/commands/template.rs b/src/commands/template.rs index f52afdc..e9cbaf7 100644 --- a/src/commands/template.rs +++ b/src/commands/template.rs @@ -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, + + /// 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, + + /// 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, } 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 { + Some(env::var(GITHUB_REPOSITORY_OWNER).ok()?.to_lowercase()) + } + + fn get_gitlab_registry_path() -> Option { + Some( + format!( + "{}/{}/{}", + env::var(CI_REGISTRY).ok()?, + env::var(CI_PROJECT_NAMESPACE).ok()?, + env::var(CI_PROJECT_NAME).ok()?, + ) + .to_lowercase(), + ) + } } // ======================================================== // diff --git a/template/src/lib.rs b/template/src/lib.rs index ae163f2..1c37e22 100644 --- a/template/src/lib.rs +++ b/template/src/lib.rs @@ -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 { - Some(env::var(GITHUB_REPOSITORY_OWNER).ok()?.to_lowercase()) -} - -fn get_gitlab_registry_path() -> Option { - 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 { Some( match ( diff --git a/template/templates/Containerfile.j2 b/template/templates/Containerfile.j2 index b7c90dc..4331f8b 100644 --- a/template/templates/Containerfile.j2 +++ b/template/templates/Containerfile.j2 @@ -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 }}"