jenkins-shared-libs/vars/buildContainerImage.groovy

61 lines
1.9 KiB
Groovy

// 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"
}
}
}
}
}
}
}