-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
82 lines (81 loc) · 2.54 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
podTemplate(
namespace: "default",
serviceAccount: "colabot-build",
yaml: '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: docker
image: docker:19.03.8
command:
- sleep
args:
- 99d
env:
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: docker-daemon
image: docker:19.03.8-dind
securityContext:
privileged: true
env:
- name: DOCKER_TLS_CERTDIR
value: ""
- name: kubectl
image: ciscops/kubectl
command: ["sleep"]
args: ["100000"]
''') {
node(POD_LABEL) {
container('docker') {
stage('Clone repository') {
checkout scm
sh "echo '${env.JOB_NAME}'"
branch = getBranch()
sh "echo '${branch}'"
}
stage('Build container') {
if ( "${branch}" == "master" ) {
imageName = "ciscops/colabot-prod"
} else if ( "${branch}" == "dev" ) {
imageName = "ciscops/colabot-dev"
}
colabot = docker.build(imageName)
}
stage('Push container to docker hub ') {
docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
colabot.push("${env.BUILD_NUMBER}")
colabot.push("latest")
}
}
}
container("kubectl") {
stage('Clone k8s manifest') {
sh "apk update"
sh "apk upgrade"
sh "apk add git"
sh 'git config --global credential.helper cache'
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'pass', usernameVariable: 'user')]) {
sh 'git clone https://"$user":"$pass"@github.com/ciscops/colabot-private.git'
}
}
stage('Apply new COLABot-dev to K8s cluster') {
if ( "${branch}" == "dev" ) {
sh "kubectl apply -f colabot-private/colabot_dev/colabot-dev.yaml"
sh "kubectl rollout restart deployment/colabot-dev-1"
sh 'echo Finished'
} else if ( "${branch}" == "master" ) {
sh "kubectl apply -f colabot-private/colabot_prod/colabot-prod.yaml"
sh "kubectl rollout restart deployment/colabot-prod-1"
sh 'echo Finished'
}
}
}
}
}
def getBranch() {
tokens = "${env.JOB_NAME}".tokenize('/')
branch = tokens[tokens.size()-1]
return "${branch}"
}