Skip to content

Commit 71cdc1c

Browse files
authored
Merge pull request #5 from Lemoncode/feature/jenkins-fixes
2 parents 6bd779f + 50f9ba3 commit 71cdc1c

File tree

8 files changed

+94
-131
lines changed

8 files changed

+94
-131
lines changed
Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
## Downloading and running Jenkins in Docker
22

3-
### 1. Create a network for Jenkis
3+
### 1. Create a network for Jenkins
44

55
```bash
66
$ docker network create jenkins
77
```
88

9-
### 2. Create the following volumes to share the Docker client TLS certificates needed to connect to the Docker daemon and persist the Jenkins data
9+
### 2. Create the following volumes to share the Docker client TLS certificates needed to connect to the Docker daemon and persist the Jenkins data
1010

1111
```bash
12-
$ docker volume create jenkins-docker-certs
13-
$ docker volume create jenkins-data
12+
$ docker volume create jenkins-docker-certs
13+
$ docker volume create jenkins-data
1414
```
1515

1616
### 3. In order to execute Docker commands inside Jenkins nodes, download and run the docker:dind Docker image
@@ -22,11 +22,11 @@ docker container run \
2222
--detach \
2323
--privileged \
2424
--network jenkins \
25-
--ntework-alias docker \
25+
--network-alias docker \
2626
--env DOCKER_TLS_CERTDIR=/certs \
2727
--volume jenkins-docker-certs:/certs/client \
2828
--volume jenkins-data:/var/jenkins_home \
29-
--publish 2376:2376
29+
--publish 2376:2376 \
3030
docker:dind
3131
```
3232

@@ -39,37 +39,26 @@ docker container run \
3939
--detach \ # 3
4040
--privileged \ # 4
4141
--network jenkins \ # 5
42-
--ntework-alias docker \ # 6
42+
--network-alias docker \ # 6
4343
--env DOCKER_TLS_CERTDIR=/certs \ # 7
4444
--volume jenkins-docker-certs:/certs/client \ # 8
4545
--volume jenkins-data:/var/jenkins_home \ # 9
46-
--publish 2376:2376 # 10
46+
--publish 2376:2376 \ # 10
4747
docker:dind # 11
4848
```
4949

50-
1. The Docker conatiner name
51-
2. Removes the Docker conatiner instance when it is shut down.
50+
1. The Docker container name.
51+
2. Removes the Docker container instance when it is shut down.
5252
3. Runs the Docker container in the background.
53-
4. Running Docker in Docker currently requires provileged access to function properly.
54-
5. Join to previous created network
53+
4. Running Docker in Docker currently requires privileged access to function properly.
54+
5. Join to previous created network.
5555
6. Makes the Docker in Docker container available as the hostname _docker_ within the _jenkins_ network.
5656
7. Enables the use of TLS in the Docker server. Due to the use of a privileged container, this is recommended, though it requires the use of the shared volume described below. This environment variable controls the root directory where Docker TLS certificates are managed.
5757
8. Maps the _/certs/client_ directory inside the container to a Docker volume named _jenkins-docker-certs_ as created above.
58-
9. Maps the */var/jenkins_home* directory inside the container to the Docker volume named _jenkins-data_ as created above. This will allow for other Docker containers controlled by this Docker containers Docker daemon to mount data from Jenkins.
58+
9. Maps the _/var/jenkins_home_ directory inside the container to the Docker volume named _jenkins-data_ as created above. This will allow for other Docker containers controlled by this Docker container's Docker daemon to mount data from Jenkins.
5959
10. Exposes the Docker daemon port on the host machine. This is useful for executing _docker_ commands on the host machine to control this inner Docker daemon.
6060
11. The _docker:dind_ image itself.
6161

62-
* Annotation free version
63-
64-
```bash
65-
docker container run --name jenkins-docker --rm --detach \
66-
--privileged --network jenkins --network-alias docker \
67-
--env DOCKER_TLS_CERTDIR=/certs \
68-
--volume jenkins-docker-certs:/certs/client \
69-
--volume jenkins-data:/var/jenkins_home \
70-
--publish 2376:2376 docker:dind
71-
```
72-
7362
### 4. Run jenkins as a container
7463

7564
```bash
@@ -88,7 +77,7 @@ docker container run \
8877
jenkinsci/blueocean
8978
```
9079

91-
#### Commands explantion
80+
#### Commands explanation
9281

9382
```
9483
docker container run \
@@ -106,51 +95,33 @@ docker container run \
10695
jenkinsci/blueocean # 10
10796
```
10897

109-
1. Specifies the Docker container name for this instance of the _jenkinsci/blueocean_ Docker image.
98+
1. Specifies the Docker container name for this instance of the _jenkinsci/blueocean_ Docker image.
11099

