fix(pipeline): add registryCredentialsId parameter to ImageBuilder and update related references

Signed-off-by: 孙振宇 <>
This commit is contained in:
孙振宇 2025-02-08 12:07:41 +08:00
parent cf209c4cc3
commit fbf00a1046
3 changed files with 26 additions and 13 deletions

View File

@ -17,7 +17,7 @@ class ImageBuilder {
this.builderType = builderType
}
def build(name, repository, registry, architectures, version) {
def build(name, repository, registry, architectures, version, registryCredentialsId) {
steps.log.info("ImageBuilder", "Building image with ${builderType.builder}")
steps.log.info("ImageBuilder", "Workspace sets to: ${workspace}")
steps.log.info("ImageBuilder", "Using dockerfile at: ${dockerfile}, context root sets to: ${contextRoot}")
@ -27,8 +27,26 @@ class ImageBuilder {
architectures = ['linux/amd64']
}
steps.log.info("ImageBuilder", "Login to ${registry}")
steps.sh "docker login ${registry}"
steps.withCredentials([usernamePassword(credentialsId: registryCredentialsId, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
steps.log.info("ImageBuilder", "Authentication to ${registry}")
switch(builderType) {
case ImageBuilderTypes.DOCKER_IN_DOCKER:
steps.sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${registry}"
break
case ImageBuilderTypes.KANIKO:
def auth = "${DOCKER_USERNAME}:${DOCKER_PASSWORD}".bytes.encodeBase64().toString()
steps.writeFile file: '/kaniko/.docker/config.json', text: """{
"auths": {
"${registry}": {
"auth": "${auth}"
}
}
}"""
break
default:
steps.error("Unsupported builder type: ${builderType.builder}")
}
}
switch(builderType) {
case ImageBuilderTypes.DOCKER_IN_DOCKER:

View File

@ -74,8 +74,8 @@ executeFreeleapsPipeline {
imageBuildRoot: '.',
// imageReleaseArchitectures used to specify the released image architectures
imageReleaseArchitectures: ['linux/amd64', 'linux/arm64'],
// registryCredentialName used to specify the registry credential that stored in Freeleaps Kubernetes Cluster
registryCredentialName: 'gitops-mvp-app-secret',
// registryCredentialsId used to specify the registry credential that stored in Jenkins
registryCredentialsId: 'gitops-mvp-app-secret',
// semanticReleaseEnabled used to specify whether to enable semantic release
semanticReleaseEnabled: true
],
@ -121,8 +121,8 @@ executeFreeleapsPipeline {
imageBuildRoot: '.',
// imageReleaseArchitectures used to specify the released image architectures
imageReleaseArchitectures: ['linux/amd64', 'linux/arm64'],
// registryCredentialName used to specify the registry credential that stored in Freeleaps Kubernetes Cluster
registryCredentialName: 'gitops-mvp-app-secret',
// registryCredentialsId used to specify the registry credential that stored in Jenkins
registryCredentialsId: 'gitops-mvp-app-secret',
// semanticReleaseEnabled used to specify whether to enable semantic release
semanticReleaseEnabled: true
]

View File

@ -370,11 +370,6 @@ def generateComponentStages(component, configurations) {
},
// Image Building & Publishing
stage("${component.name} :: Image Building & Publishing") {
when {
expression {
return (env.executeMode == "fully" || env.changedComponents.contains(component.name)) && env.imageBuilderImage != null && !env.imageBuilderImage.isEmpty()
}
}
podTemplate(
label: "image-builder-${component.name}",
containers: [
@ -424,7 +419,7 @@ def generateComponentStages(component, configurations) {
}
def version
imageBuilder.build(component.imageName, component.imageRepository, component.imageRegistry, component.imageReleaseArchitectures, env.LATEST_VERSION)
imageBuilder.build(component.imageName, component.imageRepository, component.imageRegistry, component.imageReleaseArchitectures, env.LATEST_VERSION, component.registryCredentialsId)
}
}
}