Skip to content

Instantly share code, notes, and snippets.

@SergK
Last active December 10, 2021 09:41
Show Gist options
  • Save SergK/9afef191bfced4fd39a6d8dfe8a79f5d to your computer and use it in GitHub Desktop.
Save SergK/9afef191bfced4fd39a6d8dfe8a79f5d to your computer and use it in GitHub Desktop.
/* Copyright 2021 EPAM Systems.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
import groovy.json.*
import jenkins.model.Jenkins
import hudson.model.*
Jenkins jenkins = Jenkins.instance
def codebaseName = "${NAME}"
def stages = [:]
def jiraIntegrationEnabled = Boolean.parseBoolean("${JIRA_INTEGRATION_ENABLED}" as String)
def commitValidateStage = jiraIntegrationEnabled ? ',{"name": "commit-validate"}' : ''
def createJIMStage = jiraIntegrationEnabled ? ',{"name": "create-jira-issue-metadata"}' : ''
def e2eStage = codebaseName == "admin-console-operator" ? ',{"name": "e2e"}' : ''
def buildTool = "${BUILD_TOOL}"
def goBuildStage = buildTool.toString() == "go" ? ',{"name": "build"}' : ',{"name": "compile"}'
stages['Code-review-application'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}" + goBuildStage +
',{"name": "tests"},[{"name": "sonar"},{"name": "dockerfile-lint"},{"name": "helm-lint"},{"name": "dockerbuild-verify"}]]'
stages['Code-review-application-docker'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}" +
',{"name": "dockerfile-lint"},[{"name": "dockerbuild-verify"}]]'
stages['Code-review-application-helm'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}"+ ']'
stages['Code-review-application-platform'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}" + ']'
stages['Code-review-application-edp-common'] = '[{"name": "gerrit-checkout"}' + "${commitValidateStage}" + ',{"name": "tests"},{"name": "sonar"}]'
stages['Build-application-go'] = '[{"name": "checkout"},{"name": "get-version"},{"name": "tests"},{"name": "sonar"},' +
'{"name": "build"},{"name": "build-image-kaniko"}' + "${e2eStage}" +
"${createJIMStage}" + ',{"name": "ecr-to-docker"},{"name": "git-tag"}]'
stages['Build-application-docker'] = '[{"name": "checkout"},{"name": "get-version"},{"name": "dockerfile-lint"},' +
'{"name": "build-image-kaniko"}' +
"${createJIMStage}" + ',{"name": "ecr-to-docker"},{"name": "git-tag"}]'
stages['Build-application-helm'] = '[{"name": "checkout"},{"name": "get-version"},{"name": "dockerfile-lint"},' +
'{"name": "build-image-kaniko"}' +
"${createJIMStage}" + ',{"name": "ecr-to-docker"},{"name": "git-tag"}]'
stages['Build-application-platform'] = '[{"name": "checkout"}' + "${createJIMStage}" + ']'
stages['Build-application-edp-common'] = '[{"name": "checkout"},{"name": "get-version"},{"name": "tests"},{"name": "sonar"}' +
"${e2eStage}" +
"${createJIMStage}" + ',{"name": "git-tag"}]'
stages['Build-application-edp-install'] = '[{"name": "checkout"},{"name": "get-version"}' +
"${createJIMStage}" + ',{"name": "mkdocs-build"},{"name": "git-tag"}]'
stages['Create-release'] = '[{"name": "checkout"},{"name": "create-branch"},{"name": "trigger-job"}]'
def defaultBuild = '[{"name": "checkout"}' + "${createJIMStage}" + ']'
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 defaultBranch = "${DEFAULT_BRANCH}"
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, defaultBranch)
if (buildTool.toString().equalsIgnoreCase('none')) {
return true
}
if (BRANCH) {
def branch = "${BRANCH}"
def formattedBranch = "${branch.toUpperCase().replaceAll(/\\//, "-")}"
createListView(codebaseName, formattedBranch)
def type = "${TYPE}"
def crKey = getStageKeyName(buildTool,codebaseName)
createCiPipeline("Code-review-${codebaseName}", codebaseName, stages[crKey], "code-review.groovy",
repositoryPath, gitCredentialsId, branch, gitServerCrName, gitServerCrVersion)
def buildKey = getCDStageKeyName(buildTool, codebaseName, type)
if (type.equalsIgnoreCase('application') || type.equalsIgnoreCase('library')) {
def jobExists = false
if("${formattedBranch}-Build-${codebaseName}".toString() in Jenkins.instance.getAllItems().collect{it.name})
jobExists = true
createCiPipeline("Build-${codebaseName}", codebaseName, stages[buildKey], "build.groovy",
repositoryPath, gitCredentialsId, branch, gitServerCrName, gitServerCrVersion)
if(!jobExists)
queue("${codebaseName}/${formattedBranch}-Build-${codebaseName}")
}
}
def createCiPipeline(pipelineName, codebaseName, codebaseStages, pipelineScript, repository, credId, watchBranch, 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")
stringParam("BRANCH", "${watchBranch}", "Branch to build artifact from")
}
}
}
}
}
def getStageKeyName(buildTool, codebaseName) {
if (buildTool.toString().equalsIgnoreCase('platform')) {
return "Code-review-application-platform"
}
if (buildTool.toString().equalsIgnoreCase('helm')) {
return "Code-review-application-helm"
}
if (buildTool.toString().equalsIgnoreCase('docker')) {
return "Code-review-application-docker"
}
if (buildTool.toString().equalsIgnoreCase('go') && codebaseName == 'edp-common') {
return "Code-review-application-edp-common"
}
def buildToolsOutOfTheBox = ["maven","npm","gradle","dotnet","none","go","python"]
def supBuildTool = buildToolsOutOfTheBox.contains(buildTool.toString())
return supBuildTool ? "Code-review-${TYPE}" : "Code-review-default"
}
def getCDStageKeyName(buildTool, codebaseName, type) {
if (buildTool.toString().equalsIgnoreCase('go') && codebaseName == 'edp-common') {
return "Build-${type}-edp-common"
}
if (buildTool.toString().equalsIgnoreCase('helm') && codebaseName == 'edp-install') {
return "Build-${type}-edp-install"
}
"Build-${type}-${buildTool.toLowerCase()}".toString()
}
def createReleasePipeline(pipelineName, codebaseName, codebaseStages, pipelineScript, repository, credId,
gitServerCrName, gitServerCrVersion, jiraIntegrationEnabled, defaultBranch) {
pipelineJob("${codebaseName}/${pipelineName}") {
logRotator {
numToKeep(14)
daysToKeep(30)
}
definition {
cpsScm {
scm {
git {
remote {
url(repository)
credentials(credId)
}
branches("${defaultBranch}")
scriptPath("${pipelineScript}")
}
}
parameters {
stringParam("STAGES", "${codebaseStages}", "")
if (pipelineName.contains("Create-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")
stringParam("DEFAULT_BRANCH", "${defaultBranch}", "Default repository branch")
}
}
}
}
}
}
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()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment