Skip to content

Commit bf85835

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
using groovy methods
1 parent 721d1b4 commit bf85835

File tree

1 file changed

+117
-24
lines changed

1 file changed

+117
-24
lines changed

03-cd/01-jenkins/02-construyendo-pipelines-reusables/00_clean_code_refactor_pipelines.md

Lines changed: 117 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Creamos `01/demo1/1.1/Jenkinsfile`
2222
pipeline {
2323
agent any
2424
environment {
25-
VERSION = ""
25+
VERSION = sh([ script: 'cd ./01/src && npm run env | grep "npm_package_version"', returnStdout: true ]).trim()
2626
VERSION_RC = "rc.2"
2727
}
2828
stages {
@@ -38,12 +38,13 @@ pipeline {
3838
}
3939
stage('Build') {
4040
steps {
41-
env.VERSION = sh([ script: 'npm run env | grep "npm_package_version"', returnStdout: true ]).trim()
42-
echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
43-
sh '''
44-
npm install
45-
npm run build
46-
'''
41+
dir('./01/src') {
42+
echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
43+
sh '''
44+
npm install
45+
npm run build
46+
'''
47+
}
4748
}
4849
}
4950
stage('Unit Test') {
@@ -57,13 +58,13 @@ pipeline {
5758
}
5859
```
5960

60-
## 1.1 Una pipeline que comeinza a hacer trabajo
61+
## 1.1 Una pipeline que comienza a hacer trabajo
6162

6263
Log into Jenkins at http://localhost:8080 with `lemoncode`/`lemoncode`.
6364

6465
- New item, pipeline, `demo1-1`
6566
- Select pipeline from source control
66-
- Git - https://github.com/JaimeSalas/jenkins-pipeline-demos.git
67+
- Git - https://github.com/JaimeSalas/jenkins-pipeline-demos.git https://github.com/Lemoncode/bootcamp-jenkins-demo.git
6768
- Path to Jenkinsfile - `01/demo1/1.1/Jenkinsfile`
6869
- Open in Blue Ocean
6970
- Run
@@ -81,7 +82,7 @@ pipeline {
8182
}
8283
/*diff*/
8384
environment {
84-
VERSION = ""
85+
VERSION = sh([ script: 'cd ./01/src && npm run env | grep "npm_package_version"', returnStdout: true ]).trim()
8586
VERSION_RC = "rc.2"
8687
}
8788
stages {
@@ -102,13 +103,14 @@ pipeline {
102103
}
103104
/*diff*/
104105
steps {
105-
env.VERSION = sh([ script: 'npm run env | grep "npm_package_version"', returnStdout: true ]).trim()
106-
// echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
107-
echo "Building version ${VERSION} with suffix: ${VERSION_SUFFIX}"
108-
sh '''
109-
npm install
110-
npm run build
111-
'''
106+
dir('./01/src') {
107+
// echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
108+
echo "Building version ${VERSION} with suffix: ${VERSION_SUFFIX}"
109+
sh '''
110+
npm install
111+
npm run build
112+
'''
113+
}
112114
}
113115
}
114116
stage('Unit Test') {
@@ -118,14 +120,16 @@ pipeline {
118120
}
119121
}
120122
}
123+
/*diff*/
121124
stage('Publish') {
122125
when {
123126
expression { return params.RC }
124-
steps {
125-
archiveArtifacts('app/')
126-
}
127+
}
128+
steps {
129+
archiveArtifacts('01/src/app/')
127130
}
128131
}
132+
/*diff*/
129133
}
130134
}
131135
```
@@ -139,15 +143,15 @@ Push changes to remote repository
139143

140144
> Walk through the [Jenkinsfile](./1.2/Jenkinsfile)
141145
142-
This is conditional stage, and only will run if `RC` parameter was set to true
146+
Lo importanet es notar aquí, es este paso condicional que sólo se ejecutará si `RC` vale **true**.
143147

144148
```groovy
145149
stage('Publish') {
146150
when {
147151
expression { return params.RC }
148-
steps {
149-
archiveArtifacts('app/')
150-
}
152+
}
153+
steps {
154+
archiveArtifacts('01/src/app/')
151155
}
152156
}
153157
```
@@ -159,3 +163,92 @@ stage('Publish') {
159163
160164
## 1.3 Usando métodos de Groovy
161165

166+
Crear `01/demo1/1.3/Jenkinsfile` empezando desde el anterior y editandolo de la siguiente manera_
167+
168+
```diff
169+
pipeline {
170+
agent any
171+
parameters {
172+
booleanParam(name: 'RC', defaultValue: false, description: 'Is this a Release Candidate?')
173+
}
174+
environment {
175+
VERSION = sh([ script: 'cd ./01/src && npm run env | grep "npm_package_version"', returnStdout: true ]).trim()
176+
VERSION_RC = "rc.2"
177+
}
178+
stages {
179+
stage('Audit tools') {
180+
steps {
181+
- sh '''
182+
- git version
183+
- docker version
184+
- node --version
185+
- npm version
186+
- '''
187+
+ auditTools()
188+
}
189+
}
190+
stage('Build') {
191+
environment {
192+
- VERSION_SUFFIX = "${sh(script:'if [ "${RC}" == "false" ] ; then echo -n "${VERSION_RC}+ci.${BUILD_NUMBER}"; else echo -n "${VERSION_RC}"; fi', returnStdout: true)}"
193+
+ VERSION_SUFFIX = getVersionSuffix()
194+
}
195+
steps {
196+
dir('./01/src') {
197+
echo "Building version ${VERSION} with suffix: ${VERSION_SUFFIX}"
198+
sh '''
199+
npm install
200+
npm run build
201+
'''
202+
}
203+
}
204+
}
205+
stage('Unit Test') {
206+
steps {
207+
dir('./01/src') {
208+
sh 'npm test'
209+
}
210+
}
211+
}
212+
stage('Publish') {
213+
when {
214+
expression { return params.RC }
215+
}
216+
steps {
217+
archiveArtifacts('01/src/app/')
218+
}
219+
}
220+
}
221+
}
222+
+
223+
+String getVersionSuffix() {
224+
+ if (params.RC) {
225+
+ return env.VERSION_RC
226+
+ } else {
227+
+ return env.VERSION_RC + '+ci' + env.BUILD_NUMBER
228+
+ }
229+
+}
230+
+
231+
+void auditTools() {
232+
+ sh '''
233+
+ git version
234+
+ docker version
235+
+ node --version
236+
+ npm version
237+
+ '''
238+
+}
239+
```
240+
241+
Push changes
242+
243+
- Copy item, `demo1-3` from `demo1-1`
244+
- Path to Jenkinsfile `01/demo1/1.3/Jenkinsfile`
245+
- Build now
246+
- Run
247+
248+
> Walk through the [Jenkinsfile](./1.3/Jenkinsfile)
249+
250+
- Open in Blue Ocean
251+
- Run again - _RC = no_
252+
- Run again - _RC = yes_
253+
254+
> Check logs and artifacts

0 commit comments

Comments
 (0)