test #197
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | |
| # Step 1: Checkout code | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| with: | |
| fetch-depth: 0 # Fetch all history for all branches and tags | |
| # Step 2: Set up Node.js environment | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v2 | |
| with: | |
| node-version: "16" | |
| # Step 3: Cache server dependencies | |
| - name: Cache server dependencies | |
| id: server-cache | |
| uses: actions/cache@v2 | |
| with: | |
| path: ./server/node_modules | |
| key: ${{ runner.os }}-server-${{ hashFiles('./server/package-lock.json') }} | |
| restore-keys: | | |
| ${{ runner.os }}-server- | |
| # Step 4: Install server dependencies (if not cached) | |
| - name: Install server dependencies | |
| working-directory: ./server | |
| run: npm install | |
| if: steps.server-cache.outputs.cache-hit != 'true' | |
| # Step 5: Start the Express backend | |
| - name: Start Express backend | |
| working-directory: ./server | |
| run: npm start & # Use '&' to run it in the background | |
| # Step 6: 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 | |
| # Step 7: Cache client dependencies | |
| - name: Cache client dependencies | |
| id: client-cache | |
| uses: actions/cache@v2 | |
| with: | |
| path: ./client/node_modules | |
| key: ${{ runner.os }}-client-${{ hashFiles('./client/package-lock.json') }} | |
| restore-keys: | | |
| ${{ runner.os }}-client- | |
| # Step 8: Install and build frontend (React 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 | |
| # Step 9: Move frontend build to server/public directory | |
| - name: Move frontend build to server/public | |
| run: | | |
| echo "Cleaning up existing public directory..." | |
| rm -rf ./server/public # Clean the public directory | |
| echo "Moving new build files to server/public..." | |
| mv ./client/dist ./server/public | |
| # Step 10: Verify frontend build in server/public | |
| - name: Verify frontend build in server/public | |
| run: ls -la ./server/public | |
| # Step 11: Debug step: MySQL logs | |
| - name: MySQL Logs | |
| run: docker logs $(docker ps -q --filter "ancestor=mysql:8.0") | |
| # Step 12: Deploy to Heroku (frontend and backend together) | |
| - 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 |