Skip to content

Commit

Permalink
ci(backend): ajout du déploiement et de la configuration sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
dthib committed Jul 22, 2024
1 parent 2987c2e commit f29e864
Show file tree
Hide file tree
Showing 7 changed files with 939 additions and 5 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/cd-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,29 @@ jobs:
--platform=linux/amd64
+backend-build
--ENV_NAME=${{ github.event.inputs.target }}
deploy-backend:
name: Déploie le backend
needs: build-backend
environment: ${{ github.event.inputs.target }}
runs-on: ubuntu-latest
timeout-minutes: 20

steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- uses: earthly/actions/setup-earthly@v1
with:
version: v0.7.20
- name: Login into registry
run: earthly --use-inline-cache +docker-dev-login --GH_USER=${{ secrets.GH_USER }} --GH_TOKEN=${{ secrets.GH_TOKEN }}

- name: Deploy app on Koyeb
run: >
earthly
--use-inline-cache
+backend-deploy
--ENV_NAME=${{ github.event.inputs.target }}
--KOYEB_API_KEY=${{ secrets.KOYEB_API_KEY }}
6 changes: 6 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ARG --global DL_TAG=$ENV_NAME-$(sh ./subdirs_hash.sh data_layer)
ARG --global DB_SAVE_IMG_NAME=$REG_TARGET/db-save:$DL_TAG
ARG --global DB_VOLUME_NAME=supabase_db_tet
ARG --global GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
ARG --global GIT_COMMIT_SHORT_SHA=$(git rev-parse --short HEAD)

postgres:
FROM postgres:15
Expand Down Expand Up @@ -318,6 +319,11 @@ backend-build:
CMD ["node", "backend/dist/main.js"]
SAVE IMAGE --cache-from=$BACKEND_IMG_NAME --push $BACKEND_IMG_NAME

backend-deploy: ## Déploie le backend dans une app Koyeb existante
ARG --required KOYEB_API_KEY
FROM +koyeb
RUN ./koyeb services update $ENV_NAME-backend/backend --docker $BACKEND_IMG_NAME --env GIT_COMMIT_SHORT_SHA=$GIT_COMMIT_SHORT_SHA

app-build: ## construit l'image de l'app
ARG PLATFORM
ARG --required ANON_KEY
Expand Down
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@sentry/nestjs": "^8.19.0",
"@sentry/profiling-node": "^8.19.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand Down Expand Up @@ -66,4 +68,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
7 changes: 6 additions & 1 deletion backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, NotFoundException } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
Expand All @@ -9,4 +9,9 @@ export class AppController {
getHello(): string {
return this.appService.getHello();
}

@Get('exception')
getException(): string {
throw new NotFoundException('User not found');
}
}
21 changes: 21 additions & 0 deletions backend/src/common/configs/SentryInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Sentry from '@sentry/nestjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';

export const SENTRY_DSN = process.env.SENTRY_DSN || '';

// Ensure to call this before requiring any other modules!
Sentry.init({
dsn: SENTRY_DSN,
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],

// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,

// Set sampling rate for profiling
// This is relative to tracesSampleRate
profilesSampleRate: 1.0,
});
17 changes: 16 additions & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { NestFactory } from '@nestjs/core';
// WARNING: Do this import first
import './common/configs/SentryInit';
import * as Sentry from '@sentry/nestjs';
import {
BaseExceptionFilter,
HttpAdapterHost,
NestFactory,
} from '@nestjs/core';
import { AppModule } from './app.module';
import { SENTRY_DSN } from './common/configs/SentryInit';

const port = process.env.PORT || 8080;
console.log(`Launching NestJS app on port ${port}`);

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const { httpAdapter } = app.get(HttpAdapterHost);

if (SENTRY_DSN) {
console.log('Sentry enabled with DSN:', SENTRY_DSN);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
}

await app.listen(port);
}
bootstrap();
Loading

0 comments on commit f29e864

Please sign in to comment.