-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathJenkinsfile
277 lines (258 loc) · 11 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env groovy
@Library('apm@current') _
pipeline {
agent { label 'ubuntu-20 && immutable' }
environment {
BASE_DIR="src/github.com/elastic/integrations"
GITHUB_TOKEN_CREDENTIALS = "2a9602aa-ab9f-4e52-baf3-b71ca88469c7"
JOB_GIT_CREDENTIALS = "f6c7695a-671e-4f4f-a331-acdce44ff9ba"
PACKAGE_STORAGE_BASE_DIR = "src/github.com/elastic/package-storage"
AWS_ACCOUNT_SECRET = "secret/observability-team/ci/elastic-observability-aws-account-auth"
HOME = "${env.WORKSPACE}"
KIND_VERSION = "v0.10.0"
K8S_VERSION = "v1.20.2"
JOB_GCS_BUCKET = 'beats-ci-temp'
JOB_GCS_CREDENTIALS = 'beats-ci-gcs-plugin'
ELASTIC_STACK_VERSION_PREV = "7.12.0-SNAPSHOT"
ELASTIC_STACK_VERSION_PREV_PREV = "7.11.2-SNAPSHOT"
}
options {
timeout(time: 2, 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('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*')
}
stages {
stage('Prepare workspace') {
steps {
deleteDir()
gitCheckout(basedir: "${BASE_DIR}")
stashV2(name: 'source', bucket: "${JOB_GCS_BUCKET}", credentialsId: "${JOB_GCS_CREDENTIALS}")
}
}
stage('Check Go sources') {
steps {
withMageEnv(){
dir("${BASE_DIR}"){
sh(label: 'Checks and builds Go sources', script: 'mage -debug check')
checkGitDiff()
}
}
}
}
stage('Check integrations') {
steps {
script {
dir("${BASE_DIR}/packages") {
def integrations = [:]
// Include hack to skip temporary files with "@tmp" suffix.
// For reference: https://issues.jenkins.io/browse/JENKINS-52750
findFiles()?.findAll{ !it.name.endsWith('@tmp') }?.collect{ it.name }?.sort()?.each {
if (isPrAffected(it)) {
integrations[it] = {
withNode(labels: 'ubuntu-20 && immutable', sleepMin: 10, sleepMax: 100) {
stage("${it}: check") {
deleteDir()
unstashV2(name: 'source', bucket: "${JOB_GCS_BUCKET}", credentialsId: "${JOB_GCS_CREDENTIALS}")
useElasticPackage()
try {
dir("${BASE_DIR}/packages/${it}") {
sh(label: "Check integration: ${it}", script: '../../build/elastic-package check -v')
sh(label: "Boot up the Elastic stack", script: '../../build/elastic-package stack up -d -v')
withKubernetes() {
withCloudTestEnv() {
sh(label: "Test integration: ${it}", script: '''
eval "$(../../build/elastic-package stack shellinit)"
../../build/elastic-package test -v --report-format xUnit --report-output file
''')
}
}
}
} finally {
dir("${BASE_DIR}") {
archiveArtifacts(allowEmptyArchive: true, artifacts: 'build/test-results/*.xml')
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "build/test-results/*.xml")
sh(label: "Collect Elastic stack logs", script: "build/elastic-package stack dump -v --output build/elastic-stack-dump/latest/${it}")
archiveArtifacts(allowEmptyArchive: true, artifacts: "build/elastic-stack-dump/latest/${it}/logs/*.log")
sh(label: "Take down the Elastic stack", script: 'build/elastic-package stack down -v')
}
}
// Check compatibility with previous stacks
checkPackageCompatibility(it, ELASTIC_STACK_VERSION_PREV)
checkPackageCompatibility(it, ELASTIC_STACK_VERSION_PREV_PREV)
}
}
}
}
}
parallel integrations
}
}
}
}
stage('Update Package Storage') {
when {
branch 'master'
}
steps {
useElasticPackage()
dir("${BASE_DIR}/packages") {
sh(label: "Build packages", script: '''
for d in *; do
(
cd $d
../../build/elastic-package build -v
)
done
''')
}
gitCheckout(basedir: "${PACKAGE_STORAGE_BASE_DIR}", branch: 'master', repo: 'git@github.com:elastic/package-storage.git', credentialsId: "${JOB_GIT_CREDENTIALS}")
dir("${PACKAGE_STORAGE_BASE_DIR}"){
sh(label: 'Configure Git user.name', script: 'git config user.name "Elastic Machine"')
sh(label: 'Configure Git user.email', script: 'git config user.email "elasticmachine@users.noreply.github.com"')
sh(label: 'Add upstream to tracked repositories', script: 'git remote add upstream https://github.com/elastic/package-storage')
sh(label: 'Fetch upstream', script: 'git fetch upstream')
sh(label: 'Track snapshot', script: 'git checkout -b snapshot -t upstream/snapshot')
sh(label: 'Track staging', script: 'git checkout -b staging -t upstream/staging')
sh(label: 'Track production', script: 'git checkout -b production -t upstream/production')
}
withMageEnv(){
dir("${BASE_DIR}"){
withCredentials([string(credentialsId: "${GITHUB_TOKEN_CREDENTIALS}", variable: 'GITHUB_TOKEN')]) {
sh(label: 'Updates Package Storage', script: 'mage -debug updatePackageStorage')
}
}
}
}
}
}
post {
cleanup {
notifyBuildResult(prComment: true)
}
}
}
def cleanup(){
dir("${BASE_DIR}"){
deleteDir()
}
}
def useElasticPackage() {
withGoEnv() {
dir("${BASE_DIR}/build") {
sh(label: 'Install elastic-package', script: 'go build github.com/elastic/elastic-package')
}
}
}
// Check if there are non-versioned local changes.
// For reference: https://stackoverflow.com/questions/34807971/why-does-git-diff-index-head-result-change-for-touched-files-after-git-diff-or-g
def checkGitDiff() {
dir("${BASE_DIR}") {
sh(label: "git update-index", script: 'git update-index --refresh')
sh(label: "git diff-index", script: 'git diff-index --exit-code HEAD --')
}
}
def isPrAffected(integrationName) {
if (env.BRANCH_NAME == "master") {
echo "[${integrationName}] PR is affected: running on master branch"
return true
}
// Source: https://github.com/elastic/apm-pipeline-library/blob/721115cf0fdb2b5a4e1cb6a576bfbb7035d14485/vars/getGitMatchingGroup.groovy#L40-L41
def from = env.CHANGE_TARGET?.trim() ? "origin/${env.CHANGE_TARGET}" : "${env.GIT_PREVIOUS_COMMIT?.trim() ? env.GIT_PREVIOUS_COMMIT : env.GIT_BASE_COMMIT}"
def to = env.GIT_BASE_COMMIT
def r = sh(label: "[${integrationName}] git-diff: check non-package files", script: "git diff --name-only \$(git merge-base ${from} ${to}) | egrep -v '^packages/'", returnStatus: true)
if (r == 0) {
echo "[${integrationName}] PR is affected: found non-package files"
return true
}
r = sh(label: "[${integrationName}] git-diff: check package files", script: "git diff --name-only \$(git merge-base ${from} ${to}) | egrep '^packages/${integrationName}/'", returnStatus: true)
if (r == 0) {
echo "[${integrationName}] PR is affected: found package files"
return true
}
echo "[${integrationName}] PR is not affected"
return false
}
def withKubernetes(Closure body) {
def r = sh(label: "git-diff: check if Kubernetes service deployer is used",
script: "find . -type d | egrep '_dev/deploy/k8s\$'",
returnStatus: true)
withEnv(["PATH=${env.WORKSPACE}/bin:${env.PATH}"]) {
if (r == 0) {
retryWithSleep(retries: 2, seconds: 5, backoff: true) { sh(label: "Install kind", script: '''
mkdir -p ${HOME}/bin
curl -sSLo ${HOME}/bin/kind "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-linux-amd64"
chmod +x ${HOME}/bin/kind
kind version
''') }
retryWithSleep(retries: 2, seconds: 5, backoff: true) { sh(label: "Install kubectl", script: '''
mkdir -p ${HOME}/bin
curl -sSLo ${HOME}/bin/kubectl "https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl"
chmod +x ${HOME}/bin/kubectl
kubectl version --client
''') }
sh(label: "Create kind cluster", script: 'kind create cluster')
}
body()
if (r == 0) {
sh(label: "Delete kind cluster", script: 'kind delete cluster')
}
}
}
def withCloudTestEnv(Closure body) {
def maskedVars = []
// AWS
def aws = getVaultSecret(secret: "${AWS_ACCOUNT_SECRET}").data
if (!aws.containsKey('access_key')) {
error("${AWS_ACCOUNT_SECRET} doesn't contain 'access_key'")
}
if (!aws.containsKey('secret_key')) {
error("${AWS_ACCOUNT_SECRET} doesn't contain 'secret_key'")
}
maskedVars.addAll([
[var: "AWS_ACCESS_KEY_ID", password: aws.access_key],
[var: "AWS_SECRET_ACCESS_KEY", password: aws.secret_key],
])
withEnvMask(vars: maskedVars) {
body()
}
}
def checkPackageCompatibility(integrationName, stackVersion) {
if (!isPackageCompatible(integrationName, stackVersion)) {
return // the package doesn't have to be supported by this stack
}
try {
dir("${BASE_DIR}/packages/${integrationName}") {
sh(label: "Boot up the Elastic stack (${stackVersion})", script: "../../build/elastic-package stack up -d -v --version ${stackVersion}")
sh(label: "Install integration: ${integrationName}", script: '''
eval "$(../../build/elastic-package stack shellinit)"
../../build/elastic-package install -v''')
}
} finally {
dir("${BASE_DIR}") {
sh(label: "Collect Elastic stack logs", script: "build/elastic-package stack dump -v --output build/elastic-stack-dump/${stackVersion}/${integrationName}")
archiveArtifacts(allowEmptyArchive: true, artifacts: "build/elastic-stack-dump/${stackVersion}/${integrationName}/logs/*.log")
sh(label: "Take down the Elastic stack (${stackVersion})", script: 'build/elastic-package stack down -v')
}
}
}
def isPackageCompatible(integrationName, stackVersion) {
dir("${BASE_DIR}/packages/${integrationName}") {
def r = sh(label: "Check compatibility with ${stackVersion}",
script: "../../build/elastic-package install -v -c kibana.version=${stackVersion}",
returnStatus: true)
if (r == 0) {
echo "Package should be compatible"
return true
}
echo "Package is not compatible. Skipping."
return false
}
}