Jenkinsfile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. pipeline {
  2. options {
  3. ansiColor('xterm')
  4. }
  5. agent {
  6. kubernetes {
  7. yamlFile 'builder.yaml'
  8. }
  9. }
  10. // Definición de variables de entorno
  11. environment {
  12. NOMBRE_APP = 'finanzas'
  13. NAMESPACE = 'ftest'
  14. }
  15. // Definición de parámetros
  16. parameters {
  17. booleanParam(name: 'install', description: 'Instalar todo', defaultValue: false)
  18. booleanParam(name: 'clean', description: 'Borrar todo', defaultValue: false)
  19. booleanParam(name: 'claims', description: 'Crea PVC', defaultValue: false)
  20. booleanParam(name: 'borraclaims', description: 'Borra PVC', defaultValue: false)
  21. booleanParam(name: 'namespace', description: 'Crea namespace', defaultValue: false)
  22. booleanParam(name: 'borranamespace', description: 'Borra namespace', defaultValue: false)
  23. }
  24. stages {
  25. stage('Crea las PVC') {
  26. when {
  27. expression { params.claims}
  28. }
  29. steps {
  30. echo "Creando las PVC..."
  31. container('kubectl') {
  32. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  33. sh """#!/bin/bash
  34. sed -i 's|<NS>|"${NAMESPACE}"|' K8S/pvc-${NOMBRE_APP}.yaml
  35. sed -i 's|<NS>|"${NAMESPACE}"|' K8S/pvc-static.yaml
  36. sed -i 's|<NS>|"${NAMESPACE}"|' K8S/pvc-postgresql.yaml
  37. kubectl create -f K8S/pvc-${NOMBRE_APP}.yaml
  38. kubectl create -f K8S/pvc-static.yaml
  39. kubectl create -f K8S/pvc-postgresql.yaml
  40. """
  41. }
  42. }
  43. }
  44. }
  45. stage('Borra las PVC') {
  46. when {
  47. expression { params.borraclaims}
  48. }
  49. steps {
  50. echo "Borrando las PVC..."
  51. container('kubectl') {
  52. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  53. sh 'sh K8S/patchPVC.sh||true'
  54. sh 'kubectl delete -f K8S/pvc-${NOMBRE_APP}.yaml||true'
  55. sh 'kubectl delete -f K8S/pvc-static.yaml||true'
  56. sh 'kubectl delete -f K8S/pvc-postgresql.yaml||true'
  57. }
  58. }
  59. }
  60. }
  61. stage('Borra todo') {
  62. when {
  63. expression { params.clean }
  64. }
  65. steps {
  66. echo "Borrando todo..."
  67. container('kubectl') {
  68. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  69. sh 'kubectl delete -f K8S/db-deployment.yaml||true'
  70. sh 'kubectl delete -f K8S/db-service.yaml||true'
  71. sh 'kubectl delete -f K8S/${NOMBRE_APP}-deployment.yaml||true'
  72. sh 'kubectl delete -f K8S/${NOMBRE_APP}-ingress.yaml||true'
  73. sh 'kubectl delete -f K8S/reg-secret.yaml||true'
  74. sh 'kubectl delete -f K8S/env-prod-configmap.yaml||true'
  75. sh 'kubectl delete -f K8S/env-prod-db-configmap.yaml||true'
  76. }
  77. }
  78. }
  79. }
  80. stage('Borra namespace') {
  81. when {
  82. expression { params.borranamespace }
  83. }
  84. steps {
  85. echo "Borrando namespace..."
  86. container('kubectl') {
  87. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  88. sh 'kubectl delete -f K8S/namespace.yaml||true'
  89. }
  90. }
  91. }
  92. }
  93. stage('Crea namespace') {
  94. when {
  95. expression { params.namespace }
  96. }
  97. steps {
  98. echo "Creando namespace..."
  99. container('kubectl') {
  100. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  101. sh """#!/bin/bash
  102. sed -i 's|<NS>|"${NAMESPACE}"|' K8S/namespace.yaml
  103. kubectl create -f K8S/namespace.yaml
  104. """
  105. }
  106. }
  107. }
  108. }
  109. stage('Construir image y subirla al registry con Kaniko') {
  110. when {
  111. allOf {
  112. expression { !params.clean }
  113. expression { !params.namespace }
  114. expression { !params.borranamespace }
  115. expression { !params.claims }
  116. expression { !params.borraclaims }
  117. }
  118. }
  119. steps {
  120. container('kaniko') {
  121. script {
  122. sh '''
  123. /kaniko/executor --dockerfile `pwd`/Dockerfile \
  124. --context `pwd` \
  125. --destination=registry.reymota.es/${NOMBRE_APP}:${BUILD_NUMBER}
  126. '''
  127. }
  128. }
  129. }
  130. }
  131. stage('Instala todo') {
  132. when {
  133. expression { params.install }
  134. }
  135. steps {
  136. echo "Creando todo..."
  137. container('kubectl') {
  138. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  139. sh 'kubectl create -f K8S/reg-secret.yaml||true'
  140. sh 'kubectl apply -f K8S/env-prod-configmap.yaml'
  141. sh 'kubectl create -f K8S/env-prod-db-configmap.yaml||true'
  142. sh 'kubectl create -f K8S/db-deployment.yaml||true'
  143. sh 'kubectl create -f K8S/db-service.yaml||true'
  144. sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/${NOMBRE_APP}-deployment.yaml'
  145. sh 'kubectl apply -f K8S/${NOMBRE_APP}-deployment.yaml'
  146. sh 'kubectl create -f K8S/${NOMBRE_APP}-ingress.yaml||true'
  147. }
  148. }
  149. }
  150. }
  151. stage('Despliega la aplicación en Kubernetes') {
  152. when {
  153. allOf {
  154. expression { !params.clean }
  155. expression { !params.namespace }
  156. expression { !params.borranamespace }
  157. expression { !params.claims }
  158. expression { !params.borraclaims }
  159. expression { !params.install }
  160. }
  161. }
  162. steps {
  163. echo "Desplegando la aplicación"
  164. container('kubectl') {
  165. withCredentials([file(credentialsId: env.Destino, variable: 'KUBECONFIG')]) {
  166. sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/${NOMBRE_APP}-deployment.yaml'
  167. sh 'kubectl delete -f K8S/${NOMBRE_APP}-deployment.yaml || true'
  168. sh 'kubectl apply -f K8S/${NOMBRE_APP}-deployment.yaml'
  169. }
  170. }
  171. }
  172. }
  173. }
  174. post {
  175. always {
  176. mail to: 'creylopez@yahoo.es',
  177. subject: "Trabajo ${currentBuild.fullDisplayName}",
  178. 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.",
  179. mimeType: 'text/html'
  180. }
  181. failure {
  182. mail to: 'creylopez@yahoo.es',
  183. subject: "El trabajo ${currentBuild.fullDisplayName} ha fallado",
  184. body: "El trabajo ha <b>FALLADO</b>: Comprueba la salida de consola en ${env.BUILD_URL} para ver los resultados.",
  185. mimeType: 'text/html'
  186. }
  187. unstable {
  188. echo 'El trabajo ${currentBuild.fullDisplayName} ha sido marcado como inestable'
  189. }
  190. changed {
  191. mail bcc: '',
  192. body: "El estado del trabajo '${currentBuild.fullDisplayName}' ha cambiado",
  193. from: '',
  194. replyTo: '',
  195. subject: "El estado del trabajo ha cambiado",
  196. to: 'creylopez@yahoo.es',
  197. mimeType: 'text/html'
  198. }
  199. }
  200. }