blob: fb42d019aa89c2369884f004346479e643d0dc0b [file] [log] [blame]
Rohan Patelf49bd1e2019-09-23 15:04:19 -04001#!/usr/bin/env groovy
2
Rohan Patele19624e2019-11-11 16:30:46 -05003/* 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##############################################################################*/
Rohan Patelf49bd1e2019-09-23 15:04:19 -040017
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: "org-oran-otf"]
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="otf-ssh-test-head";
37 VERSION="Blitzcrank.1.1";
38 NAMESPACE="org-oran-otf"
39 DOCKER_REGISTRY="registry.hub.docker.io"
40
41 if( ENV.equalsIgnoreCase("dev") ){
42 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + "/" + 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')]) {
67
68 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 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
80
81 stage 'Init Helm'
82
83 //check if helm exists if not install
84 if(fileExists('linux-amd64/helm')){
85 sh """
86 echo "helm is already installed"
87 """
88 }
89 else{
90 //download helm
91 sh """
92 echo "installing helm"
93 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
94 tar -xf helm-v2.8.2-linux-amd64.tar.gz
95 rm helm-v2.8.2-linux-amd64.tar.gz
96 """
97 }
98
99 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
100
101 dir('helm'){
102 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
103
104 if (PHASES.contains("UNDEPLOY") ) {
105 stage 'Undeploy'
106
107 sh """
108 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
109 """
110 }
111
112 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
113 if (PHASES.contains("DEPLOY") ){
114 stage 'Deploy'
115 withCredentials([
116 usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
117 file(credentialsId: 'id_PRIVATE_KEY', variable: 'VAR_id_PRIVATE_KEY'),
118 string(credentialsId: 'id_otf_key_passphrase', variable: 'VAR_id_otf_key_passphrase')
119 ]) {
120
121 sh """
122
123 cp $VAR_id_PRIVATE_KEY $ARTIFACT_ID
124 FILE_id_PRIVATE_KEY=`basename $VAR_id_PRIVATE_KEY`
125
126 echo "Validate Yaml"
127 helm lint $ARTIFACT_ID
128
129 echo "View Helm Templates"
130 helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \
131 --set appName=$ARTIFACT_ID \
132 --set version=$VERSION \
133 --set env=$ENV \
134 --set image=$IMAGE_NAME \
135 --set namespace=$TILLER_NAMESPACE \
136 --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
137 --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
138
139
140
141 echo "Perform Dry Run Of Install"
142 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
143 --set appName=$ARTIFACT_ID \
144 --set version=$VERSION \
145 --set env=$ENV \
146 --set image=$IMAGE_NAME \
147 --set namespace=$TILLER_NAMESPACE \
148 --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
149 --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
150
151
152 echo "Helm Install/Upgrade"
153 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
154 --set appName=$ARTIFACT_ID \
155 --set version=$VERSION \
156 --set env=$ENV \
157 --set image=$IMAGE_NAME \
158 --set namespace=$TILLER_NAMESPACE \
159 --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
160 --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
161
162 """
163 }
164 }
165
166 }
167 }
168 }
169
170 }
171}