Просмотр исходного кода

Cambios para funcionar con Jenkins

Celestino Rey 1 год назад
Родитель
Сommit
2ed1e1bc38
6 измененных файлов с 206 добавлено и 8 удалено
  1. 203 0
      Jenkinsfile
  2. 2 7
      K8S/finanzas-deployment.yaml
  3. 0 0
      K8S/pvc-finanzas.yaml
  4. 0 0
      K8S/pvc-postgresql.yaml
  5. 0 0
      K8S/pvc-static.yaml
  6. 1 1
      Yamls/finanzas-service.yaml

+ 203 - 0
Jenkinsfile

@@ -0,0 +1,203 @@
+pipeline {
+
+  options {
+    ansiColor('xterm')
+  }
+
+  agent {
+    kubernetes {
+      yamlFile 'builder.yaml'
+    }
+  }
+  
+  // Definición de variables de entorno
+  environment {
+    NOMBRE_APP = 'finanzas'
+  }
+
+  // Definición de parámetros
+  parameters {
+    booleanParam(name: 'install', description: 'Instalar todo', defaultValue: false)
+    booleanParam(name: 'clean', description: 'Borrar todo', defaultValue: false)    
+    booleanParam(name: 'claims', description: 'Crea PVC', defaultValue: false)
+    booleanParam(name: 'borraclaims', description: 'Borra PVC', defaultValue: false)
+    booleanParam(name: 'namespace', description: 'Crea namespace', defaultValue: false)
+    booleanParam(name: 'borranamespace', description: 'Borra namespace', defaultValue: false)
+  }
+  
+  stages {
+    stage('Crea las PVC') {
+      when {
+        expression { params.claims}
+      }
+      steps {
+        echo "Creando las PVC..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'kubectl create -f K8S/pvc-${NOMBRE_APP}.yaml'
+            sh 'kubectl create -f K8S/pvc-static.yaml'
+            sh 'kubectl create -f K8S/pvc-postgresql.yaml'
+          }
+        }
+      }      
+    }
+
+    stage('Borra las PVC') {
+      when {
+        expression { params.borraclaims}
+      }
+      steps {
+        echo "Borrando las PVC..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'kubectl delete -f K8S/pvc-${NOMBRE_APP}.yaml||true'
+            sh 'kubectl delete -f K8S/pvc-static.yaml||true'
+            sh 'kubectl delete -f K8S/pvc-postgresql.yaml||true'
+          }
+        }
+      }     
+    }
+
+    stage('Borra todo') {
+      when {
+        expression { params.clean }
+      }
+      steps {
+        echo "Borrando todo..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'kubectl delete -f K8S/db-deployment.yaml||true'
+            sh 'kubectl delete -f K8S/db-service.yaml||true'
+
+            sh 'kubectl delete -f K8S/${NOMBRE_APP}-deployment.yaml||true'
+            sh 'kubectl delete -f K8S/${NOMBRE_APP}-ingress.yaml||true'
+
+            sh 'kubectl delete -f K8S/reg-secret.yaml||true'
+            sh 'kubectl delete -f K8S/env-prod-configmap.yaml||true' 
+            sh 'kubectl delete -f K8S/env-prod-db-configmap.yaml||true' 
+
+          }
+        }
+      }      
+    }
+
+    stage('Borra namespace') {
+      when {
+        expression { params.borranamespace }
+      }
+      steps {
+        echo "Borrando namespace..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'kubectl delete -f K8S/namespace.yaml||true'
+          }
+        }
+      }      
+    }
+
+    stage('Crea namespace') {
+      when {
+        expression { params.namespace }
+      }
+      steps {
+        echo "Creando namespace..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'kubectl apply -f K8S/namespace.yaml'
+          }
+        }
+      }      
+    }
+
+    stage('Construir image y subirla al registry con Kaniko') {
+      when {
+        allOf {
+          expression { !params.clean }
+          expression { !params.namespace }
+          expression { !params.borranamespace } 
+          expression { !params.claims }
+          expression { !params.borraclaims }
+        }
+      }
+      steps {
+        container('kaniko') {
+          script {
+            sh '''
+            /kaniko/executor --dockerfile `pwd`/Dockerfile \
+                             --context `pwd` \
+                             --destination=registry.reymota.es/${NOMBRE_APP}:${BUILD_NUMBER}
+            '''
+          }
+        }
+      }
+    }
+
+    stage('Instala todo') {
+      when {
+        expression { params.install }
+      }
+      steps {
+        echo "Creando todo..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {           
+            sh 'kubectl create -f K8S/reg-secret.yaml'
+            sh 'kubectl create -f K8S/env-prod-configmap.yaml' 
+            sh 'kubectl create -f K8S/env-prod-db-configmap.yaml' 
+
+            sh 'kubectl create -f K8S/db-deployment.yaml'
+            sh 'kubectl create -f K8S/db-service.yaml'
+
+            sh 'kubectl apply -f K8S/${NOMBRE_APP}-deployment.yaml'
+            sh 'kubectl create -f K8S/${NOMBRE_APP}-ingress.yaml'
+          }
+        }
+      }
+    }
+
+    stage('Despliega la aplicación en Kubernetes') {
+      when {
+        expression { !params.clean }
+      }
+      steps {
+        echo "Desplegando la aplicación" 
+        container('kubectl') {
+          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
+            sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/${NOMBRE_APP}-deployment.yaml'
+            sh 'kubectl delete -f K8S/${NOMBRE_APP}-deployment.yaml || true'          
+            sh 'kubectl apply -f K8S/${NOMBRE_APP}-deployment.yaml'
+          }
+        }
+      }
+    }   
+  }
+  
+  post {
+    always {
+      mail to: 'creylopez@yahoo.es',
+        subject: "Trabajo ${currentBuild.fullDisplayName}",
+        body: "El resultado del trabajo '${currentBuild.fullDisplayName}' ha sido <b>${currentBuild.result}</b>: Comprueba la salida de consola en ${env.BUILD_URL} para ver los resultados.",
+        mimeType: 'text/html'
+    }
+
+    failure {
+      mail to: 'creylopez@yahoo.es',
+        subject: "El trabajo ${currentBuild.fullDisplayName} ha fallado",
+        body: "El trabajo ha <b>FALLADO</b>: Comprueba la salida de consola en ${env.BUILD_URL} para ver los resultados.",
+        mimeType: 'text/html'
+    }
+
+    unstable {
+      echo 'El trabajo ${currentBuild.fullDisplayName} ha sido marcado como inestable'
+    }
+
+    changed {
+      mail bcc: '', 
+           body: "El estado del trabajo '${currentBuild.fullDisplayName}' ha cambiado",
+           from: '', 
+           replyTo: '', 
+           subject: "El estado del trabajo ha cambiado", 
+           to: 'creylopez@yahoo.es',
+           mimeType: 'text/html'
+    }
+  }
+}

+ 2 - 7
K8S/finanzas-deployment.yaml

@@ -31,13 +31,8 @@ spec:
         app: finanzas
     spec:
       containers:
-      - args:
-          - gunicorn
-          - misfinanzas.wsgi:application
-          - --bind
-          - 0.0.0.0:8000
-        name: finanzas
-        image: $REGISTRY/finanzas-$ARQUITECTURA:$IMG_VERSION
+      - name: finanzas
+        image: registry.reymota.es/finanzas:<TAG>
         env:
         - name: IMG_VERSION
           value: "$IMG_VERSION"

+ 0 - 0
K8S/finanzas-prod-persistentvolumeclaim.yaml → K8S/pvc-finanzas.yaml


+ 0 - 0
K8S/postgres-data-persistentvolumeclaim.yaml → K8S/pvc-postgresql.yaml


+ 0 - 0
K8S/static-volume-persistentvolumeclaim.yaml → K8S/pvc-static.yaml


+ 1 - 1
Yamls/finanzas-service.yaml

@@ -4,7 +4,7 @@ metadata:
   name: finanzas
   namespace: finanzas
 spec:
-  type: LoadBalancer
+  type: NodePort
   ports:
     - name: "8080"
       port: 8080