-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
103 lines (93 loc) · 4.68 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
node {
def dockerhubuser = 'vykozlov'
def appName = 'tf-mnist-cd'
def mainVer = "1.0"
def imageTag = "${dockerhubuser}/${appName}:${env.BRANCH_NAME}-${mainVer}.${env.BUILD_NUMBER}-gpu"
def k8sConfigMaster = "/home/jenkins/.kube/config.master"
def jname = "jupyter-pass"
def jpassfile = "./jpassword"
try {
stage ('Clone repository') {
checkout scm
}
stage('Build image') {
sh("nvidia-docker build -t ${imageTag} .")
}
stage('Run tests') {
//sh("nvidia-docker run ${imageTag} python tools/tf_vers.py")
echo "Here should be some tests.."
}
stage('Push image to registry') {
echo "${imageTag}"
withCredentials([usernamePassword(credentialsId: 'dockerhub-vykozlov-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh '''
docker login -u ${USERNAME} -p ${PASSWORD}
'''
}
sh("docker push ${imageTag}")
}
stage('Deploy Application') {
switch (env.BRANCH_NAME) {
// Roll out to production
case "master":
// Change deployed image in canary to the one we just built
withCredentials([usernamePassword(credentialsId: 'jupyter-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
//how to pass parameters here??
sh '''
echo -n ${PASSWORD} > ./jpassword
'''
}
sh("kubectl --kubeconfig=${k8sConfigMaster} create secret generic ${jname} --from-file=${jpassfile} --namespace=production --dry-run -o json >${jpassfile}.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} apply -f ${jpassfile}.yaml")
sh("rm ${jpassfile} ${jpassfile}.yaml")
sh("sed -i.bak 's#vykozlov/tf-mnist-cd:1.5.0-gpu#${imageTag}#' ./k8s/production/*.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} --namespace=production apply -f k8s/services/tf-mnist-cd-svc.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} --namespace=production apply -f k8s/production/")
break
// Roll out a dev environment
default:
// Create namespace if it doesn't exist
sh("kubectl --kubeconfig=${k8sConfigMaster} get ns ${env.BRANCH_NAME} || kubectl --kubeconfig=${k8sConfigMaster} create ns ${env.BRANCH_NAME}")
withCredentials([usernamePassword(credentialsId: 'jupyter-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh '''
echo -n ${PASSWORD} > ./jpassword
'''
}
sh("kubectl --kubeconfig=${k8sConfigMaster} create secret generic ${jname} --from-file=${jpassfile} --namespace=${env.BRANCH_NAME} --dry-run -o json >${jpassfile}.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} apply -f ${jpassfile}.yaml")
sh("rm ${jpassfile} ${jpassfile}.yaml")
// Don't use public load balancing for development branches
sh("sed -i.bak 's#vykozlov/tf-mnist-cd:1.5.0-gpu#${imageTag}#' ./k8s/dev/*.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} --namespace=${env.BRANCH_NAME} apply -f k8s/services/tf-mnist-cd-dev-svc.yaml")
sh("kubectl --kubeconfig=${k8sConfigMaster} --namespace=${env.BRANCH_NAME} apply -f k8s/dev/")
//echo 'To access your environment run `kubectl proxy`'
//echo "Then access your service via http://localhost:8001/api/v1/proxy/namespaces/${env.BRANCH_NAME}/services/${feSvcName}:80/"
}
}
stage('Post Deploymnet') {
// delete docker image from Jenkins site
sh("docker rmi ${imageTag}")
}
} catch (e) {
// If there was an exception thrown, the build failed
currentBuild.result = "FAILED"
throw e
} finally {
// Success or failure, always send notifications
notifyBuild(currentBuild.result)
}
}
def notifyBuild(String buildStatus = 'STARTED') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>"""
emailext (
subject: subject,
body: details,
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}