-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile
128 lines (117 loc) · 3.35 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
def project = 'anishnath'
def appName = 'hello'
def imageTag = "${project}/${appName}:${env.BRANCH_NAME}.${env.BUILD_NUMBER}"
def svcName = "${appName}"
pipeline {
environment {
registry = "anishnath/hello"
registryCredential = 'docker-hub-creds'
dockerImage = ''
}
agent {
kubernetes {
label 'hello-app'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
component: ci
spec:
# Use service account that can deploy to all namespaces
serviceAccountName: jenkins
containers:
- name: golang
image: golang:1.10
command:
- cat
tty: true
- name: kubectl
image: gcr.io/cloud-builders/kubectl
command:
- cat
tty: true
- name: docker
image: docker
command:
- cat
tty: true
volumeMounts:
- name: dockersock
mountPath: "/var/run/docker.sock"
volumes:
- name: dockersock
hostPath:
path: /var/run/docker.sock
"""
}
}
stages {
stage('Test') {
steps {
container('golang') {
sh """
ln -s `pwd` /go/src/hello-app
cd /go/src/hello-app
go test
"""
}
}
}
stage('Build and push image with Container Builder') {
steps {
container('docker') {
//sh "which docker"
//sh "docker build -t ${imageTag} ."
script {
dockerImage = docker.build "$imageTag"
docker.withRegistry( '', registryCredential ) {
dockerImage.push()
}
}
}
}
}
stage('Deploy Canary') {
// Canary branch
when { branch 'canary' }
steps {
container('kubectl') {
sh("sed -i.bak 's#anishnath/hello:latest#${imageTag}#' ./canary/*.yaml")
sh("kubectl --namespace=production apply -f canary/deploy.yaml")
sh("echo http://`kubectl --namespace=production get service/kuebernetes-by-example-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}'` > ${svcName}")
}
}
}
stage('Deploy Production') {
// Production branch
when { branch 'master' }
steps{
container('kubectl') {
sh("sed -i.bak 's#anishnath/hello:latest#${imageTag}#' ./production/*.yaml")
sh("kubectl --namespace=production apply -f production/deploy.yaml")
sh("echo http://`kubectl --namespace=production get service/kuebernetes-by-example-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}'` > ${svcName}")
}
}
}
stage('Deploy Dev') {
// Developer Branches
when {
not { branch 'master' }
not { branch 'canary' }
}
steps {
container('kubectl') {
// Don't use public load balancing for development branches
// Create a Seperate name space for the Development Branch
sh("kubectl get ns ${env.BRANCH_NAME} || kubectl create ns ${env.BRANCH_NAME}")
sh("sed -i.bak 's#anishnath/hello:latest#${imageTag}#' ./dev/*.yaml")
sh("kubectl --namespace=${env.BRANCH_NAME} apply -f dev/deploy.yaml")
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/${svcName}:80/"
}
}
}
}
}