From f8ee051964aeea500b8746348f89ecb50b17712f Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 30 Jun 2024 02:47:32 +0200 Subject: [PATCH 01/10] Create Docker service to serve the development and production. --- .docker/Dockerfile_dependencies | 14 ++++++++++++++ .docker/Dockerfile_develop | 11 +++++++++++ .docker/Dockerfile_production | 27 +++++++++++++++++++++++++++ README.md | 10 ++++++++++ docker-compose.yaml | 29 +++++++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .docker/Dockerfile_dependencies create mode 100644 .docker/Dockerfile_develop create mode 100644 .docker/Dockerfile_production create mode 100644 docker-compose.yaml diff --git a/.docker/Dockerfile_dependencies b/.docker/Dockerfile_dependencies new file mode 100644 index 000000000..fd8011b0a --- /dev/null +++ b/.docker/Dockerfile_dependencies @@ -0,0 +1,14 @@ +# Base image +FROM node:18 + +# Set the working directory in the container +WORKDIR /app + +COPY ../package.json ./ + +# Install app dependencies +RUN npm install -g pnpm + +RUN pnpm install + + diff --git a/.docker/Dockerfile_develop b/.docker/Dockerfile_develop new file mode 100644 index 000000000..ca7998f50 --- /dev/null +++ b/.docker/Dockerfile_develop @@ -0,0 +1,11 @@ +# Base image +FROM node:18 + +RUN npm install -g pnpm + +# Set the working directory in the container +WORKDIR /app + +COPY .. . + +EXPOSE 8080 diff --git a/.docker/Dockerfile_production b/.docker/Dockerfile_production new file mode 100644 index 000000000..10dacf400 --- /dev/null +++ b/.docker/Dockerfile_production @@ -0,0 +1,27 @@ +# Base image +FROM node:18 + +# Set the working directory in the container +WORKDIR /app + +# copy src +COPY .. . + +# install pnpm +RUN npm install -g pnpm + +# install initial depenancies +RUN pnpm install + +# install production depencancies +RUN pnpm install --production + +# build +RUN node build + + +#pull nginx as webser and load balancer +FROM nginx:stable-alpine3.19-slim + +# copy the build files to nginx entry point +COPY ./public /usr/share/nginx/html diff --git a/README.md b/README.md index 37d6366d3..5354871f7 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,16 @@ Open http://localhost:8080/ in your browser. Run `node build` to build the minimized production version of the app. Copy `public` folder contents to your web server. +### Running in docker + +#### Developing: +* Install dependencies `docker-compose up tweb.dependencies`. +* Run develop container `docker-compose up tweb.develop `. +* Open http://localhost:8080/ in your browser. + +#### Production: +* Run `docker-compose up tweb.production -d` nginx image and container to serve the build +* Open http://localhost:80/ in your browser. ### Dependencies * [BigInteger.js](https://github.com/peterolson/BigInteger.js) ([Unlicense](https://github.com/peterolson/BigInteger.js/blob/master/LICENSE)) diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 000000000..5b2dd56c5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,29 @@ +version: "3.8" +services: + tweb.dependencies: + container_name: tweb.dependencies + build: + context: . + dockerfile: .docker/Dockerfile_dependencies + volumes: + - "./node_modules:/app/node_modules" + command: pnpm install + + tweb.develop: + container_name: tweb.develop + build: + context: . + dockerfile: .docker/Dockerfile_develop + volumes: + - "./:/app" + ports: + - 8080:8080 + command: pnpm start + + tweb.production: + container_name: tweb.production + build: + context: . + dockerfile: .docker/Dockerfile_production + ports: + - 80:80 diff --git a/package.json b/package.json index b200868ce..1dda2142b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "preinstall": "npx only-allow pnpm", - "start": "vite --force", + "start": "vite --force --host", "serve": "pnpm run build; node server.js", "build": "pnpm run generate-changelog && vite build", "test": "vitest", From 877ff1fa635156e56193090142767d4c05782b59 Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal <10581212+Moustafa-Elgammal@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:58:53 +0200 Subject: [PATCH 02/10] Update README.md : Start use docker --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 5354871f7..690589f6c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,20 @@ Run `node build` to build the minimized production version of the app. Copy `pub * Run `docker-compose up tweb.production -d` nginx image and container to serve the build * Open http://localhost:80/ in your browser. + +I also created an image https://hub.docker.com/r/elgammalx/tweb/tags based on Nginx ready to be deployed. + +You can use `docker build -f ./.docker/Dockerfile_production -t {dockerhub-username}/{imageName}:{latest} .` to build your production ready image. + +My build use in `docker-compose.yaml` file +```yaml +services: + tweb.production: + image: elgammalx/tweb + ports: + - 80:80 +``` + ### Dependencies * [BigInteger.js](https://github.com/peterolson/BigInteger.js) ([Unlicense](https://github.com/peterolson/BigInteger.js/blob/master/LICENSE)) * [pako](https://github.com/nodeca/pako) ([MIT License](https://github.com/nodeca/pako/blob/master/LICENSE)) From d5dc9f3f210da77e1936d62a01912e893fd828e6 Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal <10581212+Moustafa-Elgammal@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:59:32 +0200 Subject: [PATCH 03/10] Create SECURITY.md --- SECURITY.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..034e84803 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,21 @@ +# Security Policy + +## Supported Versions + +Use this section to tell people about which versions of your project are +currently being supported with security updates. + +| Version | Supported | +| ------- | ------------------ | +| 5.1.x | :white_check_mark: | +| 5.0.x | :x: | +| 4.0.x | :white_check_mark: | +| < 4.0 | :x: | + +## Reporting a Vulnerability + +Use this section to tell people how to report a vulnerability. + +Tell them where to go, how often they can expect to get an update on a +reported vulnerability, what to expect if the vulnerability is accepted or +declined, etc. From 8bfac4a9e6a7e087631701041b3b45cead00e34d Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 15:32:09 +0200 Subject: [PATCH 04/10] ci to build docker image --- .env | 1 + .github/workflows/production-image.yml | 47 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 .github/workflows/production-image.yml diff --git a/.env b/.env index fa74348cb..056f0818b 100644 --- a/.env +++ b/.env @@ -11,3 +11,4 @@ VITE_MTPROTO_AUTO=1 VITE_MTPROTO_HAS_HTTP=1 VITE_MTPROTO_HAS_WS=1 VITE_SAFARI_PROXY_WEBSOCKET= +PRODUCTION_BUILD_VERSION=2.1.1 diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml new file mode 100644 index 000000000..b4bfa69a4 --- /dev/null +++ b/.github/workflows/production-image.yml @@ -0,0 +1,47 @@ +name: Docker Image Build CI + +on: + push: + tags: + - 'tag/build-docker-image/*' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: Read version from .env + id: env + run: echo "::set-output name=VERSION::$(grep ^VERSION= .env | cut -d'=' -f2)" + + - name: Build the Docker image for latest + run: docker build -f ./.docker/Dockerfile_production -t ${{ secrets.DOCKERHUB_USERNAME }}/tweb:latest . + + - name: Build the Docker image for specific version + run: | + version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }} + docker build -f ./.docker/Dockerfile_production -t ${{ secrets.DOCKERHUB_USERNAME }}/tweb:${version} . + + - name: Login to Docker Hub + run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin + + - name: Push versioned image + run: | + version=${{ steps.env.outputs.VERSION }} + docker push ${{ secrets.DOCKERHUB_USERNAME }}/tweb:${version} + + - name: Push latest image + + run: | + docker push ${{ secrets.DOCKERHUB_USERNAME }}/tweb:latest From e0e3f4a670aeee9aa593683109f621895375586a Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 15:49:58 +0200 Subject: [PATCH 05/10] load username from github variable env not secrets --- .github/workflows/production-image.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml index b4bfa69a4..1238990bd 100644 --- a/.github/workflows/production-image.yml +++ b/.github/workflows/production-image.yml @@ -26,22 +26,22 @@ jobs: run: echo "::set-output name=VERSION::$(grep ^VERSION= .env | cut -d'=' -f2)" - name: Build the Docker image for latest - run: docker build -f ./.docker/Dockerfile_production -t ${{ secrets.DOCKERHUB_USERNAME }}/tweb:latest . + run: docker build -f ./.docker/Dockerfile_production -t ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest . - name: Build the Docker image for specific version run: | version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }} - docker build -f ./.docker/Dockerfile_production -t ${{ secrets.DOCKERHUB_USERNAME }}/tweb:${version} . + docker build -f ./.docker/Dockerfile_production -t ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version} . - name: Login to Docker Hub - run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin + run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ vars.DOCKERHUB_USERNAME}}" --password-stdin - name: Push versioned image run: | version=${{ steps.env.outputs.VERSION }} - docker push ${{ secrets.DOCKERHUB_USERNAME }}/tweb:${version} + docker push $DOCKERHUB_USERNAME/tweb:${version} - name: Push latest image run: | - docker push ${{ secrets.DOCKERHUB_USERNAME }}/tweb:latest + docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest From 700e32228b0525daa15a584fc23685b58887b5ba Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 16:17:00 +0200 Subject: [PATCH 06/10] fix reading version name --- .github/workflows/production-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml index 1238990bd..1ed2f5f25 100644 --- a/.github/workflows/production-image.yml +++ b/.github/workflows/production-image.yml @@ -23,7 +23,7 @@ jobs: - name: Read version from .env id: env - run: echo "::set-output name=VERSION::$(grep ^VERSION= .env | cut -d'=' -f2)" + run: echo "::set-output name=PRODUCTION_BUILD_VERSION::$(grep ^PRODUCTION_BUILD_VERSION= .env | cut -d'=' -f2)" - name: Build the Docker image for latest run: docker build -f ./.docker/Dockerfile_production -t ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest . From 0fb8cb6e5f7d3a02336fc0590327b258d46757eb Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 16:19:03 +0200 Subject: [PATCH 07/10] fix username --- .github/workflows/production-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml index 1ed2f5f25..ea702e8d5 100644 --- a/.github/workflows/production-image.yml +++ b/.github/workflows/production-image.yml @@ -39,7 +39,7 @@ jobs: - name: Push versioned image run: | version=${{ steps.env.outputs.VERSION }} - docker push $DOCKERHUB_USERNAME/tweb:${version} + docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version} - name: Push latest image From 65b44bc26d00f44abb0e70d3372ed9c780f7f9ca Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 16:23:58 +0200 Subject: [PATCH 08/10] fix version --- .github/workflows/production-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml index ea702e8d5..e1b977a29 100644 --- a/.github/workflows/production-image.yml +++ b/.github/workflows/production-image.yml @@ -38,7 +38,7 @@ jobs: - name: Push versioned image run: | - version=${{ steps.env.outputs.VERSION }} + version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }} docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version} - name: Push latest image From 0175ec9551e7e955d58917b90cfd93412d426b95 Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 28 Jul 2024 16:30:09 +0200 Subject: [PATCH 09/10] test build images --- .env | 2 +- .github/workflows/production-image.yml | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 056f0818b..0e1ef62ca 100644 --- a/.env +++ b/.env @@ -11,4 +11,4 @@ VITE_MTPROTO_AUTO=1 VITE_MTPROTO_HAS_HTTP=1 VITE_MTPROTO_HAS_WS=1 VITE_SAFARI_PROXY_WEBSOCKET= -PRODUCTION_BUILD_VERSION=2.1.1 +PRODUCTION_BUILD_VERSION=2.1.2 diff --git a/.github/workflows/production-image.yml b/.github/workflows/production-image.yml index e1b977a29..ab1ea09f5 100644 --- a/.github/workflows/production-image.yml +++ b/.github/workflows/production-image.yml @@ -45,3 +45,24 @@ jobs: run: | docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest + + + test: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Read version from .env + id: env + run: echo "::set-output name=PRODUCTION_BUILD_VERSION::$(grep ^PRODUCTION_BUILD_VERSION= .env | cut -d'=' -f2)" + + - name: Pull versioned image + run: | + version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }} + docker pull ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version} + + - name: Pull latest image + run: docker pull ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest From 08e1b9d93ca6a130eb8d498dc51cba2763240fe1 Mon Sep 17 00:00:00 2001 From: Moustafa Elgammal Date: Sun, 1 Sep 2024 12:37:58 +0200 Subject: [PATCH 10/10] PRODUCTION_BUILD_VERSION=2.1.3 --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 5b8db0b0f..d5fcb0cbf 100644 --- a/.env +++ b/.env @@ -11,4 +11,4 @@ VITE_MTPROTO_AUTO=1 VITE_MTPROTO_HAS_HTTP=1 VITE_MTPROTO_HAS_WS=1 VITE_SAFARI_PROXY_WEBSOCKET= -PRODUCTION_BUILD_VERSION=2.1.2 +PRODUCTION_BUILD_VERSION=2.1.3