111-
2. Removes the Docker conatiner instance when it is shut down.
100+
2. Removes the Docker container instance when it is shut down.
112101

113102
3. Runs the Docker container in the background.
114103

115104
4. Connects this container to the jenkins network defined in the earlier step. This makes the Docker daemon from the previous step available to this Jenkins container through the hostname docker.
116105

117106
5. Specifies the environment variables used by `docker`, `docker-compose`, and other Docker tools to connect to the Docker daemon from the previous step.
118107

119-
6. Maps (i.e. "publishes") port 8080 of the _jenkinsci/blueocean_ container to port 8080 on the host machine. The first number represents the port on the host while the last represents the containers port. Therefore, if you specified _-p 49000:8080_ for this option, you would be accessing Jenkins on your host machine through port 49000.
108+
6. Maps (i.e. "publishes") port 8080 of the _jenkinsci/blueocean_ container to port 8080 on the host machine. The first number represents the port on the host while the last represents the container's port. Therefore, if you specified _-p 49000:8080_ for this option, you would be accessing Jenkins on your host machine through port 49000.
120109

121110
7. Maps port 50000 of the jenkinsci/blueocean container to port 50000 on the host machine. This is only necessary if you have set up one or more inbound Jenkins agents on other machines, which in turn interact with the _jenkinsci/blueocean_ container (the Jenkins "controller"). Inbound Jenkins agents communicate with the Jenkins controller through TCP port 50000 by default. You can change this port number on your Jenkins controller through the Configure Global Security page. If you were to change the TCP port for inbound Jenkins agents of your Jenkins controller to 51000 (for example), then you would need to re-run Jenkins (via this docker run …​ command) and specify this "publish" option with something like _--publish 52000:51000_, where the last value matches this changed value on the Jenkins controller and the first value is the port number on the machine hosting the Jenkins controller. Inbound Jenkins agents communicate with the Jenkins controller on that port (52000 in this example). Note that WebSocket agents in Jenkins 2.217 do not need this configuration.
122111

123-
8. Maps the */var/jenkins_home* directory in the container to the Docker volume with the name _jenkins-data_. Instead of mapping the */var/jenkins_home* directory to a Docker volume, you could also map this directory to one on your machines local file system. For example, specifying the option
124-
*--volume $HOME/jenkins:/var/jenkins_home* would map the container’s */var/jenkins_home* directory to the jenkins subdirectory within the *$HOME* directory on your local machine, which would typically be */Users/<your-username>/jenkins* or */home/<your-username>/jenkins*. Note that if you change the source volume or directory for this, the volume from the _docker:dind_ container above needs to be updated to match this.
112+
8. Maps the _/var/jenkins_home_ directory in the container to the Docker volume with the name _jenkins-data_. Instead of mapping the _/var/jenkins_home_ directory to a Docker volume, you could also map this directory to one on your machine's local file system. For example, specifying the option
113+
_--volume $HOME/jenkins:/var/jenkins_home_ would map the container's _/var/jenkins_home_ directory to the jenkins subdirectory within the _$HOME_ directory on your local machine, which would typically be _/Users/<your-username>/jenkins_ or _/home/<your-username>/jenkins_. Note that if you change the source volume or directory for this, the volume from the _docker:dind_ container above needs to be updated to match this.
125114

126-
9. Maps the _/certs/client_ directory to the previously created _jenkins-docker-certs_ volume. This makes the client TLS certificates needed to connect to the Docker daemon available in the path specified by the *DOCKER_CERT_PATH* environment variable.
115+
9. Maps the _/certs/client_ directory to the previously created _jenkins-docker-certs_ volume. This makes the client TLS certificates needed to connect to the Docker daemon available in the path specified by the _DOCKER_CERT_PATH_ environment variable.
127116

128117
10. The _jenkinsci/blueocean_ Docker image itself.
129118

130-
* Annotation-free version
131-
132-
```bash
133-
docker container run --name jenkins-blueocean --rm --detach \
134-
--network jenkins --env DOCKER_HOST=tcp://docker:2376 \
135-
--env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
136-
--volume jenkins-data:/var/jenkins_home \
137-
--volume jenkins-docker-certs:/certs/client:ro \
138-
--publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean
139-
```
140-
141119
## Starting Jenkins
142120

143121
To unlock Jenkins we have to paste a password, we can find the password inside the running container, run the following command `cat /var/jenkins_home/secrets/initialAdminPassword` to obtain the initial password
144122

