Skip to content

Commit

Permalink
fix CI and deploy epic-stack app in a monorepo setup (#1)
Browse files Browse the repository at this point in the history
The first version of the CI and Deploy that handles only one Epic-Stack app. Should be improved.

* Replace npm with pnpm in GitHub actions.
* Install Turbo
* Create basic turbo pipelines to run commands on all packages/app/.
* Update Dockerfile to use turbo (docker prune) and pnpm.
* Fix all occurrences of relative path to files to the harcoded path of the epic-app in the monorepo.
  • Loading branch information
PhilDL committed Sep 22, 2023
1 parent 5296754 commit a824868
Show file tree
Hide file tree
Showing 12 changed files with 502 additions and 242 deletions.
36 changes: 36 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.log
.git
.github
# editor
.idea
# dependencies
node_modules
.pnp
.pnp.js

# testing
coverage

# next.js
.next/
build

# packages
build
dist
packages/**/dist

# misc
.DS_Store
*.pem

.turbo
.vercel
.cache
.output
apps/**/public/build

cypress/screenshots
cypress/videos

packages/**/styles/tailwind.css
181 changes: 137 additions & 44 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ jobs:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
- uses: pnpm/action-setup@v2.4.0
with:
version: 8

- name: ⎔ Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
run: pnpm install --ignore-scripts

- name: 🖼 Build icons
run: npm run build:icons
run: pnpm run build

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

typecheck:
name: ʦ TypeScript
Expand All @@ -43,19 +48,27 @@ jobs:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
- uses: pnpm/action-setup@v2.4.0
with:
version: 8

- name: ⎔ Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
run: pnpm install --ignore-scripts

- name: 🛠 Setup Database
run: pnpm run generate

- name: 🖼 Build icons
run: npm run build:icons
run: pnpm run build

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

vitest:
name: ⚡ Vitest
Expand All @@ -64,22 +77,30 @@ jobs:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
- uses: pnpm/action-setup@v2.4.0
with:
version: 8

- name: ⎔ Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
run: pnpm install --ignore-scripts

- name: 🏄 Copy test env vars
run: cp .env.example .env
run: cp apps/epic-app/.env.example apps/epic-app/.env

- name: 🛠 Setup Database
run: pnpm run generate

- name: 🖼 Build icons
run: npm run build:icons
run: pnpm run build

- name: ⚡ Run vitest
run: npm run test -- --coverage
run: pnpm run test

playwright:
name: 🎭 Playwright
Expand All @@ -90,43 +111,46 @@ jobs:
uses: actions/checkout@v3

- name: 🏄 Copy test env vars
run: cp .env.example .env
run: cp ./apps/epic-app/.env.example ./apps/epic-app/.env

- uses: pnpm/action-setup@v2.4.0
with:
version: 8

- name: ⎔ Setup node
- name: ⎔ Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'

- name: 📥 Download deps
uses: bahmutov/npm-install@v1

- name: 📥 Install Playwright Browsers
run: npm run test:e2e:install
run: pnpm install

- name: 🛠 Setup Database
run: npx prisma migrate deploy
run: pnpm run db:migrate:force

- name: 🏦 Cache Database
id: db-cache
uses: actions/cache@v3
with:
path: prisma/data.db
key:
db-cache-schema_${{ hashFiles('./prisma/schema.prisma')
}}-migrations_${{ hashFiles('./prisma/migrations/*/migration.sql')
}}
db-cache-schema_${{
hashFiles('./apps/epic-stack/prisma/schema.prisma')
}}-migrations_${{
hashFiles('./apps/epic-stack/prisma/migrations/*/migration.sql') }}

- name: 🌱 Seed Database
if: steps.db-cache.outputs.cache-hit != 'true'
run: npx prisma db seed
run: pnpm db:seed
env:
MINIMAL_SEED: true

- name: 🏗 Build
run: npm run build
run: pnpm run build

- name: 🎭 Playwright tests
run: npx playwright test
- name: Install Playwright Browsers and run tests
run: pnpm test:e2e:ci

- name: 📊 Upload report
uses: actions/upload-artifact@v3
Expand All @@ -136,46 +160,115 @@ jobs:
path: playwright-report/
retention-days: 30

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' }}
runs-on: ubuntu-latest
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.11.0

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

- name: 👀 Read app name
uses: SebRollen/toml-action@v1.0.2
id: app_name
with:
file: 'apps/epic-app/fly.toml'
field: 'app'

- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
version: v0.9.1

# 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: 🔑 Fly Registry Auth
uses: docker/login-action@v2
with:
registry: registry.fly.io
username: x
password: ${{ secrets.FLY_API_TOKEN }}

- name: 🐳 Docker build
uses: docker/build-push-action@v4
with:
context: .
file: apps/epic-app/other/Dockerfile
push: true
tags:
registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}
build-args: |
COMMIT_SHA=${{ github.sha }}
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
deploy:
name: 🚀 Deploy
runs-on: ubuntu-latest
needs: [lint, typecheck, vitest, playwright]
# needs: [lint, typecheck, vitest, playwright, build]
needs: [lint, typecheck, vitest, build]
# only build/deploy main branch on pushes
if:
${{ (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') &&
github.event_name == 'push' }}

steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.11.0

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

- name: 👀 Read app name
uses: SebRollen/toml-action@v1.0.2
id: app_name
with:
file: 'fly.toml'
file: 'apps/epic-app/fly.toml'
field: 'app'

# move Dockerfile to root
- name: 🚚 Move Dockerfile
run: |
mv ./other/Dockerfile ./Dockerfile
mv ./other/.dockerignore ./.dockerignore
- name: 🎈 Setup Fly
uses: superfly/flyctl-actions/setup-flyctl@v1.4

- name: 🚀 Deploy Staging
if: ${{ github.ref == 'refs/heads/dev' }}
run:
flyctl deploy --remote-only --build-arg COMMIT_SHA=${{ github.sha }}
--app ${{ steps.app_name.outputs.value }}-staging
uses: superfly/flyctl-actions@v1.4
with:
args:
'deploy --app ${{ steps.app_name.outputs.value }}-staging --config
./apps/epic-app/fly.toml --image registry.fly.io/${{
steps.app_name.outputs.value }}:${{ github.ref_name }}-${{
github.sha }}'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

- name: 🚀 Deploy Production
if: ${{ github.ref == 'refs/heads/main' }}
run:
flyctl deploy --remote-only --build-arg COMMIT_SHA=${{ github.sha }}
uses: superfly/flyctl-actions@v1.4
with:
args:
'deploy --config ./apps/epic-app/fly.toml --image
registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ node_modules

# Easy way to create temporary files/folders that won't accidentally be added to git
*.local.*

.turbo

0 comments on commit a824868

Please sign in to comment.