diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 5a3136ea..77eff8e7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -1,4 +1,4 @@ -name: Deploy +name: CD on: push: @@ -9,7 +9,6 @@ on: jobs: deploy: runs-on: ubuntu-latest - steps: - name: Checkout code uses: actions/checkout@v4 @@ -17,16 +16,9 @@ jobs: - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: - aws-access-key-id: ${{ secrets.ACCESS_KEY }} - aws-secret-access-key: ${{ secrets.SECRET_ACCESS_KEY }} - aws-region: us-east-1 - - - name: Retrieve AWS Secrets - uses: aws-actions/aws-secretsmanager-get-secrets@v1 - with: - secret-ids: | - staging/next-staging - # parse-json-secrets: true + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr @@ -40,14 +32,16 @@ jobs: REPOSITORY: ufabc-next-backend IMAGE_TAG: ${{ github.sha }} run: | + echo ${{ secrets.PRODUCTION_ENV }} > .env.production docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG . docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG - - name: Deploy to EC2 Instance + - name: Deploy and start container uses: appleboy/ssh-action@v1.0.0 with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_KEY }} + host: ${{ secrets.HOSTNAME }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.SECRET_SSH_KEY }} script: | - cat ~/.ssh/id_rsa + docker pull ${{ secrets.AWS_ACCOUNT_ID }}.${{ secrets.ECR_REGISTRY }}/ufabc-next-backend:${{ github.sha }} + docker run -d --name ufabc-next-backend -p ${{ secrets.PORT }}:${{ secrets.PORT }} ${{ secrets.AWS_ACCOUNT_ID }}.${{ secrets.ECR_REGISTRY }}/ufabc-next-backend:${{ github.sha }} diff --git a/Dockerfile b/Dockerfile index 307a59fc..0ef108d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ COPY pnpm*.yaml ./ # mount pnpm store as cache & fetch dependencies RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm-store \ - pnpm fetch + pnpm fetch --ignore-scripts FROM fetcher as builder # specify the app in apps/ we want to build @@ -25,16 +25,18 @@ ENV APP_NAME=${APP_NAME} WORKDIR /workspace COPY . . -RUN pnpm install --frozen-lockfile --offline --silent +RUN pnpm i --frozen-lockfile --offline --silent # build app + RUN --mount=type=cache,target=/workspace/node_modules/.cache \ pnpm turbo run build --filter="${APP_NAME}" # deploy app FROM builder as deployer WORKDIR /workspace -RUN pnpm --filter ${APP_NAME} deploy --prod ./out +RUN export NODE_ENV=prod +RUN pnpm --filter ${APP_NAME} deploy --prod --ignore-scripts ./out FROM runtime as runner WORKDIR /workspace diff --git a/apps/core/src/config/config.ts b/apps/core/src/config/config.ts index b5379556..3d49e73c 100644 --- a/apps/core/src/config/config.ts +++ b/apps/core/src/config/config.ts @@ -1,6 +1,7 @@ import { config as dotEnvConfig } from 'dotenv'; import { z } from 'zod'; +// eslint-disable-next-line if (process.env.NODE_ENV === 'test') { dotEnvConfig({ path: '.env.test' }); } else { diff --git a/packages/common/config.ts b/packages/common/config.ts deleted file mode 100644 index a722518a..00000000 --- a/packages/common/config.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { config as dotEnvConfig } from 'dotenv'; -import { z } from 'zod'; - -if (process.env.NODE_ENV === 'test') { - dotEnvConfig({ path: '.env.test' }); -} else { - dotEnvConfig(); -} - -const envSchema = z.object({ - // Local machine - NODE_ENV: z.enum(['dev', 'test', 'prod']).default('dev'), -}); - -const _env = envSchema.safeParse(process.env); - -if (!_env.success) { - console.error('invalid envs', _env.error.format()); - throw new Error('Invalid environments variables'); -} - -export type Config = z.infer; -export const Config = _env.data; diff --git a/packages/common/lib/logger.ts b/packages/common/lib/logger.ts index 844a1728..52fa80e6 100644 --- a/packages/common/lib/logger.ts +++ b/packages/common/lib/logger.ts @@ -1,6 +1,5 @@ import { pino } from 'pino'; import { PrettyOptions } from 'pino-pretty'; -import { Config } from '../config'; const pinoPrettyOptions = { destination: 1, @@ -20,7 +19,7 @@ const loggerSetup = { test: false, }; -function buildLogger(nodeEnv: Config['NODE_ENV']) { +function buildLogger(nodeEnv: string = 'dev') { if (nodeEnv === 'dev') { const logger = pino(loggerSetup.dev); return logger; @@ -30,4 +29,4 @@ function buildLogger(nodeEnv: Config['NODE_ENV']) { } } -export const logger = buildLogger(Config.NODE_ENV); +export const logger = buildLogger(process.env.NODE_ENV); diff --git a/packages/common/package.json b/packages/common/package.json index 6e7e578f..ffc20aa2 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -26,8 +26,6 @@ "typescript": "^5.2.2" }, "dependencies": { - "dotenv": "^16.3.1", - "pino": "^8.15.3", - "zod": "^3.22.2" + "pino": "^8.15.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bb13cdbd..04d2bddb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -200,15 +200,9 @@ importers: packages/common: dependencies: - dotenv: - specifier: ^16.3.1 - version: 16.3.1 pino: specifier: ^8.15.3 version: 8.15.3 - zod: - specifier: ^3.22.2 - version: 3.22.2 devDependencies: '@types/node': specifier: ^20.8.0