From e6f97d4258973206fe1d94b31883c7380781941a Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sun, 3 Mar 2024 06:01:24 -0500 Subject: [PATCH] fix: Add `org.opencontainers.image.source` LABEL for CI images (#113) --- template/src/lib.rs | 32 +++++++++++++++++++++++++++++ template/templates/Containerfile.j2 | 3 +++ utils/src/constants.rs | 2 ++ 3 files changed, 37 insertions(+) diff --git a/template/src/lib.rs b/template/src/lib.rs index d5e2d0e..61c43e7 100644 --- a/template/src/lib.rs +++ b/template/src/lib.rs @@ -131,6 +131,38 @@ fn get_gitlab_registry_path() -> Option { ) } +fn get_repo_url() -> Option { + Some( + match ( + // GitHub vars + env::var(GITHUB_SERVER_URL), + env::var(GITHUB_RESPOSITORY), + // GitLab vars + env::var(CI_SERVER_PROTOCOL), + env::var(CI_SERVER_HOST), + env::var(CI_PROJECT_NAMESPACE), + env::var(CI_PROJECT_NAME), + ) { + (Ok(github_server), Ok(github_repo), _, _, _, _) => { + format!("{github_server}/{github_repo}") + } + ( + _, + _, + Ok(ci_server_protocol), + Ok(ci_server_host), + Ok(ci_project_namespace), + Ok(ci_project_name), + ) => { + format!( + "{ci_server_protocol}://{ci_server_host}/{ci_project_namespace}/{ci_project_name}" + ) + } + _ => return None, + }, + ) +} + fn modules_exists() -> bool { let mod_path = Path::new("modules"); mod_path.exists() && mod_path.is_dir() diff --git a/template/templates/Containerfile.j2 b/template/templates/Containerfile.j2 index c3c96e9..d460080 100644 --- a/template/templates/Containerfile.j2 +++ b/template/templates/Containerfile.j2 @@ -26,6 +26,9 @@ FROM {{ recipe.base_image }}:{{ recipe.image_version }} LABEL {{ blue_build_utils::constants::BUILD_ID_LABEL }}="{{ build_id }}" LABEL org.opencontainers.image.title="{{ recipe.name }}" LABEL org.opencontainers.image.description="{{ recipe.description }}" +{%- if let Some(repo) = self::get_repo_url() %} +LABEL org.opencontainers.image.source="{{ repo }}" +{%- endif %} LABEL io.artifacthub.package.readme-url=https://raw.githubusercontent.com/blue-build/cli/main/README.md ARG RECIPE={{ recipe_path.display() }} diff --git a/utils/src/constants.rs b/utils/src/constants.rs index 576fb5d..f27661c 100644 --- a/utils/src/constants.rs +++ b/utils/src/constants.rs @@ -26,7 +26,9 @@ pub const GITHUB_ACTIONS: &str = "GITHUB_ACTIONS"; pub const GITHUB_ACTOR: &str = "GITHUB_ACTOR"; pub const GITHUB_EVENT_NAME: &str = "GITHUB_EVENT_NAME"; pub const GITHUB_REF_NAME: &str = "GITHUB_REF_NAME"; +pub const GITHUB_RESPOSITORY: &str = "GITHUB_REPOSITORY"; pub const GITHUB_REPOSITORY_OWNER: &str = "GITHUB_REPOSITORY_OWNER"; +pub const GITHUB_SERVER_URL: &str = "GITHUB_SERVER_URL"; pub const GITHUB_SHA: &str = "GITHUB_SHA"; pub const GITHUB_TOKEN: &str = "GH_TOKEN"; pub const GITHUB_WORKFLOW_REF: &str = "GITHUB_WORKFLOW_REF";