Skip to content

Commit 955c20d

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
added demos code
1 parent dc95892 commit 955c20d

20 files changed

+818
-1
lines changed

03-cd/01-jenkins/code/.dockerignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Local Devolpment
2+
nodemon.json
3+
4+
# environment setup
5+
.env
6+
.env.test
7+
.env.template
8+
9+
# Test files
10+
/**/*.spec.ts
11+
/**/*.test.ts
12+
13+
# Dependencies
14+
node_modules/
15+
16+
# Local build directory
17+
app/
18+
19+
# Database setup
20+
data/
21+
migrations/
22+
knexfile.js
23+
create_todos_db.sql
24+
Dockerfile.todos_db
25+
26+
# Guides
27+
README.md

03-cd/01-jenkins/code/.env.template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DB_HOST=
2+
DB_USER=
3+
DB_PASSWORD=
4+
DB_PORT=
5+
DB_NAME=
6+
DB_VERSION=

03-cd/01-jenkins/code/.env.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
NODE_ENV=test
2+
PORT=3001
3+
DB_HOST=localhost
4+
DB_USER=postgres
5+
DB_PASSWORD=postgres
6+
DB_PORT=5432
7+
DB_NAME=todos_db
8+
DB_VERSION=10.4

03-cd/01-jenkins/code/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:alpine3.12 as builder
2+
3+
WORKDIR /build
4+
5+
COPY . .
6+
7+
RUN npm install
8+
9+
RUN npm run build
10+
11+
FROM node:alpine3.12 as application
12+
13+
WORKDIR /opt/app
14+
15+
COPY package.json .
16+
17+
COPY package-lock.json .
18+
19+
COPY --from=builder /build/app .
20+
21+
RUN npm i --only=production
22+
23+
CMD ["node", "app.js"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM postgres:10.4
2+
3+
COPY ./create_todos_db.sql /docker-entrypoint-initdb.d

03-cd/01-jenkins/code/README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Todo App API
2+
3+
## Variables de Entorno
4+
5+
```ini
6+
NODE_ENV=
7+
PORT=
8+
DB_HOST=
9+
DB_USER=
10+
DB_PASSWORD=
11+
DB_PORT=
12+
DB_NAME=
13+
DB_VERSION=
14+
```
15+
16+
* **NODE_ENV** El entorno en el que nosencontramos
17+
* **PORT** El puerto donde va a correr la aplicación
18+
* **DB_HOST** El `host` donde se rncuentra corriendo la base de datos
19+
* **DB_USER** El usuario con el cual accedemos a esa base de datos
20+
* **DB_PASSWORD** El password asociado a ese usuario
21+
* **DB_PORT** El puerto en el cual escucha la base de datos
22+
* **DB_NAME** El nombre asociado a la base de datos
23+
* **DB_VERSION** La versión del motor de la base de datos
24+
25+
## Arrancar la Base de Datos usando Docker
26+
27+
Construimos primero la imagen del servidor de la base de datos
28+
29+
```bash
30+
$ docker build -t lemoncode/postgres_todo_server -f Dockerfile.todos_db .
31+
```
32+
33+
Levantamos la base de datos construida anteriormente.
34+
35+
```bash
36+
$ docker run -d -p 5432:5432 -v todos:/var/lib/postgresql/data --name postgres_todo_server lemoncode/postgres_todo_server
37+
```
38+
39+
> NOTA: Para que la base de datos este, el volumen al que estamos mapeando debe de estar vacío, si no la inicialización no ocurrirá.
40+
41+
Para comprobar que la base de datos se encuentra en ejecución debemos ejecutar
42+
43+
```bash
44+
$ docker exec -it postgres_todo_server psql -U postgres
45+
```
46+
47+
Y dentro del listado de la bases de datos, deberíamos ver `todos_db`, para listar las bases de datos `\l`, y después `enter`.
48+
49+
```
50+
postgres=# \l
51+
List of databases
52+
Name | Owner | Encoding | Collate | Ctype | Access privileges
53+
-----------+----------+----------+------------+------------+-----------------------
54+
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
55+
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
56+
| | | | | postgres=CTc/postgres
57+
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
58+
| | | | | postgres=CTc/postgres
59+
todos_db | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
60+
(4 rows)
61+
```
62+
63+
Para conectar con la base de datos en nuestro entorno local, debemos tener creado un fichero `.env` en el raíz, que se vea de la siguiente manera:
64+
65+
```ini
66+
DB_HOST=localhost
67+
DB_USER=postgres
68+
DB_PASSWORD=postgres
69+
DB_PORT=5432
70+
DB_NAME=todos_db
71+
DB_VERSION=10.4
72+
```
73+
74+
Para generar las distintas tablas dentro de nuestra base de datos ejecutaremos:
75+
76+
```bash
77+
$ $(npm bin)/knex migrate:latest
78+
```
79+
80+
Para tener una semilla de datos, podemos lanzar
81+
82+
```bash
83+
$ $(npm bin)/knex seed:run
84+
```
85+
86+
## Ejecutando Unit Tests
87+
88+
Para ejecutar los unit tests, simplemente, tenemos que lanzar el siguiente comando
89+
90+
```bash
91+
$ npm run test
92+
```
93+
94+
## Ejecutando Tests de Integración
95+
96+
Para jecutar los test de integración necesitamos que la base de datos, corriendo y lista para recibir peticiones:
97+
98+
```bash
99+
$ docker run -d -p 5432:5432 -v todos:/var/lib/postgresql/data --name postgres_todo_server lemoncode/postgres_todo_server
100+
```
101+
102+
Después de hacer esto simplemente tenemos que ejecutar:
103+
104+
```bash
105+
$ npm run test:e2e
106+
```
107+
108+
## Ejecutando la aplicación con Docker en nuestro entorno local
109+
110+
```bash
111+
$ docker build -t lemoncode/todo-app .
112+
```
113+
114+
```bash
115+
$ docker network create lemoncode
116+
```
117+
118+
```bash
119+
$ docker run -d -v todos:/var/lib/postgresql/data \
120+
--network lemoncode \
121+
--name pg-todo-server \
122+
lemoncode/postgres_todo_server
123+
```
124+
125+
```bash
126+
$ docker run -d --rm -p 3000:3000 \
127+
--network lemoncode \
128+
-e NODE_ENV=production \
129+
-e PORT=3000 \
130+
-e DB_HOST=pg-todo-server \
131+
-e DB_USER=postgres \
132+
-e DB_PASSWORD=postgres \
133+
-e DB_PORT=5432 \
134+
-e DB_NAME=todos_db \
135+
-e DB_VERSION=10.4 \
136+
lemoncode/todo-app
137+
```
138+
139+
```bash
140+
$ curl localhost:3000/api/
141+
[{"id":1,"title":"Learn GitHub Actions","completed":false,"order":null,"dueDate":null},{"id":2,"title":"Learn Groovy","completed":false,"order":null,"dueDate":null},{"id":3,"title":"Learn EKS","completed":false,"order":null,"dueDate":null}]
142+
```
143+
144+
145+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE todos_db;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
exports.seed = function(knex) {
3+
// Deletes ALL existing entries
4+
return knex('todos').truncate()
5+
.then(function () {
6+
// Inserts seed entries
7+
return knex('todos').insert([
8+
{id: 1, title: 'Learn GitHub Actions', completed: false},
9+
{id: 2, title: 'Learn Groovy', completed: false},
10+
{id: 3, title: 'Learn EKS', completed: false}
11+
]);
12+
});
13+
};
14+

03-cd/01-jenkins/code/jest.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require('dotenv').config({
2+
path: '.env.test',
3+
});
4+
5+
module.exports = {
6+
testEnvironment: 'node',
7+
roots: ['<rootDir>/src'],
8+
testMatch: ['**/?(*.)+(spec).+(ts|js)'],
9+
transform: {
10+
'^.+\\.ts$': 'ts-jest',
11+
},
12+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require('dotenv').config({
2+
path: '.env.test',
3+
});
4+
5+
module.exports = {
6+
testEnvironment: 'node',
7+
roots: ['<rootDir>/src'],
8+
testMatch: ['**/?(*.)+(test).+(ts|js)'],
9+
transform: {
10+
'^.+\\.ts$': 'ts-jest',
11+
},
12+
};

0 commit comments

Comments
 (0)