forked from kyma-project/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
132 lines (108 loc) · 5.8 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
#!/usr/bin/env groovy
import groovy.json.JsonSlurperClassic
import groovy.json.JsonOutput
def label = "kyma-${UUID.randomUUID().toString()}"
application = 'examples'
def isMaster = params.GIT_BRANCH == 'master'
dockerPushRoot = isMaster
? "${env.DOCKER_REGISTRY}"
: "${env.DOCKER_REGISTRY}snapshot/"
dockerImageTag = isMaster
? params.APP_VERSION
: params.GIT_BRANCH
def changes = parseJson("${params.CHANGED_EXAMPLES}")
//For now, we only have deployment pods for these examples. Once we have for all, we can just eliminate this check.
def deploy = changes.contains("http-db-service") || changes.contains("event-email-service") || changes.contains("event-subscription/lambda")
echo """
********************************
Job started with the following parameters:
DOCKER_REGISTRY=${env.DOCKER_REGISTRY}
DOCKER_CREDENTIALS=${env.DOCKER_CREDENTIALS}
GIT_REVISION=${params.GIT_REVISION}
GIT_BRANCH=${params.GIT_BRANCH}
APP_VERSION=${params.APP_VERSION}
CHANGED_EXAMPLES=${JsonOutput.prettyPrint(params.CHANGED_EXAMPLES)}
********************************
"""
podTemplate(label: label) {
node(label) {
try {
timestamps {
timeout(time:20, unit:"MINUTES") {
ansiColor('xterm') {
stage("setup") {
checkout scm
if(dockerImageTag == ""){
error("No version for docker tag defined, please set APP_VERSION parameter for master branch or GIT_BRANCH parameter for any branch")
}
withCredentials([usernamePassword(credentialsId: env.DOCKER_CREDENTIALS, passwordVariable: 'pwd', usernameVariable: 'uname')]) {
sh "docker login -u $uname -p '$pwd' $env.DOCKER_REGISTRY"
}
withCredentials([usernamePassword(credentialsId: 'examples-jenkins-user', passwordVariable: 'pwd', usernameVariable: 'uname')]) {
sh "curl -o kubeconfig --user $uname:$pwd https://jenkins.poc.servicefactory.cd.ydev.hybris.com/job/azure/job/ondemand/job/huskiesOnDemand/lastSuccessfulBuild/artifact/kyma/kubeconfig"
}
}
stage("build image $application") {
sh "docker build -t $application:latest ."
}
stage("push image $application") {
sh "docker tag ${application}:latest ${dockerPushRoot}${application}:${dockerImageTag}"
sh "docker push ${dockerPushRoot}${application}:${dockerImageTag}"
}
if (deploy) {
stage("create namespace for $application") {
execute("kubectl create ns ${params.GIT_REVISION}")
}
stage("deploy $application") {
execute("cd examples-chart && helm install --wait --timeout=600 --name examples -f values.yaml --namespace ${params.GIT_REVISION} . --set examples.image=${dockerPushRoot}${application}:${dockerImageTag} " + configureChart(changes))
}
stage("print logs for $application") {
execute("kubectl logs -l chart=examples -n ${params.GIT_REVISION}")
}
/* stage("test $application") {
execute("helm test examples")
}
stage("print test logs for $application") {
execute("kubectl logs -l chart=examples-tests -n ${params.GIT_REVISION}")
} */
stage("delete $application") {
execute("helm delete --purge examples")
}
stage("delete namespace for $application") {
execute("kubectl delete ns ${params.GIT_REVISION}")
}
}
}
}
}
} catch (ex) {
echo "Got exception: ${ex}"
currentBuild.result = "FAILURE"
def body = "${currentBuild.currentResult} ${env.JOB_NAME}${env.BUILD_DISPLAY_NAME}: on branch: ${params.GIT_BRANCH}. See details: ${env.BUILD_URL}"
emailext body: body, recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: "${currentBuild.currentResult}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
}
}
}
def configureChart(changedExamples) {
def set = ""
if (changedExamples.contains("http-db-service")) {
set += "--set examples.httpDBService.deploy=true --set examples.httpDBService.deploymentImage=${dockerPushRoot}example/http-db-service:${dockerImageTag} "
}
if (changedExamples.contains("event-email-service")) {
set += "--set examples.eventEmailService.deploy=true --set examples.eventEmailService.deploymentImage=${dockerPushRoot}example/event-email-service:${dockerImageTag} "
}
if (changedExamples.contains("event-subscription/lambda")) {
set += "--set examples.eventSubscription.lambda.deploy=true "
}
return set
}
def execute(command, envs = '') {
def envText = envs=='' ? '' : "--env $envs"
def workDir = pwd()
sh "docker run --rm --env KUBECONFIG=/kubeconfig $envText ${dockerPushRoot}${application}:${dockerImageTag} /bin/sh -c '$command'"
}
@NonCPS
def parseJson(changedExamples) {
def parser = new JsonSlurperClassic()
return parser.parseText(changedExamples)
}