|
| 1 | +#!groovy |
| 2 | + |
| 3 | +String getApplication() { 'spring-petclinic-plain' } |
| 4 | +String getConfigRepositoryPRRepo() { 'example-tenant/gitops' } |
| 5 | +String getScmManagerCredentials() { 'scmm-user' } |
| 6 | +String getConfigRepositoryPRBaseUrl() { env.SCMM_URL } |
| 7 | + |
| 8 | +String getDockerRegistryBaseUrl() { env.REGISTRY_URL } |
| 9 | +String getDockerRegistryPath() { env.REGISTRY_PATH } |
| 10 | +String getDockerRegistryCredentials() { 'registry-user' } |
| 11 | +String getCesBuildLibRepo() { configRepositoryPRBaseUrl+"/repo/3rd-party-dependencies/ces-build-lib/" } |
| 12 | +String getCesBuildLibVersion() { 'main' } |
| 13 | +String getGitOpsBuildLibRepo() { configRepositoryPRBaseUrl+"/repo/3rd-party-dependencies/gitops-build-lib" } |
| 14 | +String getGitOpsBuildLibVersion() { 'main'} |
| 15 | + |
| 16 | +loadLibraries() |
| 17 | + |
| 18 | +properties([ |
| 19 | + // Don't run concurrent builds, because the ITs use the same port causing random failures on concurrent builds. |
| 20 | + disableConcurrentBuilds() |
| 21 | +]) |
| 22 | + |
| 23 | +node { |
| 24 | + |
| 25 | + mvn = cesBuildLib.MavenWrapper.new(this) |
| 26 | + |
| 27 | + catchError { |
| 28 | + |
| 29 | + stage('Checkout') { |
| 30 | + checkout scm |
| 31 | + } |
| 32 | + |
| 33 | + stage('Build') { |
| 34 | + mvn 'clean package -DskipTests -Dcheckstyle.skip' |
| 35 | + archiveArtifacts artifacts: '**/target/*.jar' |
| 36 | + } |
| 37 | + |
| 38 | + stage('Test') { |
| 39 | + // Tests skipped for faster demo and exercise purposes |
| 40 | + //mvn 'test -Dmaven.test.failure.ignore=true -Dcheckstyle.skip' |
| 41 | + } |
| 42 | + |
| 43 | + String imageName = "" |
| 44 | + stage('Docker') { |
| 45 | + String imageTag = createImageTag() |
| 46 | + String pathPrefix = !dockerRegistryPath?.trim() ? "" : "${dockerRegistryPath}/" |
| 47 | + imageName = "${dockerRegistryBaseUrl}/${pathPrefix}${application}:${imageTag}" |
| 48 | + image = docker.build(imageName, '.') |
| 49 | + |
| 50 | + if (isBuildSuccessful()) { |
| 51 | + docker.withRegistry("https://${dockerRegistryBaseUrl}", dockerRegistryCredentials) { |
| 52 | + image.push() |
| 53 | + } |
| 54 | + } else { |
| 55 | + echo 'Skipping docker push, because build not successful' |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + stage('Deploy') { |
| 60 | + if (isBuildSuccessful() && env.BRANCH_NAME in ['main']) { |
| 61 | + |
| 62 | + def gitopsConfig = [ |
| 63 | + scm: [ |
| 64 | + provider : 'SCMManager', |
| 65 | + credentialsId: scmManagerCredentials, |
| 66 | + baseUrl : configRepositoryPRBaseUrl, |
| 67 | + repositoryUrl : configRepositoryPRRepo, |
| 68 | + ], |
| 69 | + application: application, |
| 70 | + gitopsTool: 'ARGO', |
| 71 | + folderStructureStrategy: 'ENV_PER_APP', |
| 72 | + k8sVersion : env.K8S_VERSION, |
| 73 | + deployments: [ |
| 74 | + sourcePath: 'k8s', |
| 75 | + destinationRootPath: 'apps', |
| 76 | + plain: [ |
| 77 | + updateImages: [ |
| 78 | + [ filename: 'deployment.yaml', |
| 79 | + containerName: application, |
| 80 | + imageName: imageName ] |
| 81 | + ] |
| 82 | + ] |
| 83 | + ], |
| 84 | + fileConfigmaps: [ |
| 85 | + // Showcase for gitops-build-lib: Convert file into a config map |
| 86 | + [ |
| 87 | + name : 'messages', |
| 88 | + sourceFilePath : '../src/main/resources/messages/messages.properties', |
| 89 | + stage: ['staging', 'production'] |
| 90 | + ] |
| 91 | + ], |
| 92 | + stages: [ |
| 93 | + staging: [ |
| 94 | + namespace: 'example-apps-staging', |
| 95 | + deployDirectly: true ], |
| 96 | + production: [ |
| 97 | + namespace: 'example-apps-production', |
| 98 | + deployDirectly: false ], |
| 99 | + ] |
| 100 | + ] |
| 101 | + addSpecificGitOpsConfig(gitopsConfig) |
| 102 | + |
| 103 | + deployViaGitops(gitopsConfig) |
| 104 | + } else { |
| 105 | + echo 'Skipping deploy, because build not successful or not on main branch' |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Archive Unit and integration test results, if any |
| 111 | + junit allowEmptyResults: true, testResults: '**/target/failsafe-reports/TEST-*.xml,**/target/surefire-reports/TEST-*.xml' |
| 112 | +} |
| 113 | + |
| 114 | +/** Initializations might not be needed in a real-world setup, but are necessary for GitOps playground */ |
| 115 | +void addSpecificGitOpsConfig(gitopsConfig) { |
| 116 | + gitopsConfig += [ |
| 117 | + // In the GitOps playground, we're loading the build libs from our local SCM so it also works in an offline context |
| 118 | + // As the gitops-build-lib also uses the ces-build-lib we need to pass those parameters on. |
| 119 | + // If you can access the internet, you can rely on the defaults, which load the lib from GitHub. |
| 120 | + cesBuildLibRepo: cesBuildLibRepo, |
| 121 | + cesBuildLibVersion: cesBuildLibVersion, |
| 122 | + cesBuildLibCredentialsId: scmManagerCredentials, |
| 123 | + |
| 124 | + |
| 125 | + // The GitOps playground provides parameters for overwriting the build images used by gitops-build-lib, so |
| 126 | + // it also works in an offline context. |
| 127 | + // Those parameters overwrite the following parameters. |
| 128 | + // If you can access the internet, you can rely on the defaults, which load the images from public registries. |
| 129 | + buildImages : [ |
| 130 | + helm: 'ghcr.io/cloudogu/helm:3.16.4-1', |
| 131 | + kubectl: 'bitnami/kubectl:1.29', |
| 132 | + kubeval: 'ghcr.io/cloudogu/helm:3.16.4-1', |
| 133 | + helmKubeval: 'ghcr.io/cloudogu/helm:3.16.4-1', |
| 134 | + yamllint: 'cytopia/yamllint:1.25-0.7' |
| 135 | + ] |
| 136 | + ] |
| 137 | +} |
| 138 | + |
| 139 | +String createImageTag() { |
| 140 | + def git = cesBuildLib.Git.new(this) |
| 141 | + String branch = git.simpleBranchName |
| 142 | + String branchSuffix = "" |
| 143 | + |
| 144 | + if (!"develop".equals(branch)) { |
| 145 | + branchSuffix = "-${branch}" |
| 146 | + } |
| 147 | + |
| 148 | + return "${new Date().format('yyyyMMddHHmm')}-${git.commitHashShort}${branchSuffix}" |
| 149 | +} |
| 150 | + |
| 151 | +def loadLibraries() { |
| 152 | + // In the GitOps playground, we're loading the build libs from our local SCM so it also works in an offline context |
| 153 | + // If you can access the internet, you could also load the libraries directly from github like so |
| 154 | + // @Library(["github.com/cloudogu/ces-build-lib@${cesBuildLibVersion}", "github.com/cloudogu/gitops-build-lib@${gitOpsBuildLibRepo}"]) _ |
| 155 | + //import com.cloudogu.ces.cesbuildlib.* |
| 156 | + //import com.cloudogu.ces.gitopsbuildlib.* |
| 157 | + |
| 158 | + cesBuildLib = library(identifier: "ces-build-lib@${cesBuildLibVersion}", |
| 159 | + retriever: modernSCM([$class: 'GitSCMSource', remote: cesBuildLibRepo, credentialsId: scmManagerCredentials]) |
| 160 | + ).com.cloudogu.ces.cesbuildlib |
| 161 | + |
| 162 | + library(identifier: "gitops-build-lib@${gitOpsBuildLibVersion}", |
| 163 | + retriever: modernSCM([$class: 'GitSCMSource', remote: gitOpsBuildLibRepo, credentialsId: scmManagerCredentials]) |
| 164 | + ).com.cloudogu.gitops.gitopsbuildlib |
| 165 | +} |
| 166 | + |
| 167 | +def cesBuildLib |
| 168 | +def gitOpsBuildLib |
0 commit comments