Skip to content

Instantly share code, notes, and snippets.

@esamattis
Last active December 21, 2023 20:21
Show Gist options
  • Save esamattis/bb82b149813aa8725d0e81892143b0d5 to your computer and use it in GitHub Desktop.
Save esamattis/bb82b149813aa8725d0e81892143b0d5 to your computer and use it in GitHub Desktop.
How to deploy a single app to a container from a pnpm monorepo using `pnpm deploy` in Github Actions
name: Deploy myapp as a container
jobs:
deploy-myapp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- uses: pnpm/[email protected]
with:
version: 7.9.5
- name: Set pnpm store path
run: echo "PNPM_STORE_PATH=$(pnpm store path)" >> $GITHUB_ENV
- name: Cache pnpm modules
uses: actions/cache@v2
with:
path: ${{ env.PNPM_STORE_PATH }}
key: pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
pnpm-${{ runner.os }}-
- name: Cache turborepo
uses: actions/cache@v2
with:
path: node_modules/.cache/turbo/
key: turbo-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
turbo-${{ runner.os }}-
- name: Install node modules only for the root package and myapp
run: pnpm install --filter root --filter myapp
# Turborepo is installed to the workspace root which is why
# we need to install root deps as well
- name: Build myapp only with Turborepo
run: ./node_modules/.bin/turbo run build --filter myapp
- name: Run pnpm deploy
run: pnpm deploy --filter myapp pnpm-deploy-output
- name: Authenticate to the Container Registry
run: XXX
- name: Build the container and push it to Container Registry
uses: docker/build-push-action@v2
with:
context: .
file: packages/myapp/Dockerfile
push: true
tags: XXX
- name: Deploy the container
run: XXX
FROM node:14-slim
ENV NODE_ENV=production
COPY pnpm-deploy-output /app
WORKDIR /app
ENTRYPOINT ["/app/entrypoint.sh"]
@rohanrajpal
Copy link

This is gold. Thanks a lot!

I was curious which platform do you use to deploy your project? I'm currently using Dokku, but was thinking to switch to AWS Elastic Beanstalk

@esamattis
Copy link
Author

@rohanrajpal We push the container to AWS ECR and deploy it to AWS ECS and AWS Batch using AWS CDK.

But not really recommending this if you are just running a simple web app. It's quite complicated but it makes sense for us because we deploy bunch of other infra as well to AWS with CDK.

@rohanrajpal
Copy link

rohanrajpal commented Sep 8, 2022

@rohanrajpal We push the container to AWS ECR and deploy it to AWS ECS and AWS Batch using AWS CDK.

But not really recommending this if you are just running a simple web app. It's quite complicated but it makes sense for us because we deploy bunch of other infra as well to AWS with CDK.

Got it, and how are the build times like? Is Github Actions poweful enough to build decent sized projects (nestjs project with ~1200 nodemodules) in a minute?

I know only way to know is to give it a shot, but just curious to know what your build times are right now.

@esamattis
Copy link
Author

@rohanrajpal looking at our last Github Actions run it installed 2301 node modules in 15 seconds. Got full cache hit (no downloaded modules). In comparison the same installation takes on my Intel i9 8-core MacBook Pro (2020) 23 seconds with the full cache hit.

@rohanrajpal
Copy link

@rohanrajpal looking at our last Github Actions run it installed 2301 node modules in 15 seconds. Got full cache hit (no downloaded modules). In comparison the same installation takes on my Intel i9 8-core MacBook Pro (2020) 23 seconds with the full cache hit.

Pretty fast! Guess it shall work then, thanks!

@creeefs
Copy link

creeefs commented Feb 5, 2023

@esamattis thanks for sharing this!

Wondering if you could share parts of your /app/entrypoint.sh file, since I'm getting "module not found" errors when trying to run the container.

@esamattis
Copy link
Author

esamattis commented Feb 6, 2023

@Clee681 It's just

#!/bin/sh

set -eu

exec node myapp.js

But the node_modules/.bin usage might be broken inside the container because the file system paths are different on GH Actions and the container. So invoke your app directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment