-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
160 lines (133 loc) · 6.05 KB
/
Jenkinsfile
File metadata and controls
160 lines (133 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!groovy
@Library(['github.com/cloudogu/ces-build-lib@1.62.0', 'github.com/cloudogu/zalenium-build-lib@v2.1.1'])
import com.cloudogu.ces.cesbuildlib.*
// Creating necessary git objects, object cannot be named 'git' as this conflicts with the method named 'git' from the library
gitWrapper = new Git(this, "cesmarvin")
gitWrapper.committerName = 'cesmarvin'
gitWrapper.committerEmail = 'cesmarvin@cloudogu.com'
gitflow = new GitFlow(this, gitWrapper)
github = new GitHub(this, gitWrapper)
changelog = new Changelog(this)
// Configuration of repository
repositoryOwner = "cloudogu"
repositoryName = "k8s-blueprint-lib"
project = "github.com/${repositoryOwner}/${repositoryName}"
goVersion = "1.25.3"
// Configuration of branches
productionReleaseBranch = "main"
developmentBranch = "develop"
currentBranch = "${env.BRANCH_NAME}"
registry = "registry.cloudogu.com"
registry_namespace = "k8s"
makefile = new Makefile(this)
k8sTargetDir = "target/k8s"
helmCRDChartDir = "${k8sTargetDir}/helm-crd"
helmCRDChartName = "k8s-blueprint-operator-crd"
node('docker') {
timestamps {
properties([
// Keep only the last x builds to preserve space
buildDiscarder(logRotator(numToKeepStr: '10')),
// Don't run concurrent builds for a branch, because they use the same workspace directory
disableConcurrentBuilds(),
])
stage('Checkout') {
checkout scm
make 'clean'
}
withBuildDependencies {
// no build stage as we have no main function in this lib
stage('Unit Tests') {
make 'unit-test'
junit allowEmptyResults: true, testResults: 'target/unit-tests/*-tests.xml'
}
stage('Integration Test') {
// If SKIP_DOCKER_TESTS is true, tests which need Docker containers are skipped
make 'integration-test'
junit allowEmptyResults: true, testResults: 'target/integration-tests/*-tests.xml'
}
stage("Review dog analysis") {
stageStaticAnalysisReviewDog()
}
}
stage('SonarQube') {
stageStaticAnalysisSonarQube()
}
stageAutomaticRelease()
}
}
void stageStaticAnalysisReviewDog() {
def commitSha = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'sonarqube-gh', usernameVariable: 'USERNAME', passwordVariable: 'REVIEWDOG_GITHUB_API_TOKEN']]) {
withEnv(["CI_PULL_REQUEST=${env.CHANGE_ID}", "CI_COMMIT=${commitSha}", "CI_REPO_OWNER=${repositoryOwner}", "CI_REPO_NAME=${repositoryName}"]) {
make 'static-analysis'
}
}
}
void stageStaticAnalysisSonarQube() {
def scannerHome = tool name: 'sonar-scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withSonarQubeEnv {
gitWrapper.fetch()
if (currentBranch == productionReleaseBranch) {
echo "This branch has been detected as the production branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else if (currentBranch == developmentBranch) {
echo "This branch has been detected as the development branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else if (env.CHANGE_TARGET) {
echo "This branch has been detected as a pull request."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} -Dsonar.pullrequest.base=${developmentBranch}"
} else if (currentBranch.startsWith("feature/")) {
echo "This branch has been detected as a feature branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
} else {
echo "This branch has been detected as a miscellaneous branch."
sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME} "
}
}
timeout(time: 2, unit: 'MINUTES') { // Needed when there is no webhook for example
def qGate = waitForQualityGate()
if (qGate.status != 'OK') {
unstable("Pipeline unstable due to SonarQube quality gate failure")
}
}
}
void stageAutomaticRelease() {
if (!gitflow.isReleaseBranch()) {
return
}
String controllerVersion = makefile.getVersion()
String releaseVersion = "v${controllerVersion}".toString()
stage('Push Helm chart to Harbor') {
new Docker(this)
.image("golang:${goVersion}")
.mountJenkinsUser()
.inside("--volume ${WORKSPACE}:/go/src/${project} -w /go/src/${project}")
{
make 'crd-helm-package'
archiveArtifacts "${k8sTargetDir}/**/*"
// Push charts
withCredentials([usernamePassword(credentialsId: 'harborhelmchartpush', usernameVariable: 'HARBOR_USERNAME', passwordVariable: 'HARBOR_PASSWORD')]) {
sh ".bin/helm registry login ${registry} --username '${HARBOR_USERNAME}' --password '${HARBOR_PASSWORD}'"
sh ".bin/helm push ${helmCRDChartDir}/${helmCRDChartName}-${controllerVersion}.tgz oci://${registry}/${registry_namespace}/"
}
}
}
stage('Finish Release') {
gitflow.finishRelease(releaseVersion, productionReleaseBranch)
}
stage('Add Github-Release') {
releaseId = github.createReleaseWithChangelog(releaseVersion, changelog, productionReleaseBranch)
}
}
void make(String makeArgs) {
sh "make ${makeArgs}"
}
void withBuildDependencies(Closure closure) {
new Docker(this)
.image("golang:${goVersion}")
.mountJenkinsUser()
.inside("-e SKIP_SYSLOG_TESTS=true -e SKIP_DOCKER_TESTS=true --volume ${WORKSPACE}:/go/src/${project} -w /go/src/${project}") {
closure.call()
}
}