I have a React app served by an Express server and have set up GitHub Actions to handle the build on GitHub due to memory issues on Heroku. Previously, I was building the app directly on Heroku, and it worked fine, but I started hitting memory limits, so I switched the build process to GitHub Actions.
Folder Structure:
├── .github
├── client
├── src
├── .gitignore
├── Procfile
├── server.js
└── worker.js
GitHub Actions Workflow:
In GitHub Actions, I build the client and then deploy to Heroku.
deploy.yml:
name: Build and Deploy to Heroku
on:
push:
branches:
- staging # Change this if your default branch is different
jobs:
build-and-deploy:
runs-on: ubuntu-22.04
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Node.js (Server)
uses: actions/setup-node@v3
with:
node-version: '18.x'
cache: 'npm'
- name: Install Server Dependencies
run: npm ci
- name: Set up Node.js (Client)
uses: actions/setup-node@v3
with:
node-version: '18.x'
cache: 'npm'
- name: Install Client Dependencies
working-directory: ./client
run: npm ci
- name: Build Client
working-directory: ./client
env:
NODE_OPTIONS: --max_old_space_size=4096
CI: false
run: npm run build
# No need to copy build files; they are already in the expected location
- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.12.12
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
heroku_email: ${{ secrets.HEROKU_EMAIL }}
process_type: web worker
usedocker: false
Express Server Code:
expressApp.use(express.static(path.join(__dirname, 'client/build')));
expressApp.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
If I delete in .gitignore
the build folder I am able to see that I have a build folder in Heroku through its CLI, but I miss the node_modules. When I navigate to my url it shows the index.html is loaded and I can see the bundle but nothing renders.
When I delete node_modules from .gitignore I get the following error during Heroku Deployment after build and installations succeeded:
Error: Error: spawnSync /bin/sh ENOBUFS
So what I did was to add the dependencies through Heroku and not Github Actions:
"heroku-postbuild": "cd client && npm install",
Now I can see I do have everything but still nothing renders:
Running bash on ⬢ your-heroku-app... up, run.7224
~ $ ls
Procfile README.md Server.js client node_modules package-lock.json package.json src worker.js
~ $ cd client/
~/client $ ls
README.md build node_modules package-lock.json package.json public src tailwind.config.js