From 022a054ccdb62970a50acc614e7d6f332f6413b8 Mon Sep 17 00:00:00 2001 From: radtriste Date: Fri, 4 Aug 2023 10:35:51 +0200 Subject: [PATCH] KOGITO-9681 CI: Use cloud shared libraries --- .ci/jenkins/Jenkinsfile.build-image | 32 +++--- .ci/jenkins/Jenkinsfile.deploy | 10 +- .ci/jenkins/Jenkinsfile.promote | 160 ++++++---------------------- .ci/jenkins/dsl/jobs.groovy | 4 - 4 files changed, 49 insertions(+), 157 deletions(-) diff --git a/.ci/jenkins/Jenkinsfile.build-image b/.ci/jenkins/Jenkinsfile.build-image index 2d5d323fc..7cd3688bc 100644 --- a/.ci/jenkins/Jenkinsfile.build-image +++ b/.ci/jenkins/Jenkinsfile.build-image @@ -21,10 +21,6 @@ pipeline { CONTAINER_ENGINE = 'docker' CONTAINER_ENGINE_TLS_OPTIONS = '' - OPENSHIFT_API = credentials('OPENSHIFT_API') - OPENSHIFT_REGISTRY = credentials('OPENSHIFT_REGISTRY') - OPENSHIFT_CREDS_KEY = 'OPENSHIFT_CREDS' - IMAGE_BUILD_PLATFORMS = 'linux/amd64,linux/arm64' } stages { @@ -55,13 +51,8 @@ pipeline { } // Login to final registry if deploy is needed - if (shouldDeployImage()) { - if (isDeployImageInOpenshiftRegistry()) { - cloud.loginOpenShift(env.OPENSHIFT_API, env.OPENSHIFT_CREDS_KEY) - cloud.loginOpenshiftRegistry(env.CONTAINER_ENGINE, env.CONTAINER_ENGINE_TLS_OPTIONS ?: '') - } else if (getDeployImageRegistryCredentials()) { - cloud.loginContainerRegistry(getDeployImageRegistry(), getDeployImageRegistryCredentials(), env.CONTAINER_ENGINE, env.CONTAINER_ENGINE_TLS_OPTIONS ?: '') - } + if (shouldDeployImage() && getDeployImageRegistryCredentials()) { + getContainerEngineService().loginContainerRegistry(getDeployImageRegistry(), getDeployImageRegistryCredentials()) } } } @@ -107,7 +98,7 @@ pipeline { script { updateGithubCommitStatus('PENDING', 'Build in progress') - String localRegistry = cloud.startLocalRegistry() + String localRegistry = getContainerEngineService().startLocalRegistry() cloud.prepareForDockerMultiplatformBuild([localRegistry],[cloud.getDockerIOMirrorRegistryConfig()], false) // Generate the Dockerfile @@ -214,11 +205,16 @@ pipeline { } } +ContainerEngineService getContainerEngineService() { + return new ContainerEngineService(this) +} + void clean() { - util.cleanNode(env.CONTAINER_ENGINE) + util.cleanNode() + getContainerEngineService().clean() + getContainerEngineService().cleanLocalRegistry() cloud.cleanDockerMultiplatformBuild() - cloud.cleanLocalRegistry() cloud.cleanSkopeo() // Clean Cekit cache, in case we reuse an old node @@ -260,20 +256,16 @@ void runPythonCommand(String cmd, boolean stdout = false) { // Deploy image information //////////////////////////////////////////////////////////////////////// -boolean isDeployImageInOpenshiftRegistry() { - return params.DEPLOY_IMAGE_USE_OPENSHIFT_REGISTRY -} - String getDeployImageRegistryCredentials() { return params.DEPLOY_IMAGE_REGISTRY_CREDENTIALS } String getDeployImageRegistry() { - return isDeployImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.DEPLOY_IMAGE_REGISTRY + return params.DEPLOY_IMAGE_REGISTRY } String getDeployImageNamespace() { - return isDeployImageInOpenshiftRegistry() ? 'openshift' : params.DEPLOY_IMAGE_NAMESPACE + return params.DEPLOY_IMAGE_NAMESPACE } String getDeployImageNameSuffix() { diff --git a/.ci/jenkins/Jenkinsfile.deploy b/.ci/jenkins/Jenkinsfile.deploy index 214146399..dabd26ee0 100644 --- a/.ci/jenkins/Jenkinsfile.deploy +++ b/.ci/jenkins/Jenkinsfile.deploy @@ -27,8 +27,6 @@ pipeline { KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") PR_BRANCH_HASH = "${util.generateHash(10)}" - - OPENSHIFT_REGISTRY = credentials('OPENSHIFT_REGISTRY') } stages { @@ -282,7 +280,6 @@ void createBuildAndTestStageClosure(String image) { buildParams.add(string(name: 'TESTS_KOGITO_EXAMPLES_REF', value: params.EXAMPLES_REF)) buildParams.add(booleanParam(name: 'DEPLOY_IMAGE', value: true)) - buildParams.add(booleanParam(name: 'DEPLOY_IMAGE_USE_OPENSHIFT_REGISTRY', value: isDeployImageInOpenshiftRegistry())) buildParams.add(string(name: 'DEPLOY_IMAGE_REGISTRY_CREDENTIALS', value: getDeployImageRegistryCredentials())) buildParams.add(string(name: 'DEPLOY_IMAGE_REGISTRY', value: getDeployImageRegistry())) buildParams.add(string(name: 'DEPLOY_IMAGE_NAMESPACE', value: getDeployImageNamespace())) @@ -341,19 +338,16 @@ List getTestFailedImages() { // Deploy image information //////////////////////////////////////////////////////////////////////// -boolean isDeployImageInOpenshiftRegistry() { - return params.IMAGE_USE_OPENSHIFT_REGISTRY -} String getDeployImageRegistryCredentials() { return params.IMAGE_REGISTRY_CREDENTIALS } String getDeployImageRegistry() { - return isDeployImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.IMAGE_REGISTRY + return params.IMAGE_REGISTRY } String getDeployImageNamespace() { - return isDeployImageInOpenshiftRegistry() ? 'openshift' : params.IMAGE_NAMESPACE + return params.IMAGE_NAMESPACE } String getDeployImageNameSuffix() { return params.IMAGE_NAME_SUFFIX diff --git a/.ci/jenkins/Jenkinsfile.promote b/.ci/jenkins/Jenkinsfile.promote index ed8078c23..7b79cb002 100644 --- a/.ci/jenkins/Jenkinsfile.promote +++ b/.ci/jenkins/Jenkinsfile.promote @@ -24,13 +24,7 @@ pipeline { CONTAINER_ENGINE = 'podman' CONTAINER_ENGINE_TLS_OPTIONS = '--tls-verify=false' - OPENSHIFT_API = credentials('OPENSHIFT_API') - OPENSHIFT_REGISTRY = credentials('OPENSHIFT_REGISTRY') - OPENSHIFT_CREDS_KEY = 'OPENSHIFT_CREDS' - PR_BRANCH_HASH = "${util.generateHash(10)}" - - GITHUB_REPO = "${REPO_NAME}" // for github-release cli } stages { @@ -45,35 +39,28 @@ pipeline { readDeployProperties() - if (isRelease()) { - // Verify version is set and if on right release branch - assert getProjectVersion() - assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion()) - } + assert getProjectVersion() + assert getBuildBranch() == util.getReleaseBranchFromVersion(getProjectVersion()) // Login old registry - if (isOldImageInOpenshiftRegistry()) { - loginOpenshiftRegistry() - } else if (getOldImageRegistryCredentials() != '') { - loginContainerRegistry(getOldImageRegistry(), getOldImageRegistryCredentials()) + if (getOldImageRegistryCredentials() != '') { + getContainerEngineService().loginContainerRegistry(getOldImageRegistry(), getOldImageRegistryCredentials()) } // Login new registry - if (isNewImageInOpenshiftRegistry()) { - loginOpenshiftRegistry() - } else if (getNewImageRegistryCredentials() != '') { - loginContainerRegistry(getNewImageRegistry(), getNewImageRegistryCredentials()) + if (getNewImageRegistryCredentials() != '') { + getContainerEngineService().loginContainerRegistry(getNewImageRegistry(), getNewImageRegistryCredentials()) } dir(getRepoName()) { checkoutRepo() } - installGitHubReleaseCLI() cloud.installSkopeo() } } } + stage('Promote images') { steps { script { @@ -83,10 +70,21 @@ pipeline { } } } - stage('Merge PR and tag') { + + stage('Make Quay images public') { when { - expression { return isRelease() } + expression { isQuayRegistry(getNewImageRegistry()) } } + steps { + script { + dir(getRepoName()) { + makeQuayNewImagesPublic() + } + } + } + } + + stage('Merge PR and tag') { steps { script { dir(getRepoName()) { @@ -96,47 +94,19 @@ pipeline { githubscm.mergePR(prLink, getGitAuthorCredsID()) githubscm.pushObject('origin', getBuildBranch(), getGitAuthorCredsID()) } - - if (getGitTag()) { - createRelease() - } } } } } - stage('Set next version') - { - when { - expression { return isRelease() } - } + + stage('Create release') { steps { script { - String prLink = '' - String nextVersion = getNextVersion() - dir('pr') { // Use different folder from `Update PR with released Maven artifacts` to avoid conflicts - // Prepare PR - checkoutRepo() - githubscm.createBranch(getSnapshotBranch()) - - // Update version to next snapshot - versionCmd = "python3 scripts/manage-kogito-version.py --bump-to ${nextVersion} --artifacts-version ${getKogitoArtifactsNextVersion()} --confirm" - if (getBuildBranch() != 'main') { - versionCmd += " --examples-ref ${getBuildBranch()}" - } - runPythonCommand(versionCmd) - - def commitMsg = "[${getBuildBranch()}] Update snapshot version to ${nextVersion}" - def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}" - githubscm.commitChanges(commitMsg) - githubscm.pushObject('origin', getSnapshotBranch(), getGitAuthorCredsID()) - prLink = githubscm.createPR(commitMsg, prBody, getBuildBranch(), getGitAuthorCredsID()) - } - dir(getRepoName()) { - if (prLink) { - githubscm.mergePR(prLink, getGitAuthorCredsID()) - githubscm.pushObject('origin', getBuildBranch(), getGitAuthorCredsID()) - } + if (githubscm.isReleaseExist(getGitTag(), getGitAuthorCredsID())) { + githubscm.deleteReleaseAndTag(getGitTag(), getGitAuthorCredsID()) } + githubscm.createReleaseWithGeneratedReleaseNotes(getGitTag(), getBuildBranch(), githubscm.getPreviousTag(getGitTag()), getGitAuthorCredsID()) + githubscm.updateReleaseBody(getGitTag(), getGitAuthorCredsID()) } } } @@ -151,6 +121,10 @@ pipeline { } } +ContainerEngineService getContainerEngineService() { + return new ContainerEngineService(this) +} + void sendUnsuccessfulNotification() { if (params.SEND_NOTIFICATION) { mailer.sendMarkdownTestSummaryNotification('Promote', getNotificationSubject(), [env.KOGITO_CI_EMAIL_TO]) @@ -169,10 +143,6 @@ String getNotificationSubject() { return "[${getBuildBranch()}] Kogito Images" } -void installGitHubReleaseCLI() { - sh 'go install github.com/github-release/github-release@latest' -} - void checkoutRepo() { deleteDir() checkout(githubscm.resolveRepository(getRepoName(), getGitAuthor(), getBuildBranch(), false)) @@ -217,14 +187,6 @@ void makeQuayNewImagesPublic() { } } -void createRelease() { - if(githubscm.isReleaseExist(getGitTag(), getGitAuthorCredsID())) { - githubscm.deleteReleaseAndTag(getGitTag(), getGitAuthorCredsID()) - } - githubscm.createReleaseWithGeneratedReleaseNotes(getGitTag(), getBuildBranch(), githubscm.getPreviousTag(getGitTag()), getGitAuthorCredsID()) - githubscm.updateReleaseBody(getGitTag(), getGitAuthorCredsID()) -} - String getOldImageFullTag(String imageName) { return "${getOldImageRegistry()}/${getOldImageNamespace()}/${getFinalImageName(imageName, getOldImageNameSuffix())}:${getOldImageTag()}" } @@ -243,32 +205,13 @@ boolean isDeployLatestTag() { String getReducedTag() { try { - String version = getNewImageTag() - String[] versionSplit = version.split("\\.") - return "${versionSplit[0]}.${versionSplit[1]}" + return cloud.getReducedTag(getNewImageTag()) } catch (error) { echo "${getNewImageTag()} cannot be reduced to the format X.Y" } return '' } -void loginOpenshift() { - withCredentials([usernamePassword(credentialsId: env.OPENSHIFT_CREDS_KEY, usernameVariable: 'OC_USER', passwordVariable: 'OC_PWD')]) { - sh "oc login --username=${OC_USER} --password=${OC_PWD} --server=${env.OPENSHIFT_API} --insecure-skip-tls-verify" - } -} - -void loginOpenshiftRegistry() { - loginOpenshift() - // username can be anything. See https://docs.openshift.com/container-platform/4.4/registry/accessing-the-registry.html#registry-accessing-directly_accessing-the-registry - sh "set +x && ${env.CONTAINER_ENGINE} login -u anything -p \$(oc whoami -t) ${env.CONTAINER_ENGINE_TLS_OPTIONS ?: ''} ${env.OPENSHIFT_REGISTRY}" -} - -void loginContainerRegistry(String registry, String credsId) { - withCredentials([usernamePassword(credentialsId: credsId, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PWD')]) { - sh "${env.CONTAINER_ENGINE} login -u ${REGISTRY_USER} -p ${REGISTRY_PWD} ${env.CONTAINER_ENGINE_TLS_OPTIONS ?: ''} ${registry}" - } -} void readDeployProperties() { String deployUrl = params.DEPLOY_BUILD_URL if (deployUrl != '') { @@ -300,10 +243,6 @@ String getParamOrDeployProperty(String paramKey, String deployPropertyKey) { // Utils //////////////////////////////////////////////////////////////////////// -boolean isRelease() { - return env.RELEASE ? env.RELEASE.toBoolean() : false -} - String getRepoName() { return env.REPO_NAME } @@ -325,10 +264,6 @@ String getBuildBranch() { return params.BUILD_BRANCH_NAME } -String getPRSourceBranch() { - return getDeployProperty("${getRepoName()}.pr.source.ref") -} - String getGitAuthor() { return env.GIT_AUTHOR } @@ -337,23 +272,6 @@ String getGitAuthorCredsID() { return env.AUTHOR_CREDS_ID } -String getMavenArtifactRepository() { - return env.MAVEN_ARTIFACT_REPOSITORY ?: '' -} - -String getNextVersion() { - return util.getNextVersion(getProjectVersion(), 'micro', 'snapshot') -} - -// To be later changed by artifacts version parameter -String getKogitoArtifactsNextVersion() { - return util.getNextVersion(getKogitoArtifactsVersion(), 'micro', 'SNAPSHOT') -} - -String getSnapshotBranch() { - return "${getNextVersion()}-${env.PR_BRANCH_HASH}" -} - boolean isQuayRegistry(String registry) { return registry == 'quay.io' } @@ -366,20 +284,16 @@ String getOldImagePrefix() { return "${getRepoName()}.image" } -boolean isOldImageInOpenshiftRegistry() { - return params.BASE_IMAGE_USE_OPENSHIFT_REGISTRY -} - String getOldImageRegistryCredentials() { return params.BASE_IMAGE_REGISTRY_CREDENTIALS } String getOldImageRegistry() { - return isOldImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : getParamOrDeployProperty('BASE_IMAGE_REGISTRY' , "${getOldImagePrefix()}.registry") + return getParamOrDeployProperty('BASE_IMAGE_REGISTRY' , "${getOldImagePrefix()}.registry") } String getOldImageNamespace() { - return isOldImageInOpenshiftRegistry() ? 'openshift' : getParamOrDeployProperty('BASE_IMAGE_NAMESPACE' , "${getOldImagePrefix()}.namespace") + return getParamOrDeployProperty('BASE_IMAGE_NAMESPACE' , "${getOldImagePrefix()}.namespace") } String getOldImageNames() { @@ -398,20 +312,16 @@ String getOldImageTag() { // New image information //////////////////////////////////////////////////////////////////////// -boolean isNewImageInOpenshiftRegistry() { - return params.PROMOTE_IMAGE_USE_OPENSHIFT_REGISTRY -} - String getNewImageRegistryCredentials() { return params.PROMOTE_IMAGE_REGISTRY_CREDENTIALS } String getNewImageRegistry() { - return isNewImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.PROMOTE_IMAGE_REGISTRY + return params.PROMOTE_IMAGE_REGISTRY } String getNewImageNamespace() { - return isNewImageInOpenshiftRegistry() ? 'openshift' : params.PROMOTE_IMAGE_NAMESPACE + return params.PROMOTE_IMAGE_NAMESPACE } String getNewImageNameSuffix() { diff --git a/.ci/jenkins/dsl/jobs.groovy b/.ci/jenkins/dsl/jobs.groovy index 590a0368d..b6a1cc07c 100644 --- a/.ci/jenkins/dsl/jobs.groovy +++ b/.ci/jenkins/dsl/jobs.groovy @@ -156,7 +156,6 @@ void setupDeployJob(JobType jobType, String envName = '') { stringParam('EXAMPLES_REF', '', 'Git reference (branch/tag) to the kogito-examples repository to use for tests.') // Deploy information - booleanParam('IMAGE_USE_OPENSHIFT_REGISTRY', false, 'Set to true if image should be deployed in Openshift registry.In this case, IMAGE_REGISTRY_CREDENTIALS, IMAGE_REGISTRY and IMAGE_NAMESPACE parameters will be ignored') stringParam('IMAGE_REGISTRY_CREDENTIALS', "${CLOUD_IMAGE_REGISTRY_CREDENTIALS_NIGHTLY}", 'Image registry credentials to use to deploy images. Will be ignored if no IMAGE_REGISTRY is given') stringParam('IMAGE_REGISTRY', "${CLOUD_IMAGE_REGISTRY}", 'Image registry to use to deploy images') stringParam('IMAGE_NAMESPACE', "${CLOUD_IMAGE_NAMESPACE}", 'Image namespace to use to deploy images') @@ -216,7 +215,6 @@ void setupBuildImageJob(JobType jobType, String envName = '', boolean prodCI = f // Deploy information booleanParam('DEPLOY_IMAGE', false, 'Should we deploy image to given deploy registry ?') - booleanParam('DEPLOY_IMAGE_USE_OPENSHIFT_REGISTRY', false, 'Set to true if image should be deployed in Openshift registry.In this case, IMAGE_REGISTRY_CREDENTIALS, IMAGE_REGISTRY and IMAGE_NAMESPACE parameters will be ignored') stringParam('DEPLOY_IMAGE_REGISTRY_CREDENTIALS', "${CLOUD_IMAGE_REGISTRY_CREDENTIALS_NIGHTLY}", 'Image registry credentials to use to deploy images. Will be ignored if no IMAGE_REGISTRY is given') stringParam('DEPLOY_IMAGE_REGISTRY', "${CLOUD_IMAGE_REGISTRY}", 'Image registry to use to deploy images') stringParam('DEPLOY_IMAGE_NAMESPACE', "${CLOUD_IMAGE_NAMESPACE}", 'Image namespace to use to deploy images') @@ -258,7 +256,6 @@ void setupPromoteJob(JobType jobType) { stringParam('DEPLOY_BUILD_URL', '', 'URL to jenkins deploy build to retrieve the `deployment.properties` file. If base parameters are defined, they will override the `deployment.properties` information') // Base images information which can override `deployment.properties` - booleanParam('BASE_IMAGE_USE_OPENSHIFT_REGISTRY', false, 'Override `deployment.properties`. Set to true if base image should be retrieved from Openshift registry.In this case, BASE_IMAGE_REGISTRY_CREDENTIALS, BASE_IMAGE_REGISTRY and BASE_IMAGE_NAMESPACE parameters will be ignored') stringParam('BASE_IMAGE_REGISTRY_CREDENTIALS', "${CLOUD_IMAGE_REGISTRY_CREDENTIALS_NIGHTLY}", 'Override `deployment.properties`. Base Image registry credentials to use to deploy images. Will be ignored if no BASE_IMAGE_REGISTRY is given') stringParam('BASE_IMAGE_REGISTRY', "${CLOUD_IMAGE_REGISTRY}", 'Override `deployment.properties`. Base image registry') stringParam('BASE_IMAGE_NAMESPACE', "${CLOUD_IMAGE_NAMESPACE}", 'Override `deployment.properties`. Base image namespace') @@ -267,7 +264,6 @@ void setupPromoteJob(JobType jobType) { stringParam('BASE_IMAGE_TAG', '', 'Override `deployment.properties`. Base image tag') // Promote images information - booleanParam('PROMOTE_IMAGE_USE_OPENSHIFT_REGISTRY', false, 'Set to true if base image should be deployed in Openshift registry.In this case, PROMOTE_IMAGE_REGISTRY_CREDENTIALS, PROMOTE_IMAGE_REGISTRY and PROMOTE_IMAGE_NAMESPACE parameters will be ignored') stringParam('PROMOTE_IMAGE_REGISTRY_CREDENTIALS', "${CLOUD_IMAGE_REGISTRY_CREDENTIALS_NIGHTLY}", 'Promote Image registry credentials to use to deploy images. Will be ignored if no PROMOTE_IMAGE_REGISTRY is given') stringParam('PROMOTE_IMAGE_REGISTRY', "${CLOUD_IMAGE_REGISTRY}", 'Promote image registry') stringParam('PROMOTE_IMAGE_NAMESPACE', "${CLOUD_IMAGE_NAMESPACE}", 'Promote image namespace')