Skip to content

add postbuild

add postbuild #189

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch: # Enable manual trigger
jobs:
build:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: ${{ secrets.DATABASE }}
MYSQL_USER: ${{ secrets.USER }}
MYSQL_PASSWORD: ${{ secrets.PASSWORD }}
MYSQL_ROOT_PASSWORD: ${{ secrets.PASSWORD }}
ports:
- 33306:3306 # Expose the internal MySQL port 3306 as 33306
options: >-
--health-cmd "mysqladmin ping --silent"
--health-interval 20s
--health-timeout 10s
--health-retries 10
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetch all history for all branches and tags
# Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "16"
# Install server dependencies
- name: Install server dependencies
working-directory: ./server
run: npm install
# Start the Express backend
- name: Start Express backend
working-directory: ./server
run: npm start & # Use '&' to run it in the background
# Wait for Express server to be ready
- name: Wait for Express server to be ready
run: |
echo "Waiting for Express server to be ready..."
for i in {1..10}; do # Try for up to 10 attempts
if curl -s http://localhost:${{ secrets.PORT }}; then
echo "Express server is ready!"
break
fi
echo "Waiting for Express server..."
sleep 5
done
# Install and test backend (server)
- name: Install and test server
working-directory: ./server
env:
PORT: ${{ secrets.PORT }}
USER: ${{ secrets.USER }}
HOST: 127.0.0.1
DATABASE: ${{ secrets.DATABASE }}
PASSWORD: ${{ secrets.PASSWORD }}
DBPORT: 33306 # Match the changed port
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: npm test
# Install and build frontend (client)
- name: Install and build client
working-directory: ./client
env:
VITE_REACT_APP_CLIENT_ID: ${{ secrets.VITE_REACT_APP_CLIENT_ID }}
VITE_REACT_APP_MEALS_API_ID: ${{ secrets.VITE_REACT_APP_MEALS_API_ID }}
VITE_REACT_APP_MEALS_API_KEY: ${{ secrets.VITE_REACT_APP_MEALS_API_KEY }}
VITE_REACT_APP_GEOCODING_API_KEY: ${{ secrets.VITE_REACT_APP_GEOCODING_API_KEY }}
run: |
npm install
npm run build
mv ./dist ../server/public # Move built React files to the server's public directory
# Debug step: MySQL logs
- name: MySQL Logs
run: docker logs $(docker ps -q --filter "ancestor=mysql:8.0")
# Deploy to Heroku (only if on the main branch and build was successful)
- name: Deploy to Heroku
if: github.ref == 'refs/heads/main' && success()
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} # Ensure you have this secret set in GitHub
run: |
echo "Adding Heroku remote..."
git remote add heroku https://apikey:${{ secrets.HEROKU_API_KEY }}@git.heroku.com/gymero.git
echo "Committing built frontend..."
git config user.name "CI/CD Pipeline"
git config user.email "[email protected]"
git add server/public
git commit -m "Include frontend build artifacts [skip ci]" || echo "No changes to commit"
echo "Pushing to Heroku..."
git push heroku main --force # Ensure you are pushing the right branch