From 93da924aab96ea496cefb35df53913e1f291d90d Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Tue, 24 Oct 2023 18:27:09 -0500 Subject: [PATCH] buildContainerImage2: Handle names with slashes If the provided container image name includes a `/` character, the build will fail when copying the OCI image to a tarball: > + buildah push git.pyrocufflink.net/containerimages/build/selinux:dev-ci oci-archive:/home/jenkins/agent/workspace/ainerImages_build.selinux_dev_ci/build/selinux-amd64.tar > Error: lstat /home/jenkins/agent/workspace/ainerImages_build.selinux_dev_ci/build: no such file or directory To resolve this, we need to escape the image name when constructing the path to the tar file. --- vars/buildContainerImage2.groovy | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/vars/buildContainerImage2.groovy b/vars/buildContainerImage2.groovy index a9329d2..f120027 100644 --- a/vars/buildContainerImage2.groovy +++ b/vars/buildContainerImage2.groovy @@ -18,17 +18,10 @@ def call(args) { project = 'containerimages' } if (name == null) { - name = env.JOB_NAME. - split('/')[1]. - toLowerCase(). - replaceAll('[^a-zA-z0-9._-]', '-'). - replaceAll('^[.-]', '_') + name = escapeImageName(env.JOB_NAME.split('/')[1]) } if (tag == null) { - tag = env.BRANCH_NAME. - toLowerCase(). - replaceAll('[^a-zA-z0-9._-]', '-'). - replaceAll('^[.-]', '_') + tag = escapeImageName(env.BRANCH_NAME) } if (archlist == null) { archlist = ['amd64'] @@ -60,7 +53,7 @@ def call(args) { sh "buildah manifest create '${full_name}'" archlist.each { arch -> unstash arch - sh "buildah manifest add '${full_name}' oci-archive:\${PWD}/${name}-${arch}.tar" + sh "buildah manifest add '${full_name}' oci-archive:\${PWD}/${escapeImageName(name)}-${arch}.tar" } } } @@ -96,8 +89,8 @@ def buildStage(args) { container('buildah') { withBuildahCreds(registry) { sh "buildah build -t '${full_name}' ." - sh "buildah push '${full_name}' oci-archive:\${PWD}/${name}-${arch}.tar" - stash name: arch, includes: "${name}-*.tar" + sh "buildah push '${full_name}' oci-archive:\${PWD}/${escapeImageName(name)}-${arch}.tar" + stash name: arch, includes: "${escapeImageName(name)}-*.tar" } } } @@ -142,3 +135,10 @@ def withBuildahCreds(registry, block) { block() } } + +def escapeImageName(name) { + return name. + toLowerCase(). + replaceAll('[^a-zA-z0-9._-]', '-'). + replaceAll('^[.-]', '_') +}