jenkins-shared-libs/vars/buildContainerImage.groovy

80 lines
2.3 KiB
Groovy

// vim: set sw=4 sts=4 ts=4 et :
def call(args) {
properties([
pipelineTriggers([cron('H H H * *')])
])
def registry = args?.registry
def project = args?.project
def name = args?.name
def tag = args?.tag
def arch = args?.arch
if (registry == null) {
registry = 'git.pyrocufflink.net'
}
if (project == null) {
project = 'containerimages'
}
if (name == null) {
name = env.JOB_NAME.
split('/')[1].
toLowerCase().
replaceAll('[^a-zA-z0-9._-]', '-').
replaceAll('^[.-]', '_')
}
if (tag == null) {
tag = env.BRANCH_NAME.
toLowerCase().
replaceAll('[^a-zA-z0-9._-]', '-').
replaceAll('^[.-]', '_')
}
if (arch == null) {
arch = 'amd64'
}
def repo = "${registry}/${project}/${name}"
def full_name = "${repo}:${tag}"
def podTemplateYaml = libraryResource('podTemplate.yaml')
podTemplate(
yaml: podTemplateYaml,
nodeSelector: "kubernetes.io/arch=${arch}",
) {
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"
}
}
}
}
}
}
}