|
| 1 | +# Git Build Artifacts Prevention |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Git was picking up build files during build/deployment processes, causing: |
| 6 | +- Unintended tracking of temporary files |
| 7 | +- Repository bloat with build artifacts |
| 8 | +- Conflicts during deployment |
| 9 | +- Source maps and minified files being committed |
| 10 | + |
| 11 | +## Solution |
| 12 | + |
| 13 | +### 1. Enhanced .gitignore |
| 14 | + |
| 15 | +Updated `.gitignore` with comprehensive patterns for: |
| 16 | +- Build directories (`build/`, `dist/`, `hostinger-deploy/`) |
| 17 | +- Source maps (`*.map`) |
| 18 | +- Minified files (`*.min.js`, `*.min.css`) |
| 19 | +- Bundle files (`*.bundle.js`, `*.bundle.css`) |
| 20 | +- Cache directories (`.vite/`, `.cache/`, etc.) |
| 21 | +- Build process temporary files |
| 22 | +- Lock files created during builds |
| 23 | + |
| 24 | +### 2. Build Process Scripts |
| 25 | + |
| 26 | +#### Pre-build Script (`scripts/pre-build.sh`) |
| 27 | + |
| 28 | +- Creates lock files to indicate build in progress |
| 29 | +- Stashes uncommitted changes to prevent conflicts |
| 30 | +- Cleans existing build directories |
| 31 | +- Sets up clean build environment |
| 32 | + |
| 33 | +#### Post-build Script (`scripts/post-build.sh`) |
| 34 | + |
| 35 | +- Removes build lock files |
| 36 | +- Restores any stashed changes |
| 37 | +- Validates no build artifacts are tracked |
| 38 | +- Shows final Git status |
| 39 | + |
| 40 | +#### Cleanup Script (`scripts/clean-git-build-artifacts.sh`) |
| 41 | + |
| 42 | +- Removes accidentally tracked build artifacts |
| 43 | +- Cleans up temporary lock files |
| 44 | +- Shows what was cleaned |
| 45 | + |
| 46 | +### 3. Package.json Integration |
| 47 | + |
| 48 | +Added npm script hooks: |
| 49 | +- `prebuild`: Runs automatically before build |
| 50 | +- `postbuild`: Runs automatically after build |
| 51 | +- `clean:git`: Manual cleanup of Git artifacts |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +### Automatic (Recommended) |
| 56 | + |
| 57 | +Just run your normal build command: |
| 58 | +```bash |
| 59 | +npm run build |
| 60 | +``` |
| 61 | +The pre/post scripts will run automatically. |
| 62 | + |
| 63 | +### Manual Cleanup |
| 64 | + |
| 65 | +If you notice build files in Git: |
| 66 | +```bash |
| 67 | +npm run clean:git |
| 68 | +``` |
| 69 | + |
| 70 | +### Emergency Cleanup |
| 71 | + |
| 72 | +If build artifacts were committed: |
| 73 | +```bash |
| 74 | +./scripts/clean-git-build-artifacts.sh |
| 75 | +git commit -m "Remove build artifacts from tracking" |
| 76 | +``` |
| 77 | + |
| 78 | +## Prevention Tips |
| 79 | + |
| 80 | +1. **Always check Git status before committing:** |
| 81 | + ```bash |
| 82 | + git status |
| 83 | + ``` |
| 84 | + |
| 85 | +2. **Use .gitignore early** - Add patterns before first build |
| 86 | + |
| 87 | +3. **Separate build and source directories** clearly |
| 88 | + |
| 89 | +4. **Use CI/CD for builds** instead of local builds when possible |
| 90 | + |
| 91 | +5. **Review .gitignore regularly** as build tools evolve |
| 92 | + |
| 93 | +## Common Build Artifacts to Avoid |
| 94 | + |
| 95 | +- `*.map` - Source maps |
| 96 | +- `*.min.js` - Minified JavaScript |
| 97 | +- `*.min.css` - Minified CSS |
| 98 | +- `*.bundle.*` - Webpack/Vite bundles |
| 99 | +- Build directories (`build/`, `dist/`, etc.) |
| 100 | +- Cache directories (`.vite/`, `.cache/`, etc.) |
| 101 | +- Temporary files (`*.tmp`, `*.temp`) |
| 102 | +- Lock files from build processes |
| 103 | + |
| 104 | +## Troubleshooting |
| 105 | + |
| 106 | +### Build files still appearing in Git? |
| 107 | + |
| 108 | +1. Check if files were already tracked: `git ls-files | grep build` |
| 109 | +2. Run cleanup script: `npm run clean:git` |
| 110 | +3. Verify .gitignore patterns: `git check-ignore path/to/file` |
| 111 | + |
| 112 | +### Build process failing after changes? |
| 113 | + |
| 114 | +1. Ensure scripts are executable: `chmod +x scripts/*.sh` |
| 115 | +2. Check for shell compatibility (bash required) |
| 116 | +3. Verify no critical files were accidentally ignored |
| 117 | + |
| 118 | +### Stashed changes not restored? |
| 119 | + |
| 120 | +1. Check for `.build-stash-flag` file |
| 121 | +2. Manually restore: `git stash list` and `git stash pop` |
| 122 | + |
| 123 | +## Monitoring |
| 124 | + |
| 125 | +The post-build script automatically checks for tracked build artifacts and warns if any are found. Always review the output after builds. |
0 commit comments