From f71f45eb82fe5f3157a9387c76b444e29354f5d3 Mon Sep 17 00:00:00 2001 From: Matheusafonsouza Date: Thu, 9 Jan 2025 20:46:03 -0300 Subject: [PATCH 1/3] feat: cd docker deployment --- .eslintignore | 1 + .github/workflows/cd.yml | 22 +++++++++++++++++++ .github/workflows/ci.yml | 8 ++++--- src/auth/roles.decorator.ts | 2 +- src/auth/roles.guard.ts | 7 ++++-- .../migrations/1736291880582-create-users.ts | 19 ++++++++-------- src/users/users.controller.ts | 6 ++--- 7 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/cd.yml diff --git a/.eslintignore b/.eslintignore index a9ba028..20b299b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ .eslintrc.js +src/database/migrations/ diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..8f83d20 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,22 @@ +name: CD +on: + push: + branches: + - main +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Build, Push and Release a Docker container to Heroku. + uses: gonuit/heroku-docker-deploy@v1.3.3 + with: + email: ${{ secrets.LIVRO_LIVRE_HEROKU_EMAIL }} + heroku_api_key: ${{ secrets.LIVRO_LIVRE_HEROKU_API_KEY }} + heroku_app_name: "livro-livre-users" + dockerfile_directory: ./ + dockerfile_name: Dockerfile + docker_options: "--no-cache" + process_type: web diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7c483d..5f770a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,8 +6,8 @@ on: pull_request: types: [opened, synchronize, reopened] jobs: - sonarcloud: - name: Lint + run: + name: CI runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -15,5 +15,7 @@ jobs: fetch-depth: 0 - name: Install dependencies run: npm install - - name: Test and coverage + - name: Run lint run: npm run lint + - name: Run tests + run: npm run test diff --git a/src/auth/roles.decorator.ts b/src/auth/roles.decorator.ts index c0c0e4f..7360f2a 100644 --- a/src/auth/roles.decorator.ts +++ b/src/auth/roles.decorator.ts @@ -1,4 +1,4 @@ import { SetMetadata } from '@nestjs/common'; -import { UserRoles } from 'src/database/entities/user.entity'; +import { UserRoles } from '../database/entities/user.entity'; export const Roles = (...roles: UserRoles[]) => SetMetadata('roles', roles); diff --git a/src/auth/roles.guard.ts b/src/auth/roles.guard.ts index d31bd9c..8ee5ebd 100644 --- a/src/auth/roles.guard.ts +++ b/src/auth/roles.guard.ts @@ -1,13 +1,16 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; -import { UserRoles } from 'src/database/entities/user.entity'; +import { UserRoles } from '../database/entities/user.entity'; @Injectable() export class RolesGuard implements CanActivate { constructor(private reflector: Reflector) {} canActivate(context: ExecutionContext): boolean { - const requiredRoles = this.reflector.get('roles', context.getHandler()); + const requiredRoles = this.reflector.get( + 'roles', + context.getHandler(), + ); if (!requiredRoles) { return true; } diff --git a/src/database/migrations/1736291880582-create-users.ts b/src/database/migrations/1736291880582-create-users.ts index f8132d0..f752f73 100644 --- a/src/database/migrations/1736291880582-create-users.ts +++ b/src/database/migrations/1736291880582-create-users.ts @@ -1,14 +1,15 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; +import { MigrationInterface, QueryRunner } from 'typeorm'; export class CreateUsers1736291880582 implements MigrationInterface { - name = 'CreateUsers1736291880582' + name = 'CreateUsers1736291880582'; - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "firstName" character varying NOT NULL, "lastName" character varying NOT NULL, "password" character varying NOT NULL, "email" character varying NOT NULL, "phone" character varying NOT NULL, "role" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DROP TABLE "user"`); - } + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "firstName" character varying NOT NULL, "lastName" character varying NOT NULL, "password" character varying NOT NULL, "email" character varying NOT NULL, "phone" character varying NOT NULL, "role" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`, + ); + } + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "user"`); + } } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 73a990c..03b48eb 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -11,9 +11,9 @@ import { } from '@nestjs/common'; import { UpdateUserDto } from './dtos/updateUser.dto'; import { AuthGuard } from '../auth/auth.guard'; -import { Roles } from 'src/auth/roles.decorator'; -import { UserRoles } from 'src/database/entities/user.entity'; -import { RolesGuard } from 'src/auth/roles.guard'; +import { Roles } from '../auth/roles.decorator'; +import { UserRoles } from '../database/entities/user.entity'; +import { RolesGuard } from '../auth/roles.guard'; @Controller('users') export class UsersController { From 40b78a08b02b4c1bda2c5739c5e5821496568110 Mon Sep 17 00:00:00 2001 From: Matheusafonsouza Date: Thu, 9 Jan 2025 21:27:03 -0300 Subject: [PATCH 2/3] feat: fix cd --- .github/workflows/cd.yml | 23 ++++++++++------------- Dockerfile | 1 + Procfile | 1 - heroku.yml | 3 +++ 4 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 Procfile create mode 100644 heroku.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 8f83d20..f90d75c 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -5,18 +5,15 @@ on: - main jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-latest # Setup an environment to run the action steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Build, Push and Release a Docker container to Heroku. - uses: gonuit/heroku-docker-deploy@v1.3.3 + - uses: actions/checkout@v2 # This actions copy the repository on the environment + - name: Install Heroku CLI + run: | + curl https://cli-assets.heroku.com/install.sh | sh + - uses: akhileshns/heroku-deploy@v3.12.12 # This action deploys the content on Heroku with: - email: ${{ secrets.LIVRO_LIVRE_HEROKU_EMAIL }} - heroku_api_key: ${{ secrets.LIVRO_LIVRE_HEROKU_API_KEY }} - heroku_app_name: "livro-livre-users" - dockerfile_directory: ./ - dockerfile_name: Dockerfile - docker_options: "--no-cache" - process_type: web + heroku_api_key: ${{secrets.LIVRO_LIVRE_HEROKU_API_KEY}} #The Heroku api key we stored on our repo secret + heroku_app_name: "livro-livre-users" #The name of your heroku app - Must be unique in Heroku + heroku_email: "livrolivreeng@gmail.com" #Your heroku yuser name + usedocker: true \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index cd5a43f..d503747 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,7 @@ COPY . . RUN npm run build # Expose the port the app will listen on +EXPOSE 80 EXPOSE 3000 # Define the command to start the app diff --git a/Procfile b/Procfile deleted file mode 100644 index e6bad1f..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: npm run start:prod \ No newline at end of file diff --git a/heroku.yml b/heroku.yml new file mode 100644 index 0000000..8eec25b --- /dev/null +++ b/heroku.yml @@ -0,0 +1,3 @@ +build: + docker: + web: Dockerfile From fca369f3bfde476c0b533b532bebfa435cc65a27 Mon Sep 17 00:00:00 2001 From: Matheusafonsouza Date: Thu, 9 Jan 2025 22:09:20 -0300 Subject: [PATCH 3/3] chore: migrations --- .github/workflows/cd.yml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index f90d75c..58ee218 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -4,7 +4,26 @@ on: branches: - main jobs: + migrate: + runs-on: ubuntu-latest + env: + DB_PORT: ${{ secrets.LIVRO_LIVRE_DB_PORT }} + DB_HOST: ${{ secrets.LIVRO_LIVRE_DB_HOST }} + DB_USER: ${{ secrets.LIVRO_LIVRE_DB_USER }} + DB_PASSWORD: ${{ secrets.LIVRO_LIVRE_DB_PASSWORD }} + DB_NAME: ${{ secrets.LIVRO_LIVRE_DB_NAME }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Install dependencies + run: | + npm install + - name: Run migrations + run: | + npm run migration:run + build: + needs: migrate runs-on: ubuntu-latest # Setup an environment to run the action steps: - uses: actions/checkout@v2 # This actions copy the repository on the environment @@ -16,4 +35,4 @@ jobs: heroku_api_key: ${{secrets.LIVRO_LIVRE_HEROKU_API_KEY}} #The Heroku api key we stored on our repo secret heroku_app_name: "livro-livre-users" #The name of your heroku app - Must be unique in Heroku heroku_email: "livrolivreeng@gmail.com" #Your heroku yuser name - usedocker: true \ No newline at end of file + usedocker: true