Skip to content

Commit

Permalink
add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Varkoff committed Mar 23, 2024
1 parent fce512c commit 8dcc3e6
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 541 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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/main') && github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/[email protected]

- name: 🧑‍💻 Login to Docker Hub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
logout: true

- name: 🐳 Set up Docker Buildx
uses: docker/[email protected]

# Setup cache
- name: ⚡️ Cache Docker layers
uses: actions/[email protected]
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}-${{ github.ref_name }}
restore-keys: |
${{ runner.os }}-buildx-
- name: 🐳 Build Production Image
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/[email protected]
with:
context: .
push: true
tags: varkoff/nestjs-remix-monorepo:production
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# - name: 🐳 Build Staging Image
# if: ${{ github.ref == 'refs/heads/dev' }}
# uses: docker/[email protected]
# with:
# context: .
# push: true
# tags: varkoff/nestjs-remix-monorepo:latest

# 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
123 changes: 123 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: 🚀 Deploy
on:
push:
branches:
- main
- dev
pull_request: {}

permissions:
actions: write
contents: read

jobs:
lint:
name: ⬣ ESLint
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
TURBO_REMOTE_ONLY: true

steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm install

- name: Build Application
run: npm run build

- name: 🔬 Lint
run: npm run lint

typecheck:
name: ʦ TypeScript
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
TURBO_REMOTE_ONLY: true

steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm install

- name: Build Application
run: npm run build

- name: 🔎 Type check
run: npm run typecheck --if-present

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

deploy:
name: 🚀 Deploy
runs-on: [self-hosted]
needs: [build, lint, typecheck]
# needs: [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/main') && github.event_name == 'push' }}
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
TURBO_REMOTE_ONLY: true

steps:
- name: Cache node modules
uses: actions/[email protected]
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: ⬇️ Checkout repo
uses: actions/[email protected]

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# - name: 🚀 Run Docker Compose on Staging
# if: ${{ github.ref == 'refs/heads/dev' }}
# env:
# NODE_ENV: staging
# run: |
# docker pull varkoff/nestjs-remix-monorepo:latest
# docker compose -f docker-compose.staging.yaml up -d
# docker system prune --all --volumes --force

- name: 🚀 Run Docker Compose on Production
if: ${{ github.ref == 'refs/heads/main' }}
env:
NODE_ENV: production
run: |
docker pull varkoff/nestjs-remix-monorepo:production
docker compose -f docker-compose.prod.yaml up -d
docker system prune --all --volumes --force
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ pids

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
out/
66 changes: 66 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
FROM --platform=amd64 node:18-alpine As base

FROM base AS builder

# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm install --global turbo
COPY --chown=node:node . .
RUN turbo prune @virgile/backend --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --chown=node:node --from=builder /app/out/json/ .
COPY --chown=node:node --from=builder /app/out/package-lock.json ./package-lock.json
RUN npm install

# Build the project
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json

# Uncomment and use build args to enable remote caching
ARG TURBO_TEAM
ENV TURBO_TEAM=$TURBO_TEAM

ARG TURBO_TOKEN
ENV TURBO_TOKEN=$TURBO_TOKEN
ENV TZ=Europe/Paris
ENV NODE_ENV="production"

# ADD api/prisma api/prisma

# RUN cd api && npx prisma generate

RUN npm run build

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 remix-api
USER remix-api

# ENV TZ=Europe/Paris
# ENV NODE_ENV="production"

COPY --chown=remix-api:nodejs --from=installer /app/backend/package.json ./backend/package.json
COPY --chown=remix-api:nodejs --from=installer /app/backend/dist ./backend/dist
COPY --chown=remix-api:nodejs --from=installer /app/node_modules ./node_modules
COPY --chown=remix-api:nodejs --from=installer /app/node_modules/@virgile/frontend ./node_modules/@virgile/frontend
COPY --chown=remix-api:nodejs --from=installer /app/node_modules/@virgile/typescript-config ./node_modules/@virgile/typescript-config
COPY --chown=remix-api:nodejs --from=installer /app/node_modules/@virgile/eslint-config ./node_modules/@virgile/eslint-config

COPY --chown=remix-api:nodejs --from=builder /app/backend/start.sh ./backend/start.sh

# RUN cd backend && npm run start
ENTRYPOINT [ "backend/start.sh" ]
5 changes: 5 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
dist
6 changes: 6 additions & 0 deletions backend/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -ex
cd backend
# npx prisma migrate deploy
npm run start
13 changes: 13 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
monorepo_dev:
environment:
- NODE_ENV

container_name: nestjs-remix-monorepo-dev
build:
context: .
dockerfile: Dockerfile
# image: varkoff/nestjs-remix-monorepo:dev
restart: always
ports:
- 3000:3000
13 changes: 13 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
monorepo_prod:
environment:
- NODE_ENV

container_name: nestjs-remix-monorepo-prod
# build:
# context: .
# dockerfile: Dockerfile
image: varkoff/nestjs-remix-monorepo:production
restart: always
ports:
- 3000:3000
8 changes: 8 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/node_modules
*.log
.DS_Store
.env
/.cache
/public/build
/build
Loading

0 comments on commit 8dcc3e6

Please sign in to comment.