Celestino Rey 10 месяцев назад
Родитель
Сommit
3fcc86b865
2 измененных файлов с 243 добавлено и 0 удалено
  1. 212 0
      Jenkisfile
  2. 31 0
      builder.yaml

+ 212 - 0
Jenkisfile

@@ -0,0 +1,212 @@
+pipeline {
+
+  options {
+    ansiColor('xterm')
+  }
+
+  agent {
+    kubernetes {
+      yamlFile 'builder.yaml'
+    }
+  }
+  
+  // Definición de variables de entorno
+  environment {
+    NOMBRE_APP = 'reymota'
+  }
+
+  // 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 'sh K8S/patchPVC.sh||true'
+            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 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/${NOMBRE_APP}-deployment.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 {
+        allOf {
+          expression { !params.clean }
+          expression { !params.namespace }
+          expression { !params.borranamespace } 
+          expression { !params.claims }
+          expression { !params.borraclaims }
+          expression { !params.install }
+        }
+      }
+      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'
+    }
+  }
+}

+ 31 - 0
builder.yaml

@@ -0,0 +1,31 @@
+apiVersion: v1
+kind: Pod
+metadata:
+  name: kaniko
+  namespace: jenkins
+spec:
+  hostAliases:
+  - ip: "192.168.1.116"
+    hostnames:
+    - "api.ocp-cluster.reymota.lab"
+  containers:
+  - name: kubectl
+    image: joshendriks/alpine-k8s
+    command:
+    - /bin/cat
+    tty: true    
+  - name: kaniko
+    image: gcr.io/kaniko-project/executor:debug
+    command:
+    - /busybox/cat
+    tty: true
+    volumeMounts:
+      - name: kaniko-secret
+        mountPath: /kaniko/.docker
+  volumes:
+    - name: kaniko-secret
+      secret:
+        secretName: regcred
+        items:
+          - key: .dockerconfigjson
+            path: config.json