Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. pipeline {
  2. options {
  3. ansiColor('xterm')
  4. }
  5. agent {
  6. kubernetes {
  7. yamlFile 'builder.yaml'
  8. }
  9. }
  10. parameters {
  11. booleanParam(name: 'install', description: 'Instalar todo', defaultValue: false)
  12. booleanParam(name: 'clean', description: 'Borrar todo', defaultValue: false)
  13. }
  14. stages {
  15. stage('Borra todo') {
  16. when {
  17. expression { params.clean }
  18. }
  19. steps {
  20. echo "Borrando todo..."
  21. container('kubectl') {
  22. withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
  23. sh 'kubectl delete -f K8S/db-deployment.yaml||true'
  24. sh 'kubectl delete -f K8S/db-service.yaml||true'
  25. sh 'kubectl delete -f K8S/jugaralpadel-jenkins-deployment.yaml||true'
  26. sh 'kubectl delete -f K8S/jugaralpadel-ingress.yaml||true'
  27. sh 'kubectl delete -f K8S/pvc-jugaralpadel.yaml||true'
  28. sh 'kubectl delete -f K8S/pvc-static.yaml||true'
  29. sh 'kubectl delete -f K8S/pvc-postgresql.yaml||true'
  30. sh 'kubectl delete -f K8S/reg-secret.yaml||true'
  31. sh 'kubectl delete -f K8S/env-prod-configmap.yaml||true'
  32. sh 'kubectl delete -f K8S/env-prod-db-configmap.yaml||true'
  33. sh 'kubectl delete -f K8S/namespace.yaml||true'
  34. }
  35. }
  36. }
  37. }
  38. stage('Kaniko Build & Push Image') {
  39. when {
  40. expression { !params.clean }
  41. }
  42. steps {
  43. container('kaniko') {
  44. script {
  45. sh '''
  46. /kaniko/executor --dockerfile `pwd`/Dockerfile \
  47. --context `pwd` \
  48. --destination=registry.reymota.es/jugaralpadel:${BUILD_NUMBER}
  49. '''
  50. }
  51. }
  52. }
  53. }
  54. stage('Instala todo') {
  55. when {
  56. expression { params.install }
  57. }
  58. steps {
  59. echo "Creando todo..."
  60. container('kubectl') {
  61. withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
  62. sh 'kubectl apply -f K8S/namespace.yaml'
  63. sh 'kubectl create -f K8S/reg-secret.yaml'
  64. sh 'kubectl create -f K8S/env-prod-configmap.yaml'
  65. sh 'kubectl create -f K8S/env-prod-db-configmap.yaml'
  66. sh 'kubectl create -f K8S/pvc-jugaralpadel.yaml'
  67. sh 'kubectl create -f K8S/pvc-static.yaml'
  68. sh 'kubectl create -f K8S/pvc-postgresql.yaml'
  69. sh 'kubectl create -f K8S/db-deployment.yaml'
  70. sh 'kubectl create -f K8S/db-service.yaml'
  71. sh 'kubectl apply -f K8S/jugaralpadel-jenkins-deployment.yaml'
  72. sh 'kubectl create -f K8S/jugaralpadel-ingress.yaml'
  73. }
  74. }
  75. }
  76. }
  77. stage('Deploy App to Kubernetes') {
  78. when {
  79. expression { !params.clean }
  80. }
  81. steps {
  82. echo "Desplegando la aplicación"
  83. container('kubectl') {
  84. withCredentials([file(credentialsId: 'kubeconfig-imac', variable: 'KUBECONFIG')]) {
  85. sh 'sed -i "s/<TAG>/${BUILD_NUMBER}/" K8S/jugaralpadel-jenkins-deployment.yaml'
  86. sh 'kubectl delete -f K8S/jugaralpadel-jenkins-deployment.yaml || true'
  87. sh 'kubectl apply -f K8S/jugaralpadel-jenkins-deployment.yaml'
  88. }
  89. }
  90. }
  91. }
  92. }
  93. post {
  94. always {
  95. mail to: 'creylopez@yahoo.es',
  96. subject: "Trabajo ${currentBuild.fullDisplayName}",
  97. 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.",
  98. mimeType: 'text/html'
  99. }
  100. failure {
  101. mail to: 'creylopez@yahoo.es',
  102. subject: "El trabajo ${currentBuild.fullDisplayName} ha fallado",
  103. body: "El trabajo ha <b>FALLADO</b>: Comprueba la salida de consola en ${env.BUILD_URL} para ver los resultados.",
  104. mimeType: 'text/html'
  105. }
  106. unstable {
  107. echo 'El trabajo ${currentBuild.fullDisplayName} ha sido marcado como inestable'
  108. }
  109. changed {
  110. mail bcc: '',
  111. body: "El estado del trabajo '${currentBuild.fullDisplayName}' ha cambiado",
  112. from: '',
  113. replyTo: '',
  114. subject: "El estado del trabajo ha cambiado",
  115. to: 'creylopez@yahoo.es',
  116. mimeType: 'text/html'
  117. }
  118. }
  119. }