Skip to content

Commit

Permalink
Fixup build-build-images to upload images with smart_stage in parallel
Browse files Browse the repository at this point in the history
and mark build-build-images stage as skipped if skipped

(cherry picked from commit 9ce468e)
Change-Id: I87ff4f100d01b506254f3653a838e501eb2149c0
  • Loading branch information
JonasScharpf committed Jan 21, 2025
1 parent ae1e954 commit 4df2a86
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions buildscripts/scripts/build-build-images.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/// Other artifacts: ???
/// Depends on: image aliases for upstream OS images on Nexus, ???

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

def main() {
check_job_parameters([
"SYNC_WITH_IMAGES_WITH_UPSTREAM",
Expand All @@ -28,7 +30,7 @@ def main() {
def distros = versioning.get_distros(edition: "all", use_case: "all", override: OVERRIDE_DISTROS);

def vers_tag = versioning.get_docker_tag(scm, checkout_dir);
def branch_name = versioning.safe_branch_name(scm);
def safe_branch_name = versioning.safe_branch_name(scm);
def branch_version = versioning.get_branch_version(checkout_dir);
def publish_images = PUBLISH_IMAGES=='true'; // FIXME should be case sensitive

Expand All @@ -39,7 +41,7 @@ def main() {
|distros:........................(local) │${distros}
|publish_images:.................(local) │${publish_images}
|vers_tag:.......................(local) │${vers_tag}
|branch_name:....................(local) │${branch_name}
|safe_branch_name:...............(local) │${safe_branch_name}
|branch_version:.................(local) │${branch_version}
|===================================================
""".stripMargin());
Expand Down Expand Up @@ -78,41 +80,66 @@ def main() {
""");
}

println("alias_names: ${alias_names}");
println("dockerfiles: ${dockerfiles}");
println("image_ids: ${image_ids}");

dir("${checkout_dir}/buildscripts/infrastructure/build-nodes") {
// TODO: here it would be nice to iterate through all known distros
// and use a conditional_stage(distro in distros) approach
def stages = distros.collectEntries { distro ->
[("${distro}") : {
stage("Build ${distro}") {
def DOCKER_ARGS = (
" --build-arg ${alias_names[distro]}=${image_ids[distro]}" +
" --build-arg DOCKER_REGISTRY='${docker_registry_no_http}'" +
" --build-arg NEXUS_ARCHIVES_URL='$NEXUS_ARCHIVES_URL'" +
" --build-arg DISTRO='$distro'" +
" --build-arg NEXUS_USERNAME='$USERNAME'" +
" --build-arg NEXUS_PASSWORD='$PASSWORD'" +
" --build-arg ARTIFACT_STORAGE='$ARTIFACT_STORAGE'" +
" --build-arg VERS_TAG='$vers_tag'" +
" --build-arg BRANCH_VERSION='$branch_version'" +
" -f ${dockerfiles[distro]} .");

if (params.BUILD_IMAGE_WITHOUT_CACHE) {
DOCKER_ARGS = "--no-cache " + DOCKER_ARGS;
}
docker.build("${distro}:${vers_tag}", DOCKER_ARGS);
}}
]
}
def images = parallel(stages);
def run_condition = distro in distros;
def image = false;

/// this makes sure the whole parallel thread is marked as skipped
if (! run_condition){
Utils.markStageSkippedForConditional("${distro}");
}

conditional_stage('upload images', publish_images) {
docker.withRegistry(DOCKER_REGISTRY, "nexus") {
images.each { distro, image ->
image.push();
image.push("${branch_name}-latest");
smart_stage(
name: "Build ${distro}",
condition: run_condition,
raiseOnError: true,
) {
def image_name = "${distro}:${vers_tag}";
def docker_build_args = (""
+ " --build-arg ${alias_names[distro]}=${image_ids[distro]}"

+ " --build-arg DOCKER_REGISTRY='${docker_registry_no_http}'"
+ " --build-arg NEXUS_ARCHIVES_URL='${NEXUS_ARCHIVES_URL}'"
+ " --build-arg DISTRO='${distro}'"
+ " --build-arg NEXUS_USERNAME='${USERNAME}'"
+ " --build-arg NEXUS_PASSWORD='${PASSWORD}'"
+ " --build-arg ARTIFACT_STORAGE='${ARTIFACT_STORAGE}'"

+ " --build-arg VERS_TAG='${vers_tag}'"
+ " --build-arg BRANCH_VERSION='${branch_version}'"
+ " -f ${dockerfiles[distro]} ."
);

if (params.BUILD_IMAGE_WITHOUT_CACHE) {
docker_build_args = "--no-cache " + docker_build_args;
}

println("Build: ${image_name} with: ${docker_build_args}");
image = docker.build(image_name, docker_build_args);
}
}

smart_stage(
name: "Upload ${distro}",
condition: run_condition && publish_images,
raiseOnError: true,
) {
docker.withRegistry(DOCKER_REGISTRY, "nexus") {
image.push();
if (safe_branch_name ==~ /master|\d\.\d\.\d/) {
image.push("${safe_branch_name}-latest");
}
}
}
}]
}

currentBuild.result = parallel(stages).values().every { it } ? "SUCCESS" : "FAILURE";
}
}
}
Expand Down

0 comments on commit 4df2a86

Please sign in to comment.