|
| 1 | +# Modelando workflows en las pipelines |
| 2 | + |
| 3 | +## 1.1 Una pipeline multi-stage |
| 4 | + |
| 5 | +* Crear un nuevo Jenkinsfile en `demo1/1.1/Jenkinsfile` |
| 6 | + |
| 7 | +```groovy |
| 8 | +pipeline { |
| 9 | + agent any |
| 10 | + environment { |
| 11 | + RELEASE='0.0.1' |
| 12 | + } |
| 13 | + stages { |
| 14 | + stage('Build') { |
| 15 | + agent any |
| 16 | + environment { |
| 17 | + LOG_LEVEL='INFO' |
| 18 | + } |
| 19 | + steps { |
| 20 | + echo "Building release ${RELEASE} with log level ${LOG_LEVEL}..." |
| 21 | + } |
| 22 | + } |
| 23 | + stage('Test') { |
| 24 | + steps { |
| 25 | + echo "Testing. I can see release ${RELEASE}, but not log level ${LOG_LEVEL}" |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Log in Jenkins en http://localhost:8080 con `lemoncode`/`lemoncode`. |
| 33 | + |
| 34 | +- New item, pipeline, `demo1-1.1` |
| 35 | +- Select pipeline from source control |
| 36 | +- Git - https://github.com/JaimeSalas/jenkins-pipeline-demos.git |
| 37 | +- Branch `main` |
| 38 | +- Path to Jenkinsfile `demo1/1.1/Jenkinsfile` |
| 39 | + |
| 40 | +> Walk through the [Jenkinsfile](./1.1/Jenkinsfile) |
| 41 | +
|
| 42 | +- Run and check |
| 43 | +- Fails because step in second stage uses unknown variable `LOG_LEVEL` |
| 44 | + |
| 45 | +``` |
| 46 | +[Checks API] No suitable checks publisher found. |
| 47 | +groovy.lang.MissingPropertyException: No such property: LOG_LEVEL for class: groovy.lang.Binding |
| 48 | + at groovy.lang.Binding.getVariable(Binding.java:63) |
| 49 | +``` |
| 50 | + |
| 51 | +## 1.2 Solicitando el input del Usuario |
| 52 | + |
| 53 | +* Crear un nuevo Jenkinsfile en `demo1/1.2/Jenkinsfile` |
| 54 | + |
| 55 | +```groovy |
| 56 | +pipeline { |
| 57 | + agent any |
| 58 | + environment { |
| 59 | + RELEASE='0.0.1' |
| 60 | + } |
| 61 | + stages { |
| 62 | + stage('Build') { |
| 63 | + agent any |
| 64 | + environment { |
| 65 | + LOG_LEVEL='INFO' |
| 66 | + } |
| 67 | + steps { |
| 68 | + echo "Building release ${RELEASE} with log level ${LOG_LEVEL}..." |
| 69 | + } |
| 70 | + } |
| 71 | + stage('Test') { |
| 72 | + steps { |
| 73 | + echo "Testing release ${RELEASE}..." |
| 74 | + } |
| 75 | + } |
| 76 | + stage('Deploy') { |
| 77 | + input { |
| 78 | + message 'Deploy?' |
| 79 | + ok 'Do it!' |
| 80 | + parameters { |
| 81 | + string(name: 'TARGET_ENVIRONMENT', defaultValue: 'PROD', description: 'Target deployment environment') |
| 82 | + } |
| 83 | + } |
| 84 | + steps { |
| 85 | + echo "Deploying release ${RELEASE} to environment ${TARGET_ENVIRONMENT}" |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + post { |
| 90 | + always { |
| 91 | + echo 'Prints wether deploy happened or not, success or failure' |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Back to http://localhost:8080 |
| 98 | + |
| 99 | +- Copy item, `demo1-1.1` from `demo1-1.2` |
| 100 | +- Path to Jenkinsfile `demo1/1.2/Jenkinsfile` |
| 101 | + |
| 102 | +> Walk through the [Jenkinsfile](./1.2/Jenkinsfile) |
| 103 | +
|
| 104 | +``` |
| 105 | + input { |
| 106 | + message 'Deploy?' |
| 107 | + ok 'Do it!' |
| 108 | + parameters { |
| 109 | + string(name: 'TARGET_ENVIRONMENT', defaultValue: 'PROD', description: 'Target deployment environment') |
| 110 | + } |
| 111 | + } |
| 112 | + steps { |
| 113 | + echo "Deploying release ${RELEASE} to environment ${TARGET_ENVIRONMENT}" |
| 114 | + } |
| 115 | +``` |
| 116 | + |
| 117 | +Este paso solicitará una entrada del usuario. Notar que en el siguiente `stage`, podemos acceder a *TARGET_ENVIRONMENT*. Si seleccionamos _abort_ los pasos siguientes no se realizarán. |
| 118 | + |
| 119 | +``` |
| 120 | +post { |
| 121 | + always { |
| 122 | + echo 'Prints wether deploy happened or not, success or failure' |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +En el comando _post_ podemos tener diferentes condiciones aquí estamos usando _always_ |
| 128 | + |
| 129 | +- Run and check |
| 130 | +- Open console from running build |
| 131 | +- Pauses on input stage - OK or abort |
| 132 | +- Post runs every time |
| 133 | + |
| 134 | +## 1.3 Parallel stages |
| 135 | + |
| 136 | +* Crear un nuevo Jenkinsfile en `demo1/1.3/Jenkinsfile` |
| 137 | + |
| 138 | +```groovy |
| 139 | +pipeline { |
| 140 | + agent any |
| 141 | + environment { |
| 142 | + RELEASE='0.0.1' |
| 143 | + } |
| 144 | + stages { |
| 145 | + stage('Build') { |
| 146 | + environment { |
| 147 | + LOG_LEVEL='INFO' |
| 148 | + } |
| 149 | + parallel { |
| 150 | + stage('linux-arm64') { |
| 151 | + steps { |
| 152 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 153 | + } |
| 154 | + } |
| 155 | + stage('linux-amd64') { |
| 156 | + steps { |
| 157 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 158 | + } |
| 159 | + } |
| 160 | + stage('windows-amd64') { |
| 161 | + steps { |
| 162 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + stage('Test') { |
| 168 | + steps { |
| 169 | + echo "Testing release ${RELEASE}..." |
| 170 | + } |
| 171 | + } |
| 172 | + stage('Deploy') { |
| 173 | + input { |
| 174 | + message 'Deploy?' |
| 175 | + ok 'Do it!' |
| 176 | + parameters { |
| 177 | + string(name: 'TARGET_ENVIRONMENT', defaultValue: 'PROD', description: 'Target deployment environment') |
| 178 | + } |
| 179 | + } |
| 180 | + steps { |
| 181 | + echo "Deploying release ${RELEASE} to environment ${TARGET_ENVIRONMENT}" |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + post { |
| 186 | + always { |
| 187 | + echo 'Prints wether deploy happened or not, success or failure' |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Volver a http://localhost:8080 |
| 194 | + |
| 195 | +- Copy item, `demo1-1.3` from `demo1-1.2` |
| 196 | +- Path to Jenkinsfile `demo1/1.3/Jenkinsfile` |
| 197 | + |
| 198 | +> Walk through the [Jenkinsfile](./1.3/Jenkinsfile) |
| 199 | +
|
| 200 | +``` |
| 201 | +parallel { |
| 202 | + stage('linux-arm64') { |
| 203 | + steps { |
| 204 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 205 | + } |
| 206 | + } |
| 207 | + stage('linux-amd64') { |
| 208 | + steps { |
| 209 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 210 | + } |
| 211 | + } |
| 212 | + stage('windows-amd64') { |
| 213 | + steps { |
| 214 | + echo "Building release ${RELEASE} for ${STAGE_NAME} with log level ${LOG_LEVEL}..." |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +Con _parallel_ podemos ejecutar mútiples `stages` en paralelo. |
| 221 | + |
| 222 | +- Run and check |
| 223 | +- Parallel stages complete in any order |
| 224 | +- Then pause on input and then post |
0 commit comments