From fad319c83b889cce4381bd0b0b689b25794a0709 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sun, 6 Nov 2022 20:56:04 -0600 Subject: [PATCH] Initial commit --- README.md | 10 ++++++ resources/podTemplate.yaml | 9 +++++ vars/buildContainerImage.groovy | 60 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 README.md create mode 100644 resources/podTemplate.yaml create mode 100644 vars/buildContainerImage.groovy diff --git a/README.md b/README.md new file mode 100644 index 0000000..86dab53 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Container Images Shared Library + +This shared library allows jobs to build and publish container images to Gitea +very easily: + +```groovy +buildContainerImage() +``` + +That's it! diff --git a/resources/podTemplate.yaml b/resources/podTemplate.yaml new file mode 100644 index 0000000..c7013f4 --- /dev/null +++ b/resources/podTemplate.yaml @@ -0,0 +1,9 @@ +spec: + containers: + - name: buildah + image: quay.io/containers/buildah:v1 + command: + - sleep + - infinity + securityContext: + privileged: true diff --git a/vars/buildContainerImage.groovy b/vars/buildContainerImage.groovy new file mode 100644 index 0000000..9e477d8 --- /dev/null +++ b/vars/buildContainerImage.groovy @@ -0,0 +1,60 @@ +// vim: set sw=4 sts=4 ts=4 et : + +def call() { + properties([ + pipelineTriggers([cron('H H H * *')]) + ]) + + def registry ='git.pyrocufflink.net' + def project = 'containerimages' + def name = env.JOB_NAME. + split('/')[1]. + toLowerCase(). + replaceAll('[^a-zA-z0-9._-]', '-'). + replaceAll('^[.-]', '_') + def tag = env.BRANCH_NAME. + toLowerCase(). + replaceAll('[^a-zA-z0-9._-]', '-'). + replaceAll('^[.-]', '_') + def repo = "${registry}/${project}/${name}" + def full_name = "${repo}:${tag}" + + def podTemplateYaml = libraryResource('podTemplate.yaml') + + podTemplate(yaml: podTemplateYaml) { + node(POD_LABEL) { + checkout scm + + container('buildah') { + withEnv([ + "REGISTRY_AUTH_FILE=${env.WORKSPACE_TMP}/auth.json" + ]) { + withCredentials([usernamePassword( + credentialsId: 'jenkins-packages', + usernameVariable: 'BUILDAH_USERNAME', + passwordVariable: 'BUILDAH_PASSWORD', + )]) { + sh """ + buildah login \ + --username \${BUILDAH_USERNAME} \ + --password \${BUILDAH_PASSWORD} \ + ${registry} + """ + } + + stage('Build') { + sh "buildah build -t ${full_name} ." + } + + stage('Push') { + sh "buildah push ${full_name}" + sh "buildah push ${full_name} ${full_name}-${env.BUILD_NUMBER}" + if (env.BRANCH_NAME == 'main') { + sh "buildah push ${full_name} ${repo}:latest" + } + } + } + } + } + } +}