From 3fcc86b865479df9cb7496d2bf17aa5da22bf575 Mon Sep 17 00:00:00 2001 From: Celestino Rey Date: Fri, 26 Sep 2025 15:56:07 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1ado=20Jenkinsfile=20y=20builder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkisfile | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++ builder.yaml | 31 ++++++++ 2 files changed, 243 insertions(+) create mode 100644 Jenkisfile create mode 100644 builder.yaml diff --git a/Jenkisfile b/Jenkisfile new file mode 100644 index 0000000..f643e1a --- /dev/null +++ b/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//${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//${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 ${currentBuild.result}: 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 FALLADO: 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' + } + } +} diff --git a/builder.yaml b/builder.yaml new file mode 100644 index 0000000..66906c6 --- /dev/null +++ b/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