From 26a687727fc64bd8f20bedc88c6e4778452a3a77 Mon Sep 17 00:00:00 2001 From: XHunter Date: Wed, 20 Dec 2023 17:36:40 +0530 Subject: [PATCH] minimize(builder): scp build folder for serve --- .github/workflows/main.yml | 59 ++++++++++++++++++++++++++------------ Dockerfile | 30 ++++++++++++------- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a7c0ac4..24f8fa8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,30 +1,53 @@ -name: Build +name: Koders Portfolio App on: push: - branches: ["main"] + branches: + - main jobs: - Deploy: + build: runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v3 - - name: Use Node.js 16.13.2 + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Node.js uses: actions/setup-node@v3 with: - node-version: 16.13.2 + node-version: 16.14.0 + + - name: Install dependencies + run: npm install --production + + - name: Build React app + run: npm run build + + deploy: + runs-on: ubuntu-latest + + needs: build + + steps: + - name: Copy build to VPS via SSH + uses: appleboy/scp-action@master + with: + host: ${{ secrets.REMOTE_HOST }} + username: ${{ secrets.REMOTE_USER }} + password: ${{ secrets.REMOTE_PASSWORD }} + source: "build/" + target: /root/portfolio/ - - name: Cleaning remote docker image and container - uses: fifsky/ssh-action@master - continue-on-error: true + - name: SSH into VPS and deploy + uses: appleboy/ssh-action@master with: - command: | - cd /root/portfolio/ - git pull - docker build . -t koders-portfolio - docker rm $(docker stop $(docker ps -a -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/koders-portfolio --format="{{.ID}}")) && - docker rmi koders-portfolio - docker run -d -p 8080:80 --name koders-portfolio koders-portfolio host: ${{ secrets.REMOTE_HOST }} - user: ${{ secrets.REMOTE_USER }} - pass: ${{ secrets.REMOTE_PASSWORD}} + username: ${{ secrets.REMOTE_USER }} + password: ${{ secrets.REMOTE_PASSWORD }} + script: | + cd /root/portfolio + docker rm $(docker stop $(docker ps -a -q --filter ancestor=koders-website-v5 --format="{{.ID}}")) + docker rmi koders-website-v5 + docker build . -t koders-website-v5 + docker run -d -p 8081:3000 --name koders-website-v5 -e ${{ secrets.WEBHOOK_URL }} koders-website-v5 diff --git a/Dockerfile b/Dockerfile index f5ce3fa..f396314 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,22 @@ -FROM node:slim as build-stage -ENV NODE_OPTIONS=--openssl-legacy-provider +# Builder already added via Github Actions +# Uncomment for full workflow action +# FROM node:slim as build-stage +# ENV NODE_OPTIONS=--openssl-legacy-provider +# WORKDIR /app +# COPY package.json ./ +# COPY package-lock.json ./ +# COPY . . +# RUN npm ci --force +# RUN npm run build +# FROM nginx:stable-alpine +# COPY --from=build-stage /app/build/ /usr/share/nginx/html +# EXPOSE 80 +# CMD ["nginx", "-g", "daemon off;"] + +FROM node:16-slim WORKDIR /app -COPY package.json ./ -COPY package-lock.json ./ -COPY . . -RUN npm ci --force -RUN npm run build -FROM nginx:stable-alpine -COPY --from=build-stage /app/build/ /usr/share/nginx/html +COPY build/ ./build/ EXPOSE 80 -CMD ["nginx", "-g", "daemon off;"] +RUN npm install -g serve +CMD ["serve", "-s", "build", "-p", "80"] +