|
| 1 | +name: Deploy |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + workflow_dispatch: # Allows manual triggering |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + - uses: actions/setup-node@v3 |
| 14 | + with: |
| 15 | + node-version: 20 |
| 16 | + |
| 17 | + # Clean up existing workspace |
| 18 | + - run: | |
| 19 | + echo "Cleaning up workspace..." |
| 20 | + rm -rf hatch-project |
| 21 | + rm -rf apps |
| 22 | + rm -f nx.json |
| 23 | + rm -f package.json |
| 24 | + rm -f package-lock.json |
| 25 | + |
| 26 | + # Create temporary directory for the new workspace |
| 27 | + - run: | |
| 28 | + echo "Creating temporary directory..." |
| 29 | + mkdir -p /tmp/nx-workspace |
| 30 | + cd /tmp/nx-workspace |
| 31 | + |
| 32 | + echo "Creating new Nx workspace..." |
| 33 | + npx create-nx-workspace@latest . \ |
| 34 | + --preset=react-monorepo \ |
| 35 | + --appName=hatch_project \ |
| 36 | + --style=css \ |
| 37 | + --nxCloud=skip \ |
| 38 | + --packageManager=npm \ |
| 39 | + --no-interactive \ |
| 40 | + --defaultBase=main |
| 41 | + |
| 42 | + echo "Workspace contents:" |
| 43 | + ls -la |
| 44 | + |
| 45 | + echo "Copying workspace files back..." |
| 46 | + cd $GITHUB_WORKSPACE |
| 47 | + cp -r /tmp/nx-workspace/* . |
| 48 | + cp -r /tmp/nx-workspace/.* . 2>/dev/null || true |
| 49 | + |
| 50 | + # Configure base URL for GitHub Pages |
| 51 | + - run: | |
| 52 | + echo "Configuring base URL..." |
| 53 | + REPO_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2) |
| 54 | + echo "Base URL will be: /$REPO_NAME/" |
| 55 | + |
| 56 | + # Update vite.config.ts |
| 57 | + cat > apps/hatch_project/vite.config.ts << EOF |
| 58 | + /// <reference types='vitest' /> |
| 59 | + import { defineConfig } from 'vite'; |
| 60 | + import react from '@vitejs/plugin-react'; |
| 61 | + import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; |
| 62 | +
|
| 63 | + export default defineConfig({ |
| 64 | + root: __dirname, |
| 65 | + base: '/$REPO_NAME/', |
| 66 | + cacheDir: '../../node_modules/.vite/hatch_project', |
| 67 | +
|
| 68 | + plugins: [react(), nxViteTsPaths()], |
| 69 | +
|
| 70 | + build: { |
| 71 | + outDir: '../../dist/apps/hatch_project', |
| 72 | + emptyOutDir: true, |
| 73 | + reportCompressedSize: true, |
| 74 | + commonjsOptions: { transformMixedEsModules: true }, |
| 75 | + }, |
| 76 | +
|
| 77 | + test: { |
| 78 | + globals: true, |
| 79 | + cache: { |
| 80 | + dir: '../../node_modules/.vitest', |
| 81 | + }, |
| 82 | + environment: 'jsdom', |
| 83 | + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], |
| 84 | + }, |
| 85 | + }); |
| 86 | + EOF |
| 87 | + |
| 88 | + # Build for production |
| 89 | + - run: | |
| 90 | + echo "Running build command..." |
| 91 | + npx nx build hatch_project --configuration=production --verbose |
| 92 | + |
| 93 | + # Update HTML file with correct base URL |
| 94 | + - run: | |
| 95 | + echo "Updating base URL in index.html..." |
| 96 | + REPO_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2) |
| 97 | + |
| 98 | + # Use sed to update the base href and asset paths |
| 99 | + sed -i "s|<base href=\"/\"|<base href=\"/$REPO_NAME/\"|g" dist/apps/hatch_project/index.html |
| 100 | + sed -i "s|src=\"/assets|src=\"/$REPO_NAME/assets|g" dist/apps/hatch_project/index.html |
| 101 | + sed -i "s|href=\"/assets|href=\"/$REPO_NAME/assets|g" dist/apps/hatch_project/index.html |
| 102 | + sed -i "s|href=\"/favicon.ico|href=\"/$REPO_NAME/favicon.ico|g" dist/apps/hatch_project/index.html |
| 103 | + |
| 104 | + echo "Updated index.html contents:" |
| 105 | + cat dist/apps/hatch_project/index.html |
| 106 | + |
| 107 | + # Debug: Show build output |
| 108 | + - run: | |
| 109 | + echo "Build output structure:" |
| 110 | + ls -R dist/ || true |
| 111 | + |
| 112 | + echo "Looking for build files:" |
| 113 | + find . -type f \( \ |
| 114 | + -name "*.js" -o \ |
| 115 | + -name "*.html" -o \ |
| 116 | + -name "*.css" -o \ |
| 117 | + -name "*.json" -o \ |
| 118 | + -name "*.ico" -o \ |
| 119 | + -name "*.png" -o \ |
| 120 | + -name "*.svg" \ |
| 121 | + \) -not -path "./node_modules/*" -not -path "./.git/*" -not -path "./dist/*" |
| 122 | + |
| 123 | + echo "Contents of apps/hatch_project:" |
| 124 | + ls -la apps/hatch_project/ |
| 125 | + |
| 126 | + echo "Contents of dist directory (if exists):" |
| 127 | + ls -la dist/ || true |
| 128 | + |
| 129 | + # Verify build output |
| 130 | + - run: | |
| 131 | + echo "Verifying build output..." |
| 132 | + if [ ! -f "dist/apps/hatch_project/index.html" ]; then |
| 133 | + echo "Error: index.html not found in build output!" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + |
| 137 | + # Deploy to GitHub Pages |
| 138 | + - name: Deploy to GitHub Pages |
| 139 | + uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3 |
| 140 | + with: |
| 141 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + publish_dir: ./dist/apps/hatch_project |
| 143 | + enable_jekyll: false |
| 144 | + keep_files: false |
| 145 | + force_orphan: true |
0 commit comments