blob: 8143fcf351ae2ce8cb343048d63f472312ca282d [file] [log] [blame]
Rohan Patelf49bd1e2019-09-23 15:04:19 -04001#!/usr/bin/env groovy
2
3
4properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
5 [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
6 [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
7 [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "id_otf_dev"],
8 [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
9 [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]
10]]])
11
12
13echo "Build branch: ${env.BRANCH_NAME}"
14
15node("docker"){
16 stage 'Checkout'
17 checkout scm
18 PHASES=PHASE.tokenize( '_' );
19 echo "PHASES : " + PHASES
20
21
22 ARTIFACT_ID="otf-ping-test-head";
23 VERSION="Blitzcrank.1.1";
24 NAMESPACE="org-oran-otf"
25 DOCKER_REGISTRY="registry.hub.docker.io"
26
27 if( ENV.equalsIgnoreCase("dev") ){
28 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
29
30 }
31 if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
32 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
33
34 }
35
36 if( ENV.equalsIgnoreCase("st") ){
37 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
38
39 }
40
41 echo "Artifact: " + IMAGE_NAME
42
43 withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {
44
45 echo "PATH=${env.PATH}"
46 echo "HELM_HOME=${env.HELM_HOME}"
47
48 if (PHASES.contains("BUILD")){
49
50 stage 'Publish Artifact'
51
52 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
53
54 echo "Artifact: " + IMAGE_NAME
55
56 sh """
57 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
58 docker build -t $IMAGE_NAME .
59 docker push $IMAGE_NAME
60 """
61 }
62
63 }
64
65 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
66
67 stage 'Init Helm'
68
69 //check if helm exists if not install
70 if(fileExists('linux-amd64/helm')){
71 sh """
72 echo "helm is already installed"
73 """
74 }
75 else{
76 //download helm
77 sh """
78 echo "installing helm"
79 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
80 tar -xf helm-v2.8.2-linux-amd64.tar.gz
81 rm helm-v2.8.2-linux-amd64.tar.gz
82 """
83 }
84
85 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
86
87 dir('helm'){
88 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
89
90 if (PHASES.contains("UNDEPLOY") ) {
91 stage 'Undeploy'
92
93 sh """
94 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
95 """
96 }
97
98 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
99 if (PHASES.contains("DEPLOY") ){
100 stage 'Deploy'
101 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
102
103 sh """
104 echo "Validate Yaml"
105 helm lint $ARTIFACT_ID
106
107 echo "View Helm Templates"
108 helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \
109 --set appName=$ARTIFACT_ID \
110 --set version=$VERSION \
111 --set env=$ENV \
112 --set image=$IMAGE_NAME \
113 --set namespace=$TILLER_NAMESPACE
114
115 echo "Perform Dry Run Of Install"
116 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
117 --set appName=$ARTIFACT_ID \
118 --set version=$VERSION \
119 --set env=$ENV \
120 --set image=$IMAGE_NAME \
121 --set namespace=$TILLER_NAMESPACE
122
123
124 echo "Helm Install/Upgrade"
125 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
126 --set appName=$ARTIFACT_ID \
127 --set version=$VERSION \
128 --set env=$ENV \
129 --set image=$IMAGE_NAME \
130 --set namespace=$TILLER_NAMESPACE
131
132 """
133 }
134 }
135
136 }
137 }
138 }
139
140 }
141}