Skip to content

Commit

Permalink
devops: update CI/CD pipeline for prisma migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbira committed Sep 4, 2024
1 parent 3f8c94c commit e18590a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
46 changes: 37 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ on:
branches: ["main"]

jobs:
changes:
runs-on: ubuntu-latest
outputs:
migrations: ${{ steps.filter.outputs.migrations }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
migrations:
- 'prisma/migrations/**'
lint:
runs-on: ubuntu-latest

Expand Down Expand Up @@ -117,7 +130,7 @@ jobs:
deploy-impaas:
needs:
- build-and-push-image
if: github.ref == 'refs/heads/main'
# if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
env:
TSURU_TOKEN: ${{ secrets.IMPAAS_DEPLOY_TOKEN }}
Expand All @@ -136,14 +149,6 @@ jobs:
- name: Create upload directories
run: |
tsuru app run "mkdir -p \$UPLOAD_DIR && cd \$UPLOAD_DIR && mkdir -p banners cvs avatars logos attachments" -a cpp-connect
- name: Set database URL
run: |
tsuru app run "DATABASE_URL=postgres://\$PGUSER:\$PGPASSWORD@\$PGHOST:\$PGPORT/\$PGDATABASE && echo \$DATABASE_URL" -a cpp-connect \
| grep "^postgres://" \
| xargs -I {} tsuru env set -a cpp-connect DATABASE_URL={}
- name: Run migrations
run: |
tsuru app run "npm exec prisma migrate deploy" -a cpp-connect
- name: Set environment variables
run: |
tsuru env set -a cpp-connect \
Expand All @@ -158,3 +163,26 @@ jobs:
MS_ENTRA_CLIENT_ID=${{ secrets.MS_ENTRA_CLIENT_ID }} \
MS_ENTRA_CLIENT_SECRET=${{ secrets.MS_ENTRA_CLIENT_SECRET }} \
MS_ENTRA_TENANT_ID=${{ secrets.MS_ENTRA_TENANT_ID }}
apply-db-migrations:
runs-on: ubuntu-latest
needs:
- changes
- deploy-impaas
if: github.ref == 'refs/heads/main' && needs.changes.outputs.migrations == 'true'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'
- name: Load cached node_modules
uses: actions/cache@v4
id: cache-primes
with:
path: node_modules
key: ${{ runner.os }}-nextjs-${{ hashFiles('package-lock.json') }}
- name: Apply all pending migrations to the database
run: npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
52 changes: 19 additions & 33 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
FROM node:18-alpine AS base

FROM base AS app_base

# Make UPLOAD_DIRs
ENV UPLOAD_DIR=/uploads
RUN mkdir $UPLOAD_DIR
RUN mkdir $UPLOAD_DIR/banners $UPLOAD_DIR/cvs $UPLOAD_DIR/avatars $UPLOAD_DIR/logos $UPLOAD_DIR/attachments

# Prisma base image only for installing prisma
FROM base AS prisma_base
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
Expand All @@ -15,54 +8,47 @@ WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm install prisma --omit=dev

FROM prisma_base AS deps
FROM prisma_base AS builder
ENV NEXT_TELEMETRY_DISABLED=1
WORKDIR /app

# Won't need to re-install prisma, since it's already installed in the prisma_base image
RUN npm ci

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate
RUN npm run build

# Disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
FROM base AS runner
ENV NEXT_TELEMETRY_DISABLED=1

RUN npm run build
# Make UPLOAD_DIRs
ENV UPLOAD_DIR=/uploads
RUN mkdir $UPLOAD_DIR
RUN mkdir $UPLOAD_DIR/banners $UPLOAD_DIR/cvs $UPLOAD_DIR/avatars $UPLOAD_DIR/logos $UPLOAD_DIR/attachments

# Production image, copy all the files and run next
FROM app_base AS runner
WORKDIR /app

# Disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1
# Take ownership of the app folder and uploads
RUN chown node:node /app
RUN chown -R node:node $UPLOAD_DIR

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown node:node .next

# Copy prisma schema
COPY --from=builder --chown=node:node /app/prisma ./prisma

# Copy over the prisma client
COPY --from=prisma_base /app/node_modules ./node_modules

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/public ./public
COPY --from=builder --chown=node:node /app/.next/standalone ./
COPY --from=builder --chown=node:node /app/.next/static ./.next/static

# Take ownership of the app folder and uploads
RUN chown node:node /app
RUN chown -R node:node $UPLOAD_DIR
COPY --from=builder --chown=node:node /app/prisma ./prisma
COPY --from=prisma_base /app/node_modules ./node_modules

USER node

EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
ENV NODE_ENV=production

CMD HOSTNAME="0.0.0.0" node server.js
CMD ["npm", "run", "start"]

0 comments on commit e18590a

Please sign in to comment.