Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:jenkins [2021/01/28 17:52] alfred [Jenkins] |
wiki2:jenkins [2021/02/10 11:53] (actual) |
||
|---|---|---|---|
| Línea 31: | Línea 31: | ||
| ===== Jenkinsfiles ===== | ===== Jenkinsfiles ===== | ||
| + | ==== Basic one ==== | ||
| + | <code> | ||
| + | pipeline { | ||
| + | agent any | ||
| + | stages { | ||
| + | stage ('Initialize') { | ||
| + | steps { | ||
| + | echo 'Place' | ||
| + | echo 'holder' | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | === Correr una pipeline en contenedor === | ||
| + | Simplemente has de seleccionar el agente docker: | ||
| + | <code> | ||
| + | pipeline { | ||
| + | agent { | ||
| + | docker { image 'node:14-alpine' } | ||
| + | } | ||
| + | stages { | ||
| + | stage('Test') { | ||
| + | steps { | ||
| + | sh 'node --version' | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | === Correr pipeline en varios contenedores === | ||
| + | <code> | ||
| + | pipeline { | ||
| + | agent none | ||
| + | stages { | ||
| + | stage('Back-end') { | ||
| + | agent { | ||
| + | docker { image 'maven:3-alpine' } | ||
| + | } | ||
| + | steps { | ||
| + | sh 'mvn --version' | ||
| + | } | ||
| + | } | ||
| + | stage('Front-end') { | ||
| + | agent { | ||
| + | docker { image 'node:14-alpine' } | ||
| + | } | ||
| + | steps { | ||
| + | sh 'node --version' | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | === Los últimos pasos con post === | ||
| + | <code> | ||
| + | pipeline { | ||
| + | agent any | ||
| + | stages { | ||
| + | stage('Test') { | ||
| + | steps { | ||
| + | sh 'echo "Fail!"; exit 1' | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | post { | ||
| + | always { | ||
| + | echo '[*] This will always run' | ||
| + | } | ||
| + | success { | ||
| + | echo '[*] This will run only if successful' | ||
| + | } | ||
| + | failure { | ||
| + | echo '[*] This will run only if failed' | ||
| + | } | ||
| + | unstable { | ||
| + | echo '[*] This will run only if the run was marked as unstable' | ||
| + | } | ||
| + | changed { | ||
| + | echo '[*] This will run only if the state of the Pipeline has changed' | ||
| + | echo '[*] For example, if the Pipeline was previously failing but is now successful' | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== Útiles ==== | ||
| + | |||
| + | * https://www.jenkins.io/doc/book/pipeline/jenkinsfile/ | ||
| + | * https://www.jenkins.io/doc/book/pipeline/syntax/ | ||
| + | |||
| + | * ''checkout scm'' para obtener el código del SCM. | ||
| + | * ''sh'' para ejecutar un comando shell. | ||
| + | * ''sleep'' para dormir unos segundos. | ||
| + | * ''echo'' para escribir. | ||
| + | |||
| + | |||
| + | === Definir variables === | ||
| + | <code> | ||
| + | def docker_registry = 'https://118864902010.dkr.ecr.us-east-1.amazonaws.com'; | ||
| + | </code> | ||
| + | |||
| + | === String format === | ||
| + | <code> | ||
| + | def docker_registry = 'https://118864902010.dkr.ecr.us-east-1.amazonaws.com'; | ||
| + | def docker_registry_credential = 'ecr:us-east-1:AWSJenkins'; | ||
| + | |||
| + | pipeline { | ||
| + | agent any | ||
| + | environment { | ||
| + | PYPI_CREDS = credentials('pip_jenkins') | ||
| + | } | ||
| + | stages { | ||
| + | stage('Clone') { | ||
| + | steps { | ||
| + | echo docker_registry | ||
| + | echo "a: ${docker_registry}" // Importarte las dobles comillas | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | === Definir funciones === | ||
| + | <code> | ||
| + | |||
| + | ... | ||
| + | post { | ||
| + | failure { | ||
| + | notifyBuild(currentBuild.result); | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | |||
| + | def notifyBuild(String buildStatus = 'STARTED') { | ||
| + | // build status of null means successful | ||
| + | buildStatus = buildStatus ?: 'SUCCESSFUL' | ||
| + | |||
| + | // Default values | ||
| + | def recipientProviders = [[$class: 'DevelopersRecipientProvider']] | ||
| + | def colorName = 'RED' | ||
| + | def colorCode = '#FF0000' | ||
| + | def subject = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}" | ||
| + | def summary = "${subject} (${env.BUILD_URL})" | ||
| + | def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p> | ||
| + | <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""" | ||
| + | |||
| + | // Override default values based on build status | ||
| + | if (buildStatus == 'STARTED') { | ||
| + | color = 'YELLOW' | ||
| + | colorCode = '#FFFF00' | ||
| + | } else if (buildStatus == 'SUCCESSFUL') { | ||
| + | color = 'GREEN' | ||
| + | colorCode = '#00FF00' | ||
| + | } else { | ||
| + | color = 'RED' | ||
| + | colorCode = '#FF0000' | ||
| + | recipientProviders = [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']] | ||
| + | } | ||
| + | |||
| + | // Send notifications | ||
| + | slackSend(color: colorCode, message: summary) | ||
| + | |||
| + | emailext( | ||
| + | subject: "[Jenkins] ${subject}", | ||
| + | mimeType: 'text/html', | ||
| + | body: '''${JELLY_SCRIPT,template="html"}''', | ||
| + | recipientProviders: recipientProviders | ||
| + | ) | ||
| + | } | ||
| + | |||
| + | |||
| + | </code> | ||
| ===== Gotchas ===== | ===== Gotchas ===== | ||