145123
```bash
146-
$ docker container exec -it jenkins-blueocean bash
147-
```
148-
149-
```bash
150-
$ ls
151-
bin certs dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
152-
$ cat /var/jenkins_home/secrets/initialAdminPassword
153-
8b0ee7a1a0214fe0a3029b8232c56087
124+
$ docker container exec jenkins-blueocean cat /var/jenkins_home/secrets/initialAdminPassword
154125
```
155126

156-
Now install the suggested plugins and wait until Jenkins finishes
127+
Now install the suggested plugins and wait until Jenkins finishes

03-cd/01-jenkins/00-instalando-jenkins/start_jenkins.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ DATA_VOLUME=$4
77
# Reference: https://forums.docker.com/t/how-to-filter-docker-ps-by-exact-name/2880/3
88

99
if [[ ! $(docker network ls --filter name=^/$NETWORK$) ]]; then
10-
docker network create jenkins
10+
docker network create $NETWORK
1111
echo network jenkins ${$NETWORK}
1212
fi
1313

1414
if [[ ! $(docker volume ls --filter name=^/$CERTS_VOLUME$) ]]; then
1515
docker volume create $CERTS_VOLUME
1616
echo jenkins certs "$CERTS_VOLUME"
17-
fi
17+
fi
1818

19-
if [[ ! $(docker volume ls --filter name=^/$CERTS_VOLUME$) ]]; then
19+
if [[ ! $(docker volume ls --filter name=^/$DATA_VOLUME$) ]]; then
2020
docker volume create $DATA_VOLUME
2121
echo jenkins data "$DATA_VOLUME"
22-
fi
22+
fi
2323

2424
# StartDocker in Docker into jenkins network
2525
docker container run --name jenkins-docker --rm --detach \
@@ -40,4 +40,4 @@ docker container run --name jenkins-blueocean --rm --detach \
4040
# Ensure that script have permissions to be executed: chmod +x start_jenkins.sh
4141
# Build image from Dockerfile: docker build -t lemoncode/jenkins .
4242
# From previous image we can run custom Jenkins version as follows:
43-
# ./start_jenkins.sh lemoncode/jenkins jenkins jenkins-docker-certs jenkins-data
43+
# ./start_jenkins.sh lemoncode/jenkins jenkins jenkins-docker-certs jenkins-data

03-cd/01-jenkins/01-introduccion/01_modelando_workflows_en_pipelines.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## 1.1 Una pipeline multi-stage
44

5-
* Crear un nuevo Jenkinsfile en `demo1/1.1/Jenkinsfile`
5+
- Crear un nuevo Jenkinsfile en `demo1/1.1/Jenkinsfile`
66

77
```groovy
88
pipeline {
9-
agent any
9+
agent any
1010
environment {
1111
RELEASE='0.0.1'
1212
}
@@ -39,7 +39,7 @@ Log in Jenkins en http://localhost:8080 con `lemoncode`/`lemoncode`.
3939

4040
> Walk through the [Jenkinsfile](./1.1/Jenkinsfile)
4141
42-
- Run and check
42+
- Run and check
4343
- Fails because step in second stage uses unknown variable `LOG_LEVEL`
4444

4545
```
@@ -50,7 +50,7 @@ groovy.lang.MissingPropertyException: No such property: LOG_LEVEL for class: gro
5050

5151
## 1.2 Solicitando el input del Usuario
5252

53-
* Crear un nuevo Jenkinsfile en `demo1/1.2/Jenkinsfile`
53+
- Crear un nuevo Jenkinsfile en `demo1/1.2/Jenkinsfile`
5454

