-
Notifications
You must be signed in to change notification settings - Fork 13
/
Jenkinsfile
147 lines (123 loc) · 4.84 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
143
144
145
146
147
#!groovy
project_name = 'gonsx'
project_owner = 'sky-uk'
project_src_path = "github.com/${project_owner}/${project_name}"
github_token_id = 'svc-paas-github-access-token-as-text'
github_credentials_id = 'svc-paas-github-deploy-key'
version_file = 'VERSION'
major_version = null
minor_version = null
patch_version = null
docker_image = "paas/golang-img:0.10.7a"
// helpers
gitHelper = null
shellHelper = null
goHelper = null
slackHelper = null
slackChannel = '#ott-paas'
loadHelpers()
slackHelper.notificationWrapper(slackChannel, currentBuild, env, true) {
node {
wrap([$class: 'TimestamperBuildWrapper']) {
wrap([$class: 'AnsiColorBuildWrapper']) {
stage 'checkout'
deleteDir()
git_branch = env.BRANCH_NAME
checkout scm
gitHelper.prepareGit('svc-paas-github', '[email protected]')
stage 'version'
if (autoincVersion()) {
writeFile file: version_file, text: version()
gitHelper.commit(version_file, "bumping to: ${version()}")
}
echo "Starting pipeline for project: [${project_name}], branch: [${git_branch}], version: [${version()}]"
stage 'lint'
inContainer {
goHelper.goLint(project_src_path)
}
stage 'format'
inContainer {
goHelper.goFmt(project_src_path)
}
stage 'vet'
inContainer {
goHelper.goVet(project_src_path)
}
stage 'build'
inContainer {
goHelper.goBuild(project_src_path)
}
stage 'test'
inContainer {
goHelper.goTest(project_src_path)
}
stage 'coverage'
inContainer {
goHelper.goCoverage(project_src_path)
}
}
}
}
// we only release from master
if (git_branch == 'master' && !gitHelper.isLastCommitFromUser('svc-paas-github')) {
stage 'release'
def approved = true
timeout(time: 2, unit: 'HOURS') {
try {
input message: "Release ${project_name} ${version()} ?"
} catch (InterruptedException _x) {
echo "Releasing not approved in time!"
approved = false
}
}
if (approved) {
echo "Release has been approved!"
node() {
gitHelper.tag(version(), "Jenkins ${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}")
gitHelper.push(github_credentials_id, git_branch)
echo "Creating GitHub Release v${version()}"
withCredentials([string(credentialsId: github_token_id, variable: 'GITHUB_TOKEN')]) {
def github_release_response = gitHelper.createGitHubRelease(env.GITHUB_TOKEN, project_owner, project_name, version(), git_branch)
echo "${github_release_response}"
// FIXME: this is not working yet
// echo "Attaching artifacts to GitHub Release v${version()}"
// gitHelper.uploadToGitHubRelease(project_github_token, project_owner, project_name, github_release_response.id, "${pwd()}/coverage.html", 'application/html')
}
}
currentBuild.description = "Released ${version()}"
}
}
}
def loadHelpers() {
fileLoader.withGit('[email protected]:sky-uk/paas-jenkins-pipelines.git', 'master', github_credentials_id, '') {
this.gitHelper = fileLoader.load('lib/helpers/git')
this.shellHelper = fileLoader.load('lib/helpers/shell')
this.goHelper = fileLoader.load('lib/helpers/go')
this.slackHelper = fileLoader.load('lib/helpers/slack')
}
}
def autoincVersion() {
current_version = readFile("${pwd()}/${this.version_file}").trim().tokenize(".")
setVersion(current_version[0], current_version[1], current_version[2])
if(gitHelper.checkIfTagExists(version())) {
this.patch_version++
if(gitHelper.checkIfTagExists(version())) {
error "Next patch version (${version()}) already exists!"
}
return true
}
return false
}
def version() {
return "${this.major_version}.${this.minor_version}.${this.patch_version}"
}
def setVersion(major, minor, patch) {
this.major_version = major.toInteger()
this.minor_version = minor.toInteger()
this.patch_version = patch.toInteger()
}
def inContainer(Closure body) {
docker.image(this.docker_image).inside("-v ${pwd()}:/gows/src/${project_src_path} -v ${System.getProperty('java.io.tmpdir')}:${System.getProperty('java.io.tmpdir')}") {
body()
}
}