Skip to content

Commit

Permalink
feat: add deployment script
Browse files Browse the repository at this point in the history
  • Loading branch information
fr-ser committed Jul 24, 2024
1 parent 282b067 commit a2527d5
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ test-all:
cd backend && npm run test
cd frontend && npm run test
npm run test

#: Deploy the application to the raspberry pi
deploy: build test-all
ssh -p $${PI_SSH_PORT} pi@$${PI_SSH_ADDRESS} 'mkdir -p /home/pi/apps/next-scaffolding'
scp -P $${PI_SSH_PORT} -r ./backend/dist pi@$${PI_SSH_ADDRESS}:/home/pi/apps/next-scaffolding
scp -P $${PI_SSH_PORT} ./backend/package*.json pi@$${PI_SSH_ADDRESS}:/home/pi/apps/next-scaffolding
scp -P $${PI_SSH_PORT} -r ./deployment pi@$${PI_SSH_ADDRESS}:/home/pi/apps

@echo "To replace the old version you should run 'cd /home/pi/apps/deployment && make update' on the raspberry"
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ The backend and frontend have independent tests.
In the overall repo you can also run e2e and unit tests, though.

Run `make test-all` to run all available tests.

## Deployment

The application is manually deployed via the `make deploy` command.
In order to do this SSH access to the target environment is required.

These environment variables should be set:

- PI_SSH_PORT
- PI_SSH_ADDRESS
2 changes: 1 addition & 1 deletion backend/local.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_SECRET=local
PORT=3001
DROPBOX_ACCESS_TOKEN=local
STATIC_FILE_ROOT=../frontend/public

Expand Down
2 changes: 0 additions & 2 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function getRequired(envKey: string): string {
return process.env[envKey] as string;
}

export const APP_SECRET = getRequired("APP_SECRET"); // session secret

export const PORT = process.env.PORT || 3001;
export const STATIC_FILE_ROOT = process.env.STATIC_FILE_ROOT || "static";
export const DROPBOX_ACCESS_TOKEN = getRequired("DROPBOX_ACCESS_TOKEN");
Expand Down
6 changes: 1 addition & 5 deletions backend/src/helpers/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ export function timeoutCheck(

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function shouldLoggerSkip(req: express.Request, _: express.Response): boolean {
if (req.originalUrl.startsWith("/img")) return true;
else if (req.originalUrl.startsWith("/css")) return true;
else if (req.originalUrl.startsWith("/js")) return true;
else if (req.originalUrl.startsWith("/favicon")) return true;
else if (req.originalUrl.startsWith("/manifest.webmanifest")) return true;
if (req.originalUrl.startsWith("/assets")) return true;
else if (req.originalUrl.startsWith("/health")) return true;
else return false;
}
Expand Down
16 changes: 5 additions & 11 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "dotenv/config";
import express from "express";
import basicAuth from "express-basic-auth";
import morgan from "morgan";
import path from "path";
import "source-map-support/register";

import { STATIC_FILE_ROOT, USERS } from "@/config";
Expand All @@ -18,6 +17,11 @@ import { ordersRouter } from "@/routes/orders";
export function getApp() {
const app = express();

// this is a public endpoint (required for freshping)
app.get("/health", (_: express.Request, res: express.Response) => {
res.status(200).send("OK");
});

app.use(
basicAuth({
challenge: true,
Expand All @@ -44,16 +48,6 @@ export function getApp() {
app.use(bodyParser.text({ type: "text/plain", limit: "1mb" }));
app.use(bodyParser.text({ type: "text/html", limit: "3mb" }));
app.use(express.static(STATIC_FILE_ROOT));

app.get("/manifest.json", (_: express.Request, res: express.Response) => {
res.setHeader("Content-Type", "application/manifest+json");
res.sendFile(path.resolve("./manifest.webmanifest"));
});

app.get("/health", (_: express.Request, res: express.Response) => {
res.status(200).send("OK");
});

app.use("/api/clients", clientsRouter);
app.use("/api/orders", ordersRouter);
app.use("/api/articles", articlesRouter);
Expand Down
23 changes: 23 additions & 0 deletions deployment/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
update:
pm2 kill

cp -r /home/pi/apps/scaffolding/node_modules /home/pi/apps/next-scaffolding
cp -r /home/pi/apps/scaffolding/.env /home/pi/apps/next-scaffolding
cp /home/pi/apps/scaffolding/*.db /home/pi/apps/next-scaffolding
cd /home/pi/apps/next-scaffolding && npm install --omit=dev --no-audit

rm -rf /home/pi/apps/backup-scaffolding
mv /home/pi/apps/scaffolding /home/pi/apps/backup-scaffolding
mv /home/pi/apps/next-scaffolding /home/pi/apps/scaffolding

pm2 start pm2.config.js
pm2 save

revert:
rm -rf /home/pi/apps/next-scaffolding
mv /home/pi/apps/scaffolding /home/pi/apps/next-scaffolding
mv /home/pi/apps/backup-scaffolding /home/pi/apps/scaffolding

pm2 kill
pm2 start pm2.config.js
pm2 save
11 changes: 11 additions & 0 deletions deployment/pm2.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
apps: [
{
name: "scaffolding",
script: "./dist/src/index.js",
cwd: "/home/pi/apps/scaffolding/",
error_file: "/home/pi/apps/scaffolding/app.log",
out_file: "/home/pi/apps/scaffolding/app.log",
},
],
};

0 comments on commit a2527d5

Please sign in to comment.