Skip to content

Commit

Permalink
fix: Allow user supplied registry to be set in the template (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder authored Mar 26, 2024
1 parent 2c98a7a commit ab11362
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 52 additions & 2 deletions src/commands/template.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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()?;
Expand All @@ -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(),
)
}
}

// ======================================================== //
Expand Down
23 changes: 5 additions & 18 deletions template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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 (
Expand Down
7 changes: 0 additions & 7 deletions template/templates/Containerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down

0 comments on commit ab11362

Please sign in to comment.