Skip to content

Commit f193bf3

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
jenkins itroduction added
1 parent 8b51955 commit f193bf3

File tree

2 files changed

+374
-0
lines changed

2 files changed

+374
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Creando y ejecutando Pipelines simples
2+
3+
## 1.1 Crear una pipeline simple
4+
5+
Log in Jenkins en http://localhost:8080 con `lemoncode`/`lemoncode`.
6+
7+
- New item, pipeline, `demo1-1`
8+
- Select sample pipeline script
9+
- Replace echo with `echo "This is build number $BUILD_NUMBER"`
10+
- Run and check output
11+
12+
```groovy
13+
pipeline {
14+
agent any
15+
16+
stages {
17+
stage('Hello') {
18+
steps {
19+
echo "This is build number $BUILD_NUMBER"
20+
}
21+
}
22+
}
23+
}
24+
```
25+
26+
27+
Si hacemos click en los `logs` podemos encontrar la salida de cada `stage`, el número viene de **$BUILD_NUMBER**, la cual es una variable de entorno que nos provee `Jenkins`.
28+
29+
Como es la primera vez que ejecutamos algo dentro del servidor de Jenkins obtenemos la siguiente salida:
30+
31+
```
32+
Started by user Jaime salas
33+
Running in Durability level: MAX_SURVIVABILITY
34+
[Pipeline] Start of Pipeline
35+
[Pipeline] echo
36+
This is build number 1
37+
[Pipeline] End of Pipeline
38+
[Checks API] No suitable checks publisher found.
39+
Finished: SUCCESS
40+
```
41+
42+
## 1.2 Crear una pipeline simple desde Bitbucket
43+
44+
* Crear un nuevo repositorio en Bitbucket `demo1-2`
45+
46+
Navegamos a http://localhost:8080/blue
47+
48+
- New pipeline `demo1-2`
49+
- Bitbucket repo - git clone https://jaimesalas@bitbucket.org/jaimesalas/demo1-2.git
50+
- Log in with Bitbucket creds
51+
52+
> Needs write access to repo
53+
54+
- Build pipeline
55+
- Add environment variable `DEMO=1`
56+
- Add stage
57+
- Add _Print message_ `This is build $BUILD_NUMBER of demo $DEMO`
58+
- Run and check: doesn't interpolate strings
59+
- View Jenkinsfile in repo:editor only uses single quotes
60+
- Replace with shell script `echo "This is build $BUILD_NUMBER of demo $DEMO"`
61+
62+
63+
Jenkins creates the Jenkinsfile into our new repo, we're not going to get the expected result due to use single quotes.
64+
65+
```groovy
66+
pipeline {
67+
agent any
68+
stages {
69+
stage('stage1') {
70+
steps {
71+
echo 'This is the $BUILD_NUMBER of demo $DEMO'
72+
}
73+
}
74+
75+
}
76+
environment {
77+
DEMO = '1'
78+
}
79+
}
80+
```
81+
82+
We add a new step, bash scripting in this case, and commit the new change to master
83+
84+
```groovy
85+
pipeline {
86+
agent any
87+
stages {
88+
stage('stage1') {
89+
steps {
90+
echo 'This is the $BUILD_NUMBER of demo $DEMO'
91+
sh 'echo "This is build $BUILD_NUMBER of demo $DEMO"'
92+
}
93+
}
94+
95+
}
96+
environment {
97+
DEMO = '1'
98+
}
99+
}
100+
```
101+
102+
## 1.3 Crear una Pipeline desde Git
103+
104+
1. Create a new repository on GitHub
105+
2. Add to source control the following code
106+
107+
**./test.sh**
108+
109+
```bash
110+
#!/bin/sh
111+
echo "Inside the script, demo $DEMO"
112+
```
113+
114+
**Jenkinsfile**
115+
116+
```groovy
117+
pipeline {
118+
agent any
119+
120+
environment {
121+
DEMO='1.3'
122+
}
123+
124+
stages {
125+
stage('stage-1') {
126+
steps {
127+
echo "This is the build number $BUILD_NUMBER of demo $DEMO"
128+
sh '''
129+
echo "Using a multi-line shell step"
130+
chmod +x test.sh
131+
./test.sh
132+
'''
133+
}
134+
}
135+
}
136+
}
137+
```
138+
139+
Back in the classic UI http://localhost:8080
140+
141+
- New item, pipeline, `demo1-3`
142+
- Select pipeline from source control
143+
- Git - https://github.com/JaimeSalas/jenkins-pipeline-demos
144+
- Ensure that the source control branch is **main**
145+
146+
> Walk through the [Jenkinsfile](./1.3/Jenkinsfile)
147+
148+
- Run and check
149+
- Open in blue ocean
150+
- Repeat stage
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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

Comments
 (0)