diff --git a/first-class-pipeline/src/com/freeleaps/devops/ImageBuilder.groovy b/first-class-pipeline/src/com/freeleaps/devops/ImageBuilder.groovy index 1c45c278..9722228c 100644 --- a/first-class-pipeline/src/com/freeleaps/devops/ImageBuilder.groovy +++ b/first-class-pipeline/src/com/freeleaps/devops/ImageBuilder.groovy @@ -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: diff --git a/first-class-pipeline/tests/Jenkinsfile b/first-class-pipeline/tests/Jenkinsfile index 7d8b0dd8..bc4e9c9c 100644 --- a/first-class-pipeline/tests/Jenkinsfile +++ b/first-class-pipeline/tests/Jenkinsfile @@ -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 ] diff --git a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy index faddfaf6..474f7e52 100644 --- a/first-class-pipeline/vars/executeFreeleapsPipeline.groovy +++ b/first-class-pipeline/vars/executeFreeleapsPipeline.groovy @@ -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) } } }