From e200ed7d7f4cae8d7343e31960ee09210c8f59f9 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:11:58 +0200 Subject: [PATCH 1/6] Add dockerfile --- Dockerfile | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index dfe1cdd..c32c6b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,46 @@ -FROM node:18-alpine +# syntax=docker/dockerfile:1 -RUN apk add --no-cache python3 make gcc g++ -WORKDIR /app +ARG app_dir="/home/node/app" -COPY "docker/entrypoint.sh" ./ -COPY package*.json ./ -RUN npm install +# * Base Node.js image +FROM node:20-alpine AS base +ARG app_dir +WORKDIR ${app_dir} -COPY . ./ -VOLUME [ "/app/config.json", "/app/db.json" ] +# * Installing production dependencies +FROM base AS dependencies -CMD ["sh", "entrypoint.sh"] +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=package-lock.json,target=package-lock.json \ + --mount=type=cache,target=/root/.npm \ + npm ci --omit=dev + + +# * Installing development dependencies and building the application +FROM base AS build + +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=package-lock.json,target=package-lock.json \ + --mount=type=cache,target=/root/.npm \ + npm ci + +COPY . . + + +# * Running the final application +FROM base AS final +ARG app_dir + +ENV NODE_ENV=production +USER node + +COPY package.json . + +COPY --from=build ${app_dir} ${app_dir} +COPY --from=dependencies ${app_dir}/node_modules ${app_dir}/node_modules + +VOLUME ["./config.json", "./database.db"] + +CMD ["node", "./src/bot"] From fa49d09d7409dab5f7bd58ef4f9b806eedf8d8e9 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:12:17 +0200 Subject: [PATCH 2/6] Add option to change db storage path --- config.example.json | 1 + src/database.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/config.example.json b/config.example.json index 0670cac..08f32bc 100644 --- a/config.example.json +++ b/config.example.json @@ -2,6 +2,7 @@ "bot_token": "BOT_TOKEN", "application_id": "BOT_APP_ID", "public_key": "BOT_PUBLIC_KEY", + "db_path": "database.db", "nlp": { "force_train": true, "manager": { diff --git a/src/database.js b/src/database.js index b8a30ac..e5ab05a 100644 --- a/src/database.js +++ b/src/database.js @@ -1,12 +1,13 @@ const path = require('path'); const sqlite3 = require('sqlite3'); const sqlite = require('sqlite'); +const { db_path } = require('../config.json'); let database; async function connect() { database = await sqlite.open({ - filename: path.join(__dirname, '../database.db'), + filename: path.join(__dirname, '..', db_path ?? 'database.db'), driver: sqlite3.Database }); From a41022c424e9e06f33e26afc92133a7af48b9a2e Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:13:12 +0200 Subject: [PATCH 3/6] Add docker github action --- .github/workflows/docker.yml | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..d3d40b6 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,56 @@ +name: Build and Publish Docker Image + +on: + push: + pull_request: + workflow_dispatch: + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-publish: + env: + SHOULD_PUSH_IMAGE: ${{ (github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')) || github.event_name == 'workflow_dispatch' }} + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Set up QEMU for Docker + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log into the container registry + if: ${{ env.SHOULD_PUSH_IMAGE == 'true' }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }} + type=raw,value=edge,enable=${{ github.ref == 'refs/heads/dev' }} + type=sha + + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v6 + with: + platforms: linux/amd64,linux/arm64 + push: ${{ env.SHOULD_PUSH_IMAGE }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max From 675c34099f731cb5054e69319501399f175ce9e6 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:15:36 +0200 Subject: [PATCH 4/6] Add docker dependencies for node-gyp --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c32c6b2..77d33ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ ARG app_dir="/home/node/app" FROM node:20-alpine AS base ARG app_dir WORKDIR ${app_dir} - +RUN apk add --no-cache python3 make gcc g++ # * Installing production dependencies FROM base AS dependencies From 69f69dfae8d702c3989d1379a75983133aa961f6 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:23:04 +0200 Subject: [PATCH 5/6] Add pip to dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 77d33ba..5356808 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ ARG app_dir="/home/node/app" FROM node:20-alpine AS base ARG app_dir WORKDIR ${app_dir} -RUN apk add --no-cache python3 make gcc g++ +RUN apk add --no-cache python3 py3-pip make gcc g++ # * Installing production dependencies FROM base AS dependencies From 33ac0bc43e5d650b9805c3052ca1c0ec7f1b4cfd Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 12 Aug 2024 22:29:43 +0200 Subject: [PATCH 6/6] Make db directory if not exist --- src/database.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/database.js b/src/database.js index e5ab05a..4411300 100644 --- a/src/database.js +++ b/src/database.js @@ -1,13 +1,18 @@ const path = require('path'); +const fs = require('fs'); const sqlite3 = require('sqlite3'); const sqlite = require('sqlite'); const { db_path } = require('../config.json'); let database; +const resolvedDbPath = db_path ?? path.join(__dirname, '../database.db'); +const dbFolder = path.dirname(resolvedDbPath); +fs.mkdirSync(dbFolder, { recursive: true }); + async function connect() { database = await sqlite.open({ - filename: path.join(__dirname, '..', db_path ?? 'database.db'), + filename: resolvedDbPath, driver: sqlite3.Database });