Skip to content

Commit

Permalink
Merge pull request #3 from fga-eps-mds/feature/#143-CI_CD
Browse files Browse the repository at this point in the history
(#143) adiciona deploy e altera arquivos de configuracao docker
  • Loading branch information
HenriqueAmorim20 authored Oct 24, 2023
2 parents 1e8d5a8 + 4f933e3 commit f5152f3
Show file tree
Hide file tree
Showing 26 changed files with 429 additions and 9,646 deletions.
13 changes: 9 additions & 4 deletions .docker/entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash
# TODO
npm install
npm run start:dev
#!/bin/sh

echo "---------------Run migrations---------------"

node /app/migrations.js

echo "---------------Run migrations - END---------"

node /app/main.js
11 changes: 9 additions & 2 deletions .docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#!/bin/bash

cd /home/node/app
source .env.development

npm install --legacy-peer-deps

rm -rf dist

echo "---------------Run migrations---------------"

npm run typeorm:run

echo "---------------Run migrations - END---------"
npm run start:dev

npm run start:debug
9 changes: 3 additions & 6 deletions .docker/entrypoint.test.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/bin/bash

echo ".................................."
echo "TEST CONTAINER:"

cd home/node/app
echo "TEST:" $TEST

npm install --legacy-peer-deps
npm run lint
npm run typeorm:run


if ! [ -z $TEST ] && [ $TEST == 'unit' ]; then
echo "Running unit tests"
npm run test:cov
Expand All @@ -20,7 +17,7 @@ elif ! [ -z $TEST ] && [ $TEST == 'lint' ]; then
echo "Running lint"
npm run lint
npm run format
else
echo "Running test"
elif ! [ -z $TEST ] && [ $TEST == 'dev' ]; then
echo "Running dev"
npm run start:dev
fi
2 changes: 1 addition & 1 deletion .docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ ENV LANG pt_BR.utf8
ENV LC_ALL pt_BR.utf8
ENV LANGUAGE pt_BR.utf8

EXPOSE 5432
EXPOSE 5003
2 changes: 1 addition & 1 deletion .docker/postgres/config/postgresql.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ listen_addresses = '*'
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5003 # (change requires restart)
port = 5003 # (change requires restart)
max_connections = 300 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ jobs:
TEST=e2e docker-compose -f docker-compose.test.yml up -V --force-recreate --build --abort-on-container-exit --exit-code-from gerocuidado-saude-api-test
env:
TEST: e2e

sonarqube:
name: sonarqube
needs: ['test-unit', 'test-e2e']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install dependencies
run: yarn
- name: Test and coverage
run: yarn jest --coverage
47 changes: 47 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Deploy

on:
push:
branches: ['main']
tags: ['v*.*.*']

env:
TARGET_DIR: '~/${{ github.event.repository.name }}'

jobs:
docker-hub:
name: Docker Hub Image Push
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Sending Image to Docker Hub
uses: mr-smithers-excellent/docker-build-push@v5
with:
image: gerocuidadodev/gerocuidado-saude-api
registry: docker.io
directory: ./
dockerfile: ./Dockerfile.prod
tags: latest
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

deploy-ec2:
name: Deploy EC2
needs: docker-hub
runs-on: ubuntu-latest
steps:
- name: Checkout the files
uses: actions/checkout@v2

- name: Executing remote ssh commands using ssh key
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST_DNS }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
cd ${{env.TARGET_DIR}}
sudo git pull
sudo docker compose -f docker-compose.prod.yml up --force-recreate --build --pull --remove-orphans -d
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
{
"type": "node",
"request": "attach",
"protocol": "inspector",
"restart": true,
"name": "gerocuidado-saude-api",
"name": "API",
"port": 7003,
"address": "0.0.0.0",
"localRoot": "${workspaceFolder}/",
Expand Down
15 changes: 3 additions & 12 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18.13.0 AS BUILD_IMAGE
FROM node:18.17.0-alpine AS BUILD_IMAGE

RUN apk update && apk add curl bash make && rm -rf /var/cache/apk/*

Expand All @@ -15,7 +15,7 @@ COPY . .
RUN npm run build


FROM node:18.13.0 AS BUILD_NODE_MODULES
FROM node:18.17.0-alpine AS BUILD_NODE_MODULES

RUN apk update && apk add curl bash make && rm -rf /var/cache/apk/*

Expand All @@ -26,19 +26,10 @@ COPY package*.json ./
RUN npm ci --legacy-peer-deps --omit=dev


FROM node:18.13.0
FROM node:18.17.0-alpine

ENV NODE_ENV=production

ARG TZ='America/Sao_Paulo'

ENV TZ ${TZ}

RUN apk upgrade --update
RUN apk add --no-cache tzdata
RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime
RUN echo "${TZ}" > /etc/timezone

WORKDIR /app

COPY --chown=node:node --from=BUILD_IMAGE /home/node/app/dist /app
Expand Down
85 changes: 37 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,69 @@
# GEROcuidado-APISaude
# gerocuidado-saude-api

## CONFIGURAÇÃO
## Configuração

Definir valores iguais para os arquivos .env e docker-compose.
Definir valores iguais para os arquivos .env e docker-compose

Arquivo .env:
Arquivo .env.development e .env.test:

#POSTGRES
DB_TYPE='postgres'
DB_HOST='localhost'
DB_USERNAME='postgres'
DB_PASS='postgres'
DB_DATABASE='gerocuidado-saude-db'
DB_PORT=5003
DB_TYPE=
DB_HOST=
DB_USERNAME=
DB_PASS=
DB_DATABASE=
DB_PORT=

Arquivo .docker-compose, na seção **_environment_**:

...
environment:
- POSTGRES_DB=gerocuidado-saude-db
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=
- POSTGRES_USER=
- POSTGRES_PASSWORD=
...

Da mesma forma, alterar os valores das portas terminadas em **_xx_** (i.e 30xx para 3003) para a porta desejada nos arquivos de compose, bem como no arquivo launch.json da pasta .vscode.
Da mesma forma, alterar os valores das portas terminadas em **_x_** (i.e 3003 para 3003) para a porta desejada nos arquivos de compose, bem como no arquivo launch.json da pasta .vscode.

## AMBIENTES
## Execução

### DEV
Para subir a aplicação, basta rodar o comando:

```bash
docker-compose up
```

#### INSTALAR DEPENDÊNCIAS E CRIAR NOVAS FUNCIONALIDADES

```bash
docker-compose exec gerocuidado-saude-api bash
nest g resource users
```
```bash
docker compose up
```

### TEST
## Testes

- GERAL
Para testar a aplicação, suba o container de testes:

```bash
docker-compose -f docker-compose.test.yml up
TEST=dev docker compose -f docker-compose.test.yml up
```

após em um novo terminal

E rode os comandos para os testes unitários e E2E respectivamente (:cov gera o arquivo de coverage na raiz do projeto):
```bash
docker-compose -f docker-compose.test.yml exec gerocuidado-saude-api-test bash
cd /home/node/app
npm run test
npm run test:cov
npm run test:e2e
npm run test:e2e:cov
npm run test:cov
npm run test:e2e:cov
```

- UNIT
## Migrations

Para apenas rodar os testes unitários e sair do container rode o comando abaixo:
Sempre que houver qualquer alteração em alguma entidade na aplicação (adicionar uma entidade, remover ou edita-la), deve ser gerada uma migration para sincronizar o banco de dados.

```bash
TEST=unit docker-compose -f docker-compose.test.yml up --abort-on-container-exit --exit-code-from gerocuidado-saude-api-test
```
1. Entrar no container da api:

- E2E
Para apenas rodar os testes unitários e sair do container rode o comando abaixo:
```bash
docker exec -it gerocuidado-saude-api bash
```

2. Rodar o comando de criar uma migration (tente dar um nome descritivo, ex.: CreateTableExemplo)

```bash
TEST=e2e docker-compose -f docker-compose.test.yml up --abort-on-container-exit --exit-code-from gerocuidado-saude-api-test
```
```bash
npm run typeorm:migrate src/migration/NOME_DA_MIGRATION
```

# ENVIRONMENTS VARIABLES
# Dicionário variáveis de ambiente

| ENV | Descrição | Valor Padrão |
| ----------- | ---------------------- | -------------------- |
Expand Down
38 changes: 38 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3.7'
services:
gerocuidado-saude-api-prod:
image: gerocuidadodev/gerocuidado-saude-api:latest
container_name: gerocuidado-saude-api-prod
environment:
- DB_HOST=gerocuidado-saude-db
- DB_USERNAME=postgres
- DB_PASS=postgres
- DB_DATABASE=gerocuidado-saude-db
- DB_PORT=5003
ports:
- '3003:3003'
depends_on:
- gerocuidado-saude-db
networks:
- gerocuidado-saude-net

gerocuidado-saude-db:
build:
context: ./.docker/postgres
dockerfile: Dockerfile
command: postgres -c 'config_file=/etc/postgresql/postgresql.conf'
container_name: gerocuidado-saude-db
volumes:
- './.docker/postgres/config/postgresql.conf:/etc/postgresql/postgresql.conf'
environment:
- POSTGRES_DB=gerocuidado-saude-db
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5003:5003'
networks:
- gerocuidado-saude-net

networks:
gerocuidado-saude-net:
driver: bridge
5 changes: 3 additions & 2 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
version: '3.7'
services:
gerocuidado-saude-api-test:
image: node:18.13.0
image: gerocuidadodev/node:18.17.0-alpine-dev
container_name: gerocuidado-saude-api-test
volumes:
- '.:/home/node/app'
entrypoint: bash /home/node/app/.docker/entrypoint.test.sh
entrypoint: dockerize -wait tcp://gerocuidado-saude-db-test:5003 -timeout 40s ./.docker/entrypoint.test.sh
environment:
- NODE_ENV=test
- TEST=$TEST
Expand All @@ -22,6 +22,7 @@ services:
context: ./.docker/postgres
dockerfile: Dockerfile
command: postgres -c 'config_file=/etc/postgresql/postgresql.conf'
container_name: gerocuidado-saude-db-test
volumes:
- './.docker/postgres/config/postgresql.conf:/etc/postgresql/postgresql.conf'
environment:
Expand Down
Loading

0 comments on commit f5152f3

Please sign in to comment.