5555
```groovy
5656
pipeline {
@@ -96,25 +96,25 @@ pipeline {
9696

9797
Back to http://localhost:8080
9898

99-
- Copy item, `demo1-1.1` from `demo1-1.2`
99+
- Copy item, `demo1-1.2` from `demo1-1.1`
100100
- Path to Jenkinsfile `demo1/1.2/Jenkinsfile`
101101

102102
> Walk through the [Jenkinsfile](./1.2/Jenkinsfile)
103103
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}"
104+
```groovy
105+
input {
106+
message 'Deploy?'
107+
ok 'Do it!'
108+
parameters {
109+
string(name: 'TARGET_ENVIRONMENT', defaultValue: 'PROD', description: 'Target deployment environment')
114110
}
111+
}
112+
steps {
113+
echo "Deploying release ${RELEASE} to environment ${TARGET_ENVIRONMENT}"
114+
}
115115
```
116116

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.
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.
118118

119119
```
120120
post {
@@ -133,7 +133,7 @@ En el comando _post_ podemos tener diferentes condiciones aquí estamos usando _
133133

134134
## 1.3 Parallel stages
135135

136-
* Crear un nuevo Jenkinsfile en `demo1/1.3/Jenkinsfile`
136+
- Crear un nuevo Jenkinsfile en `demo1/1.3/Jenkinsfile`
137137

138138
```groovy
139139
pipeline {
@@ -217,8 +217,8 @@ parallel {
217217
}
218218
```
219219

220-
Con _parallel_ podemos ejecutar mútiples `stages` en paralelo.
220+
Con _parallel_ podemos ejecutar múltiples `stages` en paralelo.
221221

222222
- Run and check
223223
- Parallel stages complete in any order
224-
- Then pause on input and then post
224+
- Then pause on input and then post

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Eliminar las pipelines anteriores
88

99
Ejecutar Jenkins en Docker [Docker](https://www.docker.com/products/docker-desktop)
1010

11-
Crear un nuevo directorio *src_tmp/jenkins-demos-review/01*. `unzip` del fichero `01-jenkins/code.zip`
11+
Crear un nuevo directorio _src_tmp/jenkins-demos-review/01_. `unzip` del fichero `01-jenkins/code.zip`
1212

1313
```bash
14-
$ unzip code.zip -d ./src_temp/jenkins-demos-review/01
14+
$ unzip code.zip -d ./src_tmp/jenkins-demos-review/01
1515
```
1616

1717
Hacemos `push` de los cambios al repositorio remoto
@@ -37,7 +37,7 @@ pipeline {
3737
}
3838
}
3939
stage('Build') {
40-
steps {
40+
steps {
4141
dir('./01/src') {
4242
echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
4343
sh '''
@@ -65,7 +65,7 @@ Log into Jenkins at http://localhost:8080 with `lemoncode`/`lemoncode`.
6565
- New item, pipeline, `demo1-1`
6666
- Select pipeline from source control
6767
- Git - https://github.com/JaimeSalas/jenkins-pipeline-demos.git https://github.com/Lemoncode/bootcamp-jenkins-demo.git
68-
- Path to Jenkinsfile - `01/demo1/1.1/Jenkinsfile`
68+
- Path to Jenkinsfile - `01/demo1/1.1/Jenkinsfile`
6969
- Open in Blue Ocean
7070
- Run
7171

@@ -102,7 +102,7 @@ pipeline {
102102
VERSION_SUFFIX = "${sh(script:'if [ "${RC}" == "false" ] ; then echo -n "${VERSION_RC}+ci.${BUILD_NUMBER}"; else echo -n "${VERSION_RC}"; fi', returnStdout: true)}"
103103
}
104104
/*diff*/
105-
steps {
105+
steps {
106106
dir('./01/src') {
107107
// echo "Building version ${VERSION} with suffix: ${VERSION_RC}"
108108
echo "Building version ${VERSION} with suffix: ${VERSION_SUFFIX}"
@@ -143,7 +143,7 @@ Push changes to remote repository
143143

144144
> Walk through the [Jenkinsfile](./1.2/Jenkinsfile)
145145
146-
Lo importanet es notar aquí, es este paso condicional que sólo se ejecutará si `RC` vale **true**.
146+
Lo importante que hay que tener en cuenta es este paso condicional que sólo se ejecutará si `RC` vale **true**.
147147

148148
```groovy
149149
stage('Publish') {
@@ -163,7 +163,7 @@ stage('Publish') {
163163
164164
## 1.3 Usando métodos de Groovy
165165

166-
Crear `01/demo1/1.3/Jenkinsfile` empezando desde el anterior y editandolo de la siguiente manera_
166+
Crear `01/demo1/1.3/Jenkinsfile` empezando desde el anterior y editándolo de la siguiente manera\_:
167167

168168
```diff
169169
pipeline {
@@ -192,7 +192,7 @@ pipeline {
192192
- VERSION_SUFFIX = "${sh(script:'if [ "${RC}" == "false" ] ; then echo -n "${VERSION_RC}+ci.${BUILD_NUMBER}"; else echo -n "${VERSION_RC}"; fi', returnStdout: true)}"
193193
+ VERSION_SUFFIX = getVersionSuffix()
194194
}
195-
steps {
195+
steps {
196196
dir('./01/src') {
197197
echo "Building version ${VERSION} with suffix: ${VERSION_SUFFIX}"
198198
sh '''
@@ -234,7 +234,7 @@ pipeline {
234234
+ docker version
235235
+ node --version
236236
+ npm version
237-
+ '''
237+
+ '''
238238
+}
239239
```
240240

@@ -251,4 +251,4 @@ Push changes
251251
- Run again - _RC = no_
252252
- Run again - _RC = yes_
253253

254-
> Check logs and artifacts
254+
> Check logs and artifacts

0 commit comments

Comments
 (0)