-
Notifications
You must be signed in to change notification settings - Fork 32
/
Jenkinsfile
215 lines (209 loc) · 6.38 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
@Library('apm@current') _
pipeline {
agent { label 'ubuntu-20 && immutable' }
environment {
REPO = 'golang-crossbuild'
BASE_DIR = "src/github.com/elastic/${env.REPO}"
NOTIFY_TO = credentials('notify-to')
HOME = "${env.WORKSPACE}"
PIPELINE_LOG_LEVEL = 'INFO'
DOCKER_REGISTRY_SECRET = 'secret/observability-team/ci/docker-registry/prod'
REGISTRY = 'docker.elastic.co'
STAGING_IMAGE = "${env.REGISTRY}/observability-ci"
GO_VERSION = '1.17.7'
}
options {
timeout(time: 3, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30'))
timestamps()
ansiColor('xterm')
disableResume()
durabilityHint('PERFORMANCE_OPTIMIZED')
rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true])
quietPeriod(10)
}
triggers {
issueCommentTrigger("${obltGitHubComments()}")
}
stages {
stage('Checkout') {
steps {
deleteDir()
gitCheckout(basedir: BASE_DIR)
stash name: 'source', useDefaultExcludes: false
}
}
stage('Package'){
matrix {
agent { label "${PLATFORM}" }
axes {
axis {
name 'MAKEFILE'
values 'Makefile', 'Makefile.debian7', 'Makefile.debian8', 'Makefile.debian9', 'Makefile.debian10'
}
axis {
name 'GO_FOLDER'
values 'go1.16', 'go1.17'
}
axis {
name 'PLATFORM'
values 'ubuntu-20 && immutable', 'arm'
}
}
excludes {
exclude {
axis {
name 'PLATFORM'
values 'arm'
}
axis {
name 'MAKEFILE'
values 'Makefile'
}
}
exclude {
axis {
name 'PLATFORM'
values 'arm'
}
axis {
name 'MAKEFILE'
values 'Makefile.debian7'
}
}
exclude {
axis {
name 'PLATFORM'
values 'arm'
}
axis {
name 'MAKEFILE'
values 'Makefile.debian8'
}
}
exclude {
axis {
name 'PLATFORM'
values 'arm'
}
axis {
name 'MAKEFILE'
values 'Makefile.debian10'
}
}
}
stages {
stage('Build') {
steps {
stageStatusCache(id: "Build ${GO_FOLDER} ${MAKEFILE} ${PLATFORM}") {
withGithubNotify(context: "Build ${GO_FOLDER} ${MAKEFILE} ${PLATFORM}") {
deleteDir()
unstash 'source'
buildImages()
}
}
}
}
stage('Staging') {
environment {
REPOSITORY = "${env.STAGING_IMAGE}"
}
steps {
stageStatusCache(id: "Staging ${GO_FOLDER} ${MAKEFILE} ${PLATFORM}") {
withGithubNotify(context: "Staging ${GO_FOLDER} ${MAKEFILE} ${PLATFORM}") {
// It will use the already cached docker images that were created in the
// Build stage. But it's required to retag them with the staging repo.
buildImages()
publishImages()
}
}
}
}
stage('Release') {
when {
branch 'main'
}
steps {
withGithubNotify(context: "Release ${GO_FOLDER} ${MAKEFILE} ${PLATFORM}") {
publishImages()
}
}
}
}
}
}
stage('Post-Release') {
when {
branch 'main'
}
environment {
HOME = "${env.WORKSPACE}"
PATH = "${env.HOME}/bin:${env.WORKSPACE}/${env.BASE_DIR}/.ci/scripts:${env.PATH}"
}
steps {
whenTrue(isNewRelease()) {
postRelease()
}
}
}
}
post {
always {
notifyBuildResult()
}
}
}
def buildImages(){
log(level: 'INFO', text: "buildImages ${GO_FOLDER} with ${MAKEFILE} for ${PLATFORM}")
withGoEnv(){
withGCPEnv(secret: 'secret/observability-team/ci/elastic-observability-account-auth'){
dir("${env.BASE_DIR}"){
def platform = (PLATFORM?.trim().equals('arm')) ? '-arm' : ''
retryWithSleep(retries: 3, seconds: 15, backoff: true) {
sh "make -C ${GO_FOLDER} -f ${MAKEFILE} build${platform}"
}
sh(label: 'list Docker images', script: 'docker images --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}" --filter=reference="docker.elastic.co/beats-dev/golang-crossbuild"')
}
}
}
}
def publishImages(){
log(level: 'INFO', text: "publish ${GO_FOLDER} with ${MAKEFILE} for ${PLATFORM}")
dockerLogin(secret: "${env.DOCKER_REGISTRY_SECRET}", registry: "${env.REGISTRY}")
dir("${env.BASE_DIR}"){
def platform = (PLATFORM?.trim().equals('arm')) ? '-arm' : ''
retryWithSleep(retries: 3, seconds: 15, backoff: true) {
sh(label: "push docker image to ${env.REPOSITORY}", script: "make -C ${GO_FOLDER} -f ${MAKEFILE} push${platform}")
}
}
}
def isNewRelease() {
def releases = listGithubReleases()
log(level: 'INFO', text: "isNewRelease: ${releases}")
if (env.GO_VERSION?.trim()) {
def existsRelease = releases.containsKey(env.GO_VERSION)
log(level: 'INFO', text: "isNewRelease: look for the GO_VERSION if matches any tag release in the project = ${existsRelease}")
return !existsRelease
}
return false
}
def postRelease(){
deleteDir()
unstash 'source'
dockerLogin(secret: "${env.DOCKER_REGISTRY_SECRET}", registry: "${env.REGISTRY}")
dir("${env.BASE_DIR}"){
sh(label: 'Set branch', script: """#!/bin/bash
git checkout -b ${BRANCH_NAME}
""")
try {
gitCreateTag(tag: "${env.GO_VERSION}", pushArgs: '--force')
withCredentials([string(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7', variable: 'GREN_GITHUB_TOKEN')]) {
sh(label: 'Creating Release Notes', script: '.ci/scripts/release-notes.sh')
}
gh(command: "release create ${env.GO_VERSION}", flags: [ "notes-file": ['CHANGELOG.md'], title: "${env.GO_VERSION}" ])
} catch (e) {
// Probably the tag already exists
log(level: 'WARN', text: "postRelease failed with message : ${e?.message}")
}
}
}