-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
143 lines (121 loc) · 5.47 KB
/
Jenkinsfile
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
#!groovy
@Library(['github.com/cloudogu/[email protected]', 'github.com/cloudogu/[email protected]'])
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 = '[email protected]'
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.23.4"
// Configuration of branches
productionReleaseBranch = "main"
developmentBranch = "develop"
currentBranch = "${env.BRANCH_NAME}"
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 {
stage('Build') {
make 'compile'
}
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 releaseVersion = gitWrapper.getSimpleBranchName()
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) {
def etcdImage = docker.image('quay.io/coreos/etcd:v3.2.32')
def etcdContainerName = "${JOB_BASE_NAME}-${BUILD_NUMBER}".replaceAll("\\/|%2[fF]", "-")
withDockerNetwork { buildnetwork ->
etcdImage.withRun("--network ${buildnetwork} --name ${etcdContainerName}", 'etcd --listen-client-urls http://0.0.0.0:4001 --advertise-client-urls http://0.0.0.0:4001')
{
new Docker(this)
.image("golang:${goVersion}")
.mountJenkinsUser()
.inside("--network ${buildnetwork} -e ETCD=${etcdContainerName} -e SKIP_SYSLOG_TESTS=true -e SKIP_DOCKER_TESTS=true --volume ${WORKSPACE}:/go/src/${project} -w /go/src/${project}")
{
closure.call()
}
}
}
}