Skip to content

Commit

Permalink
move to vps
Browse files Browse the repository at this point in the history
  • Loading branch information
Varkoff committed Sep 21, 2024
1 parent b54d73b commit 6144c66
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules
*.log
.DS_Store
.env
/.cache
/public/build
/build
68 changes: 68 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: 🐳 Build And Push Docker Image
on:
workflow_call:
inputs:
tag:
type: string
description: The tag to push to the Docker registry.
# required: true
# default: latest

jobs:
build:
name: 🐳 Build
# only build/deploy main branch on pushes
# if: ${{ (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') && github.event_name == 'push' }}
if: ${{ (github.ref == 'refs/heads/strapi') && github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/[email protected]

- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: 🧑‍💻 Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
logout: true

- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Setup cache
- name: ⚡️ Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: 🐳 Build Production Image
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: algomax/ecommerce-frontend:production
build-args: |
COMMIT_SHA=${{ github.sha }} \
JWT_SECRET=${{ secrets.JWT_SECRET }} \
STRAPI_TOKEN=${{ secrets.STRAPI_TOKEN }} \
STRAPI_URL=${{ secrets.STRAPI_URL }} \
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: 🚚 Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
96 changes: 96 additions & 0 deletions .github/workflows/ci-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: 🚀 Deploy
on:
push:
branches:
- main
- dev
pull_request: {}

permissions:
actions: write
contents: read

jobs:
# lint:
# name: ⬣ ESLint
# runs-on: ubuntu-latest
# steps:
# - name: 🛑 Cancel Previous Runs
# uses: styfle/[email protected]

# - name: ⬇️ Checkout repo
# uses: actions/checkout@v3
# # - name: Setup Bun
# # uses: oven-sh/setup-bun@v1
# # - name: Install dependencies
# # run: npm install

# - name: ⎔ Setup node
# uses: actions/setup-node@v3
# with:
# node-version: 20

# - name: 📥 Download deps
# uses: bahmutov/npm-install@v1
# - name: Install dependencies
# run: npm install

# - name: 🔬 Lint
# run: npm run lint

# build:
# name: 🐳 build
# uses: ./.github/workflows/build.yml
# secrets: inherit

deploy:
name: 🚀 Deploy
runs-on: [self-hosted]
# needs: [build]
# only build/deploy main branch on pushes
if: ${{ (github.ref == 'refs/heads/main') && github.event_name == 'push' }}
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
STRAPI_TOKEN: ${{ secrets.STRAPI_TOKEN }}
STRAPI_URL: ${{ secrets.STRAPI_URL }}

steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/[email protected]

- name: ⬇️ Checkout repo
uses: actions/checkout@v3

# - name: Login to Docker Hub
# uses: docker/login-action@v2
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: 🚀 Run Docker Compose on Production
if: ${{ github.ref == 'refs/heads/main' }}
run: |
touch .env
echo "STRAPI_URL=${{ secrets.STRAPI_URL}}" >> .env
echo "DATABASE_CLIENT=${{ secrets.DATABASE_CLIENT}} >> .env
echo "DATABASE_HOST=strapiDB
echo "DATABASE_PORT=${{ secrets.DATABASE_PORT}} >> .env
echo "DATABASE_NAME=${{ secrets.DATABASE_NAME}} >> .env
echo "DATABASE_USERNAME=${{ secrets.DATABASE_USERNAME}} >> .env
echo "DATABASE_PASSWORD=${{ secrets.DATABASE_PASSWORD}} >> .env
echo "JWT_SECRET=${{ secrets.JWT_SECRET}} >> .env
echo "ADMIN_JWT_SECRET=${{ secrets.ADMIN_JWT_SECRET}} >> .env
echo "APP_KEYS=${{ secrets.APP_KEYS}} >> .env
echo "NODE_ENV=${{ secrets.NODE_ENV}} >> .env
echo "STRAPI_TOKEN=${{ secrets.STRAPI_TOKEN}} >> .env
echo "MAILGUN_API_KEY=${{ secrets.MAILGUN_API_KEY}} >> .env
echo "MAILGUN_DOMAIN=${{ secrets.MAILGUN_DOMAIN}} >> .env
echo "MAILGUN_URL=${{ secrets.MAILGUN_URL}} >> .env
echo "STRIPE_SECRET_KEY=${{ secrets.STRIPE_SECRET_KEY}} >> .env
echo "STRIPE_PUBLISHABLE_KEY=${{ secrets.STRIPE_PUBLISHABLE_KEY}} >> .env
echo "STRAPI_URL=${{ secrets.STRAPI_URL}} >> .env
echo "FRONTEND_URL=${{ secrets.FRONTEND_URL}} >> .env
echo "STRIPE_WEBHOOK_SECRET_KEY=${{ secrets.STRIPE_WEBHOOK_SECRET_KEY}} >> .env
docker compose -f docker-compose.prod.yml up --build -d
docker system prune --all --volumes --force
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:18-alpine3.18
# Installing libvips-dev for sharp Compatibility
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev git
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}

WORKDIR /opt/
COPY package.json package-lock.json ./
RUN npm install -g node-gyp
RUN npm config set fetch-retry-maxtimeout 600000 -g && npm install
ENV PATH /opt/node_modules/.bin:$PATH

WORKDIR /opt/app
COPY . .
RUN chown -R node:node /opt/app
USER node
RUN ["npm", "run", "build"]
EXPOSE 1337
CMD ["npm", "run", "develop"]
17 changes: 17 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
ecommerce_development:
environment:
- JWT_SECRET
- STRAPI_TOKEN
- STRAPI_URL

container_name: ecommerce_website_development
# image: algomax/ecommerce-frontend:development
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- '3000:3000'
volumes:
- ./image-cache:/app/image-cache
70 changes: 70 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
version: "3"
services:
strapi:
container_name: strapi
build: .
image: strapi:latest
restart: unless-stopped
env_file: .env
environment:
- DATABASE_CLIENT
- DATABASE_HOST=strapiDB
- DATABASE_PORT
- DATABASE_NAME
- DATABASE_USERNAME
- DATABASE_PASSWORD
- JWT_SECRET
- ADMIN_JWT_SECRET
- APP_KEYS
- NODE_ENV

- STRAPI_TOKEN
- MAILGUN_API_KEY
- MAILGUN_DOMAIN
- MAILGUN_URL
- STRIPE_SECRET_KEY
- STRIPE_PUBLISHABLE_KEY
- STRAPI_URL
- FRONTEND_URL
- STRIPE_WEBHOOK_SECRET_KEY

volumes:
- ./config:/opt/app/config
- ./src:/opt/app/src
- ./package.json:/opt/package.json
- ./package-lock.json:/opt/package-lock.json
- ./.env:/opt/app/.env
- ./public/uploads:/opt/app/public/uploads
ports:
- "1337:1337"
networks:
- strapi
depends_on:
- strapiDB

strapiDB:
container_name: strapiDB
platform: linux/amd64 #for platform error on Apple M1 chips
restart: unless-stopped
env_file: .env
image: postgres:12.0-alpine
environment:
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
volumes:
- strapi-data:/var/lib/postgresql/data/ #using a volume
#- ./data:/var/lib/postgresql/data/ # if you want to use a bind folder

ports:
- "5432:5432"
networks:
- strapi

volumes:
strapi-data:

networks:
strapi:
name: Strapi
driver: bridge
Loading

0 comments on commit 6144c66

Please sign in to comment.