Browse Source

Nuevo Jenkinsfile para construir, crear y limpiar

creylopez 11 months ago
parent
commit
3c0eb4095f
1 changed files with 104 additions and 8 deletions
  1. 104 8
      Jenkinsfile

+ 104 - 8
Jenkinsfile

@@ -9,15 +9,51 @@ pipeline {
       yamlFile 'builder.yaml'
     }
   }
-
+  
+  parameters {
+    booleanParam(name: 'install', description: 'Instalar todo', defaultValue: false)
+    booleanParam(name: 'clean', description: 'Borrar todo', defaultValue: false)
+  }
+  
   stages {
 
-    stage('Construye y sube la imagen usando Kaniko') {
+    stage('Borra todo') {
+      when {
+        expression { params.clean }
+      }
+      steps {
+        echo "Borrando todo..." 
+        container('kubectl') {
+          withCredentials([file(credentialsId: 'kubeconfig-imac', 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/jugaralpadel-jenkins-deployment.yaml||true'
+            sh 'kubectl delete -f K8S/jugaralpadel-ingress.yaml||true'
+
+            sh 'kubectl delete -f K8S/pvc-jugaralpadel.yaml||true'
+            sh 'kubectl delete -f K8S/pvc-static.yaml||true'
+            sh 'kubectl delete -f K8S/pvc-postgresql.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' 
+
+            sh 'kubectl delete -f K8S/namespace.yaml||true'
+          }
+        }
+      }      
+    }
+
+    stage('Kaniko Build & Push Image') {
+      when {
+        expression { !params.clean }
+      }
       steps {
         container('kaniko') {
           script {
             sh '''
-            /kaniko/executor --dockerfile `pwd`/Dockerfile.oc \
+            /kaniko/executor --dockerfile `pwd`/Dockerfile \
                              --context `pwd` \
                              --destination=registry.reymota.es/jugaralpadel:${BUILD_NUMBER}
             '''
@@ -26,17 +62,77 @@ pipeline {
       }
     }
 
-    stage('Despliega en Kubernetes') {
+    stage('Instala todo') {
+      when {
+        expression { params.install }
+      }
       steps {
+        echo "Creando todo..." 
         container('kubectl') {
-          withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
-          # withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
+          withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
+            sh 'kubectl apply -f K8S/namespace.yaml'
+            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/pvc-jugaralpadel.yaml'
+            sh 'kubectl create -f K8S/pvc-static.yaml'
+            sh 'kubectl create -f K8S/pvc-postgresql.yaml'
+
+            sh 'kubectl create -f K8S/db-deployment.yaml'
+            sh 'kubectl create -f K8S/db-service.yaml'
+
+            sh 'kubectl apply -f K8S/jugaralpadel-jenkins-deployment.yaml'
+            sh 'kubectl create -f K8S/jugaralpadel-ingress.yaml'
+          }
+        }
+      }
+    }
+
+    stage('Deploy App to Kubernetes') {
+      when {
+        expression { !params.clean }
+      }
+      steps {
+        echo "Desplegando la aplicación" 
+        container('kubectl') {
+          withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
             sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/jugaralpadel-jenkins-deployment.yaml'
-            sh 'kubectl delete -f K8S/jugaralpadel-jenkins-deployment.yaml'          
-            sh 'kubectl apply -f K8S/jugaralpadel-jenkins-deployment.yaml'          
+            sh 'kubectl delete -f K8S/jugaralpadel-jenkins-deployment.yaml || true'          
+            sh 'kubectl apply -f K8S/jugaralpadel-jenkins-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'
     }
   }
 }