En mi día a día, trabajo bastante con Jenkins. Nada del otro universo, una maquinita local, corriendo jenkins, vinculada con gcloud y cuenta de servicio. Tengo toda la potencia del bash de unix y la versatilidad de Jenkins como CI.
pipeline { agent any environment { STAGE_NAME_BB = "" SRC_PATH="[local_src_path]" #Ruta donde deberá encontrarse el proyecto GIT que debe ser compilado. SCRIPTS_PATH="[scripts_path]" #Ruta donde se almacena el script para eliminar las imágenes antiguas NAMESPACE="[k8_namespace]" K8_OBJ="[workload_type]/[workload_name]" CONTAINER="[container_name]" IMAGE="[src_image]" } stages { stage("Compile Code"){ steps { notifyBuild("STARTED") script{ STAGE_NAME_BB = "Compile Code" } echo "Building..." sh "Comandos a ejecutar para la compilación del código" } } stage("Unit Testing"){ steps { script{ STAGE_NAME_BB = "Unit testing" } echo "En este apartado puedes configurar los test para tu código" } } stage("Image Push & Tag into Container Registry"){ steps { script{ STAGE_NAME_BB = "Image Push & Tag into Container Registry" } echo "# Subir la imagen al container registry..." sh "cd ${SRC_PATH} && mvn jib:build -Djib.to.image=${IMAGE}:${BUILD_NUMBER}" echo "# Agregar la etiqueta latest a la imagen que acabamos de subir" sh "cd ${SRC_PATH} && gcloud container images add-tag ${IMAGE}:${BUILD_NUMBER} ${IMAGE}:latest --quiet" } } stage("Deploying Image into its Kubernetes Container"){ steps { script{ STAGE_NAME_BB = "Deploying Image into its Kubernetes Container" } echo "# Connecting to dev cluster" sh "gcloud container clusters get-credentials [cluster-name] --zone [cluster-zone] --project [project-name]" echo "# Configurar la nueva imagen como la ultima imagen de la carga de trabajo especificada" sh "cd ${SRC_PATH} && kubectl set image ${K8_OBJ} ${CONTAINER}=${IMAGE}:${BUILD_NUMBER} --record -n ${NAMESPACE}" } } stage("Delete Container Registry Old Images"){ steps { script{ STAGE_NAME_BB = "Delete Container Registry Old Images" } echo "# Deleting old images" echo "# Para mas detalles de este script, podéis consultar esta entrada" sh "cd ${SCRIPTS_PATH} && ./delete_old_images.sh ${IMAGE}" } } } post { success { notifyBuild("SUCCESSFUL") } failure { notifyBuild("FAILED ${STAGE_NAME_BB}") } } } def notifyBuild(String buildStatus = 'STARTED') { // build status of null means successful buildStatus = buildStatus ?: 'SUCCESSFUL' // Default values def colorName = 'RED' def colorCode = '#FF0000' def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'" def summary = "${subject} (${env.BUILD_URL})" // Override default values based on build status if (buildStatus == 'STARTED'){ color = 'YELLOW' colorCode = '#FFFF00' summary = "${subject} (${env.BUILD_URL})" } else if (buildStatus == 'SUCCESSFUL') { color = 'GREEN' colorCode = '#00FF00' } }
Problema resuelto!
Chuleta – Pipeline para Jenkins