blob: 7b204206c3af1f0203d44693da714b7647c5ccbd [file] [log] [blame]
jv246a.orane3ba9652020-05-05 15:37:02 -04001#!/usr/bin/env groovy
2
3/* Copyright (c) 2019 AT&T Intellectual Property. #
4# #
5# Licensed under the Apache License, Version 2.0 (the "License"); #
6# you may not use this file except in compliance with the License. #
7# You may obtain a copy of the License at #
8# #
9# http://www.apache.org/licenses/LICENSE-2.0 #
10# #
11# Unless required by applicable law or agreed to in writing, software #
12# distributed under the License is distributed on an "AS IS" BASIS, #
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14# See the License for the specific language governing permissions and #
15# limitations under the License. #
16##############################################################################*/
17
18properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
19 [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
20 [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
21 [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "id_otf_dev"],
22 [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
23 [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "registry.hub.docker.io"]
24]]])
25
26
27echo "Build branch: ${env.BRANCH_NAME}"
28
29node("docker"){
30 stage 'Checkout'
31 checkout scm
32 PHASES=PHASE.tokenize( '_' );
33 echo "PHASES : " + PHASES
34
35
36 ARTIFACT_ID="a1-mediator-vth";
37 VERSION="0.0.1-SNAPSHOT";
38 NAMESPACE="org-oran-otf"
39 DOCKER_REGISTRY="registry.hub.docker.io"
40
41 if( ENV.equalsIgnoreCase("dev") ){
42 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".dev" + "/" + ARTIFACT_ID + ":" + VERSION
43
44 }
45 if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
46 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
47
48 }
49
50 if( ENV.equalsIgnoreCase("st") ){
51 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
52
53 }
54
55 echo "Artifact: " + IMAGE_NAME
56
57 withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {
58
59 echo "PATH=${env.PATH}"
60 echo "HELM_HOME=${env.HELM_HOME}"
61
62 if (PHASES.contains("BUILD")){
63
64 stage 'Publish Artifact'
65
66 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
Chen, Jackieb4313732020-05-21 12:53:27 -040067 dir("docker") {
jv246a.orane3ba9652020-05-05 15:37:02 -040068 echo "Artifact: " + IMAGE_NAME
69
70 sh """
71 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
72 docker build -t $IMAGE_NAME .
73 docker push $IMAGE_NAME
74 """
75 }
76 }
77
78 }
79
80 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
81
82 stage 'Init Helm'
83
84 //check if helm exists if not install
85 if(fileExists('linux-amd64/helm')){
86 sh """
87 echo "helm is already installed"
88 """
89 }
90 else{
91 //download helm
92 sh """
93 echo "installing helm"
94 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.3-linux-amd64.tar.gz
95 tar -xf helm-v2.14.3-linux-amd64.tar.gz
96 rm helm-v2.14.3-linux-amd64.tar.gz
97 """
98 }
99
100 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
101
102 dir('a1-mediator-vth/helm'){
103 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
104
105 if (PHASES.contains("UNDEPLOY") ) {
106 stage 'Undeploy'
107
108 sh """
109 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
110 """
111 }
112
113 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
114 if (PHASES.contains("DEPLOY") ){
115 stage 'Deploy'
116 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
117
118 sh """
119 helm version
120 echo "Validate Yaml"
121 helm lint $ARTIFACT_ID
122
123 echo "View Helm Templates"
124 helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \
125 --set appName=$ARTIFACT_ID \
126 --set version=$VERSION \
127 --set env=$ENV \
128 --set image=$IMAGE_NAME \
129 --set namespace=$TILLER_NAMESPACE
130
131 echo "Perform Dry Run Of Install"
132 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
133 --set appName=$ARTIFACT_ID \
134 --set version=$VERSION \
135 --set env=$ENV \
136 --set image=$IMAGE_NAME \
137 --set namespace=$TILLER_NAMESPACE
138
139 echo "Helm Install/Upgrade"
140 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
141 --set appName=$ARTIFACT_ID \
142 --set version=$VERSION \
143 --set env=$ENV \
144 --set image=$IMAGE_NAME \
145 --set namespace=$TILLER_NAMESPACE
146
147 """
148 }
149 }
150
151 }
152 }
153 }
154
155 }
156}