Skip to content

Commit

Permalink
Github actions to create dist in server
Browse files Browse the repository at this point in the history
  • Loading branch information
karlosvas committed Jan 14, 2025
1 parent fb18f19 commit 90d9bc0
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 108 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Deploy to Vercel

on:
push:
branches:
- main

jobs:
deploy-frontend:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "21.2.0"

- name: Install Vercel CLI
run: npm install -g vercel@latest

- name: List files in client directory
run: ls -la ./client

- name: Link and pull Vercel project for frontend
run: |
vercel link --yes --project ${{ secrets.FRONTEND_PROJECT_ID }} --scope ${{ secrets.VERCEL_TEAM }} --token=${{ secrets.VERCEL_TOKEN }}
vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
working-directory: ./client

- name: Install frontend dependencies
run: npm install
working-directory: ./client

- name: Build frontend
run: npm run build
working-directory: ./client

- name: Deploy frontend to Vercel
run: vercel --token ${{ secrets.VERCEL_TOKEN }}
working-directory: ./client

deploy-backend:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "21.2.0"

- name: Install Vercel CLI
run: npm install -g vercel@latest

- name: List files in server directory
run: ls -la ./server

- name: Link and pull Vercel project for backend
run: |
vercel link --yes --project ${{ secrets.BACKEND_PROJECT_ID }} --scope ${{ secrets.VERCEL_TEAM }} --token=${{ secrets.VERCEL_TOKEN }}
vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
working-directory: ./server

- name: Install backend dependencies
run: npm install
working-directory: ./server

- name: Build backend
run: npm run build
working-directory: ./server

- name: List files in server directory after build
run: ls -la ./server/public

- name: Deploy backend to Vercel
run: vercel --token ${{ secrets.VERCEL_TOKEN }}
working-directory: ./server
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dev
dist-ssr
client/dist
*.local
dist

# Editor directories and files
.vscode/*
Expand Down
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vercel
7 changes: 0 additions & 7 deletions client/vercel.json

This file was deleted.

18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"type": "GNU",
"url": "https://www.gnu.org/licenses/gpl-3.0.html"
}
]
],
"dependencies": {
"prettier": "^3.4.2"
}
}
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vercel
2 changes: 1 addition & 1 deletion server/lib/resend/resend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from "dotenv";
dotenv.config();
////////////////////////////////////////////////////////////////
import { Message, NoteType, SubscriberType } from "types/types";
import { NoteType, SubscriberType } from "types/types";
import { Resend } from "resend";

// Configurar el cliente de Mandrill
Expand Down
25 changes: 0 additions & 25 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
"type": "module",
"main": "index.ts",
"scripts": {
"start": "npm run build && node dist/index.js",
"start": "node public/src/index.js",
"dev": "node --loader ts-node/esm src/index.js",
"test": "npm run build && playwright test",
"build": "tsc"
},
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@vercel/node": "^2.10.3",
"axios": "^1.7.7",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
Expand Down
34 changes: 0 additions & 34 deletions server/routes/resend.ts

This file was deleted.

7 changes: 6 additions & 1 deletion server/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ readdirSync(PATH_ROUTER).forEach((file) => {
const fileClean = cleanExtension(file);
const fileExtension = path.extname(file);

if (fileClean !== "routes" && (fileExtension === ".js" || fileExtension === ".ts") && !file.endsWith(".map")) {
if (
fileClean !== "routes" &&
(fileExtension === ".js" || fileExtension === ".ts") &&
!file.endsWith(".js.map") &&
!file.endsWith(".ts.map")
) {
const modulePath = path.join(PATH_ROUTER, `${fileClean}${fileExtension}`);
const moduleURL = pathToFileURL(modulePath).href;

Expand Down
56 changes: 30 additions & 26 deletions server/index.ts → server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dotenv from "dotenv";
dotenv.config();
///////////////////////////////////////////
import { connectDB } from "./src/mongodb/mongodb.js";
import { connectDB } from "./mongodb/mongodb.js";
import express from "express";
import cors from "cors";
import { router } from "./routes/routes.js";
import { router } from "../routes/routes.js";
import net from "net";
import admin from "./lib/firebase/firebase-config.js";
import admin from "../lib/firebase/firebase-config.js";

// Extendemos el límite para que pueda almacenar imagenes en base64
async function inicializeApp() {
Expand Down Expand Up @@ -60,24 +60,26 @@ async function inicializeApp() {
}
});

// Si es development y preview asignamos el puerto disponible a partir de 8080
if (process.env.NODE_ENV !== "production") {
const PORT_BACKEND = 8080;
// Creamos una funcion flecha que devuelbe una promesa
return app;
}

async function startServer() {
try {
const app = await inicializeApp();
const PORT_BACKEND = parseInt(process.env.PORT || "8080");

// Funcion para chekear si esta ocupado
const checkPort = (port: number): Promise<boolean> => {
return new Promise((resolve, reject) => {
// Creamos un servidor con net nativo de (NodeJS)
// He intentamos escuchar en el puerto
const server = net.createServer();
// Intentar escuchar en el puerto
server.listen(port);

// Verificamos si el puerto esta en uso si lo esta devuleve false, si ocurre un error lo rechaza
server.once("error", (err: any) => {
if (err.code === "EADDRINUSE") {
resolve(false);
} else {
reject(err);
}
if (err.code === "EADDRINUSE") resolve(false);
else reject(err);
});
// Si el puerto esta libre lo cerramos y resolvemos la promesa
server.once("listening", () => {
Expand All @@ -87,35 +89,37 @@ async function inicializeApp() {
});
};

const startServer = async (port: number) => {
// Encontramos un puerto libre de [8080, 8090]
// Función para cambiar el puerto si esta ocupado
const changePort = async (port: number) => {
// Puertos libres de [8080, 8090]
if (port > 8090) {
console.log("No ports available");
return;
}

// Encontra un puerto libre y se lo asigna al servidor, si no encuentra uno libre lo asigna al siguiente
const isPortFree = await checkPort(port);

// Si el puerto esta libre lo asigna al servidor
if (isPortFree) {
app.listen(port, () => {
console.log(`Server running: http://localhost:${port}`);
});
} else {
console.log(`Port ${port} is in use, please choose another port.`);
startServer(port + 1);
console.log(`Port ${port} is in use, please choose another port`);
changePort(port + 1);
}
};

startServer(PORT_BACKEND).catch((error) => {
changePort(PORT_BACKEND).catch((error) => {
console.error("Error starting the server:", error);
});
} else {
const PORT_BACKEND = 8080;
app.listen(PORT_BACKEND, () => {
console.log(`Server runing: http://localhost:${PORT_BACKEND}`);
});
} catch (error) {
console.error("Error starting the server:", error);
process.exit(1);
}
}

inicializeApp().catch((error) => {
console.error("Error starting the server:", error);
});
if (process.env.NODE_ENV !== "production") startServer();

export default inicializeApp;
4 changes: 2 additions & 2 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"rootDir": "./" /* Specify the root folder within your source files. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
"outDir": "dist" /* Specify an output folder for all emitted files. */,
"outDir": "public" /* Specify an output folder for all emitted files. */,
"paths": {
"types/*": ["types/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
Expand All @@ -20,5 +20,5 @@
"esm": true
} /* Enable all strict type-checking options. */,
"include": ["./**/*.ts", "types"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "public"]
}
Loading

0 comments on commit 90d9bc0

Please sign in to comment.