deploy-templates/JobProvisionerClusterMgmt.groovy (159 lines of code) (raw):

import groovy.json.* import jenkins.model.Jenkins Jenkins jenkins = Jenkins.instance def stages = [:] def jiraIntegrationEnabled = Boolean.parseBoolean("${JIRA_INTEGRATION_ENABLED}" as String) def commitValidateStage = jiraIntegrationEnabled ? ',{"name": "commit-validate"}' : '' def createJFVStage = jiraIntegrationEnabled ? ',{"name": "create-jira-fix-version"}' : '' def buildTool = "${BUILD_TOOL}" def goBuildStage = buildTool.toString() == "go" ? ',{"name": "build"}' : ',{"name": "compile"}' stages['Code-review-default'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}" + ']' stages['Build-application-gitops'] = '[{"name": "checkout"},{"name": "deploy-via-helmfile"}]' stages['Build-library-gitops'] = '[{"name": "checkout"},{"name": "deploy-via-helmfile"}]' stages['Build-autotests-gitops'] = '[{"name": "checkout"},{"name": "deploy-via-helmfile"}]' stages['Build-clustermgmt-gitops'] = '[{"name": "checkout"},{"name": "validating-values"},{"name": "deploy-via-helmfile"}]' stages['Build-registry-gitops'] = '[{"name": "checkout"},{"name": "deploy-via-helmfile"}]' stages['Create-release'] = '[{"name": "checkout"},{"name": "create-branch"},{"name": "trigger-job"}]' def buildToolsOutOfTheBox = ["maven","npm","gradle","dotnet","none","go","python"] def defaultBuild = '[{"name": "checkout"}]' def codebaseName = "${NAME}" def gitServerCrName = "${GIT_SERVER_CR_NAME}" def gitServerCrVersion = "${GIT_SERVER_CR_VERSION}" def gitCredentialsId = "${GIT_CREDENTIALS_ID ? GIT_CREDENTIALS_ID : 'gerrit-ciuser-sshkey'}" def repositoryPath = "${REPOSITORY_PATH}" def codebaseFolder = jenkins.getItem(codebaseName) if (codebaseFolder == null) { folder(codebaseName) } createListView(codebaseName, "Releases") createReleasePipeline("Create-release-${codebaseName}", codebaseName, stages["Create-release"], "create-release.groovy", repositoryPath, gitCredentialsId, gitServerCrName, gitServerCrVersion, jiraIntegrationEnabled) if (buildTool.toString().equalsIgnoreCase('none')) { return true } if (BRANCH) { def branch = "${BRANCH}" def formattedBranch = "${branch.toUpperCase().replaceAll(/\//, "-")}" createListView(codebaseName, formattedBranch) def type = "${TYPE}" def supBuildTool = buildToolsOutOfTheBox.contains(buildTool.toString()) def crKey = supBuildTool ? "Code-review-${type}" : "Code-review-default" createCiPipeline("Code-review-${codebaseName}", codebaseName, stages[crKey], "code-review.groovy", repositoryPath, gitCredentialsId, branch, gitServerCrName, gitServerCrVersion) def buildKey = "Build-${type}-${buildTool.toLowerCase()}".toString() if ( type.equalsIgnoreCase('registry') || type.equalsIgnoreCase('clustermgmt') ) { def jobExists = false if("${formattedBranch}-Build-${codebaseName}".toString() in Jenkins.instance.getAllItems().collect{it.name}) jobExists = true createCiPipeline("Build-${codebaseName}", codebaseName, stages.get(buildKey, defaultBuild), "build.groovy", repositoryPath, gitCredentialsId, branch, gitServerCrName, gitServerCrVersion) if(!jobExists) queue("${codebaseName}/${formattedBranch}-Build-${codebaseName}") } } def createCiPipeline(pipelineName, codebaseName, codebaseStages, pipelineScript, repository, credId, watchBranch = "master", gitServerCrName, gitServerCrVersion) { pipelineJob("${codebaseName}/${watchBranch.toUpperCase().replaceAll(/\//, "-")}-${pipelineName}") { logRotator { numToKeep(10) daysToKeep(7) } triggers { gerrit { events { if (pipelineName.contains("Build")) changeMerged() else patchsetCreated() } project("plain:${codebaseName}", ["plain:${watchBranch}"]) } } definition { cpsScm { scm { git { remote { url(repository) credentials(credId) } branches("${watchBranch}") scriptPath("${pipelineScript}") } } parameters { stringParam("GIT_SERVER_CR_NAME", "${gitServerCrName}", "Name of Git Server CR to generate link to Git server") stringParam("GIT_SERVER_CR_VERSION", "${gitServerCrVersion}", "Version of GitServer CR Resource") stringParam("STAGES", "${codebaseStages}", "Consequence of stages in JSON format to be run during execution") stringParam("GERRIT_PROJECT_NAME", "${codebaseName}", "Gerrit project name(Codebase name) to be build") if (pipelineName.contains("Build")) stringParam("BRANCH", "${watchBranch}", "Branch to build artifact from") } } } } } def createReleasePipeline(pipelineName, codebaseName, codebaseStages, pipelineScript, repository, credId, gitServerCrName, gitServerCrVersion, jiraIntegrationEnabled) { pipelineJob("${codebaseName}/${pipelineName}") { logRotator { numToKeep(14) daysToKeep(30) } definition { cpsScm { scm { git { remote { url(repository) credentials(credId) } branches("master") scriptPath("${pipelineScript}") } } parameters { stringParam("STAGES", "${codebaseStages}", "") if (pipelineName.contains("Create-release") || pipelineName.contains("Delete-release")) { stringParam("JIRA_INTEGRATION_ENABLED", "${jiraIntegrationEnabled}", "Is Jira integration enabled") stringParam("GERRIT_PROJECT", "${codebaseName}", "") stringParam("RELEASE_NAME", "", "Name of the release(branch to be created)") stringParam("COMMIT_ID", "", "Commit ID that will be used to create branch from for new release. If empty, HEAD of master will be used") stringParam("GIT_SERVER_CR_NAME", "${gitServerCrName}", "Name of Git Server CR to generate link to Git server") stringParam("GIT_SERVER_CR_VERSION", "${gitServerCrVersion}", "Version of GitServer CR Resource") stringParam("REPOSITORY_PATH", "${repository}", "Full repository path") } } } } } } def createListView(codebaseName, branchName) { listView("${codebaseName}/${branchName}") { if (branchName.toLowerCase() == "releases") { jobFilters { regex { matchType(MatchType.INCLUDE_MATCHED) matchValue(RegexMatchValue.NAME) regex("^Create-release.*") } } } else { jobFilters { regex { matchType(MatchType.INCLUDE_MATCHED) matchValue(RegexMatchValue.NAME) regex("^${branchName}-(Code-review|Build).*") } } } columns { status() weather() name() lastSuccess() lastFailure() lastDuration() buildButton() } } }